diff --git a/custom_components/versatile_thermostat/__init__.py b/custom_components/versatile_thermostat/__init__.py index 9663315..f56663a 100644 --- a/custom_components/versatile_thermostat/__init__.py +++ b/custom_components/versatile_thermostat/__init__.py @@ -6,17 +6,14 @@ from typing import Dict import logging from homeassistant.config_entries import ConfigEntry -from homeassistant.const import Platform from homeassistant.core import HomeAssistant from .climate import VersatileThermostat -from .const import DOMAIN +from .const import DOMAIN, PLATFORMS _LOGGER = logging.getLogger(__name__) -PLATFORMS: list[Platform] = [Platform.CLIMATE, Platform.BINARY_SENSOR, Platform.SENSOR] - async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Versatile Thermostat from a config entry.""" diff --git a/custom_components/versatile_thermostat/climate.py b/custom_components/versatile_thermostat/climate.py index 4193cab..83a79ab 100644 --- a/custom_components/versatile_thermostat/climate.py +++ b/custom_components/versatile_thermostat/climate.py @@ -25,6 +25,7 @@ from homeassistant.helpers.entity import DeviceInfo, DeviceEntryType from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_component import EntityComponent import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.reload import async_setup_reload_service from homeassistant.helpers.event import ( async_track_state_change_event, @@ -93,6 +94,7 @@ from homeassistant.const import ( from .const import ( DOMAIN, + PLATFORMS, DEVICE_MANUFACTURER, CONF_HEATER, CONF_POWER_SENSOR, @@ -156,6 +158,8 @@ async def async_setup_entry( "Calling async_setup_entry entry=%s, data=%s", entry.entry_id, entry.data ) + await async_setup_reload_service(hass, DOMAIN, PLATFORMS) + unique_id = entry.entry_id name = entry.data.get(CONF_NAME) @@ -1025,6 +1029,11 @@ class VersatileThermostat(ClimateEntity, RestoreEntity): """Returns the total energy calculated for this thermostast""" return self._total_energy + @property + def device_power(self) -> float | None: + """Returns the device_power for this thermostast""" + return self._device_power + @property def overpowering_state(self) -> bool | None: """Get the overpowering_state""" @@ -1506,6 +1515,9 @@ class VersatileThermostat(ClimateEntity, RestoreEntity): """Handle unerdlying climate state changes.""" new_state = event.data.get("new_state") _LOGGER.debug("%s - _async_climate_changed new_state is %s", self, new_state) + if not new_state: + return + old_state = event.data.get("old_state") old_hvac_action = ( old_state.attributes.get("hvac_action") diff --git a/custom_components/versatile_thermostat/const.py b/custom_components/versatile_thermostat/const.py index dbeed4a..2d4f46f 100644 --- a/custom_components/versatile_thermostat/const.py +++ b/custom_components/versatile_thermostat/const.py @@ -1,7 +1,8 @@ """Constants for the Versatile Thermostat integration.""" from enum import Enum -from homeassistant.const import CONF_NAME +from homeassistant.const import CONF_NAME, Platform + from homeassistant.components.climate import ( # PRESET_ACTIVITY, PRESET_BOOST, @@ -26,6 +27,8 @@ HIDDEN_PRESETS = [PRESET_POWER, PRESET_SECURITY] DOMAIN = "versatile_thermostat" +PLATFORMS: list[Platform] = [Platform.CLIMATE, Platform.BINARY_SENSOR, Platform.SENSOR] + CONF_HEATER = "heater_entity_id" CONF_TEMP_SENSOR = "temperature_sensor_entity_id" CONF_EXTERNAL_TEMP_SENSOR = "external_temperature_sensor_entity_id" diff --git a/custom_components/versatile_thermostat/sensor.py b/custom_components/versatile_thermostat/sensor.py index 381f000..de400af 100644 --- a/custom_components/versatile_thermostat/sensor.py +++ b/custom_components/versatile_thermostat/sensor.py @@ -4,7 +4,7 @@ import math from homeassistant.core import HomeAssistant, callback, Event -from homeassistant.const import UnitOfTime +from homeassistant.const import UnitOfTime, UnitOfPower, UnitOfEnergy, PERCENTAGE from homeassistant.components.sensor import ( SensorEntity, @@ -25,6 +25,8 @@ from .const import ( CONF_THERMOSTAT_TYPE, ) +THRESHOLD_WATT_KILO = 100 + _LOGGER = logging.getLogger(__name__) @@ -99,7 +101,13 @@ class EnergySensor(VersatileThermostatBaseEntity, SensorEntity): @property def native_unit_of_measurement(self) -> str | None: - return "kWh" + if not self.my_climate: + return None + + if self.my_climate.device_power > THRESHOLD_WATT_KILO: + return UnitOfEnergy.WATT_HOUR + else: + return UnitOfEnergy.KILO_WATT_HOUR @property def suggested_display_precision(self) -> int | None: @@ -150,7 +158,13 @@ class MeanPowerSensor(VersatileThermostatBaseEntity, SensorEntity): @property def native_unit_of_measurement(self) -> str | None: - return "kW" + if not self.my_climate: + return None + + if self.my_climate.device_power > THRESHOLD_WATT_KILO: + return UnitOfPower.WATT + else: + return UnitOfPower.KILO_WATT @property def suggested_display_precision(self) -> int | None: @@ -202,7 +216,7 @@ class OnPercentSensor(VersatileThermostatBaseEntity, SensorEntity): @property def native_unit_of_measurement(self) -> str | None: - return "%" + return PERCENTAGE @property def suggested_display_precision(self) -> int | None: diff --git a/custom_components/versatile_thermostat/services.yaml b/custom_components/versatile_thermostat/services.yaml index 792df12..1d2773d 100644 --- a/custom_components/versatile_thermostat/services.yaml +++ b/custom_components/versatile_thermostat/services.yaml @@ -1,3 +1,6 @@ +reload: + description: Reload all Versatile Thermostat entities. + set_presence: name: Set presence description: Force the presence mode in thermostat