From b0b62ddf328a9bba930181f058068ed491d59b9c Mon Sep 17 00:00:00 2001 From: Andrew Jackson Date: Mon, 6 May 2024 12:39:27 +0100 Subject: [PATCH] Conditional for SLR1x --- .../hive_local_thermostat/button.py | 47 +++- .../hive_local_thermostat/climate.py | 254 ++++++++++++++---- .../hive_local_thermostat/number.py | 75 ++++-- .../hive_local_thermostat/sensor.py | 154 +++++++---- 4 files changed, 389 insertions(+), 141 deletions(-) diff --git a/custom_components/hive_local_thermostat/button.py b/custom_components/hive_local_thermostat/button.py index 216ec00..4d72bf4 100644 --- a/custom_components/hive_local_thermostat/button.py +++ b/custom_components/hive_local_thermostat/button.py @@ -101,7 +101,9 @@ def __init__( def process_update(self, mqtt_data) -> None: """Update the state of the switch.""" - if (self.hass is not None): # this is a hack to get around the fact that the entity is not yet initialized at first + if ( + self.hass is not None + ): # this is a hack to get around the fact that the entity is not yet initialized at first self.async_schedule_update_ha_state() async def async_press(self) -> None: @@ -117,23 +119,44 @@ async def async_press(self) -> None: + r',"temperature_setpoint_hold_water":1}' ) elif self.entity_description.key == "boost_heating": - payload = ( - r'{"system_mode_heat":"emergency_heating","temperature_setpoint_hold_duration_heat":' - + str( - int( + if self.entity_description.model == MODEL_SLR2: + payload = ( + r'{"system_mode_heat":"emergency_heating","temperature_setpoint_hold_duration_heat":' + + str( + int( + self.get_entity_value( + "heating_boost_duration", DEFAULT_HEATING_BOOST_MINUTES + ) + ) + ) + + r',"temperature_setpoint_hold_heat":1,"occupied_heating_setpoint_heat":' + + str( self.get_entity_value( - "heating_boost_duration", DEFAULT_HEATING_BOOST_MINUTES + "heating_boost_temperature", + DEFAULT_HEATING_BOOST_TEMPERATURE, ) ) + + r"}" ) - + r',"temperature_setpoint_hold_heat":1,"occupied_heating_setpoint_heat":' - + str( - self.get_entity_value( - "heating_boost_temperature", DEFAULT_HEATING_BOOST_TEMPERATURE + else: + payload = ( + r'{"system_mode":"emergency_heating","temperature_setpoint_hold_duration":' + + str( + int( + self.get_entity_value( + "heating_boost_duration", DEFAULT_HEATING_BOOST_MINUTES + ) + ) ) + + r',"temperature_setpoint_hold":1,"occupied_heating_setpoint":' + + str( + self.get_entity_value( + "heating_boost_temperature", + DEFAULT_HEATING_BOOST_TEMPERATURE, + ) + ) + + r"}" ) - + r"}" - ) LOGGER.debug("Sending to {self._topic}/set message {payload}") await mqtt_client.async_publish(self.hass, self._topic + "/set", payload) diff --git a/custom_components/hive_local_thermostat/climate.py b/custom_components/hive_local_thermostat/climate.py index 5d2cd8d..a9b123a 100644 --- a/custom_components/hive_local_thermostat/climate.py +++ b/custom_components/hive_local_thermostat/climate.py @@ -22,9 +22,7 @@ ClimateEntityDescription, ) -from homeassistant.const import ( - Platform -) +from homeassistant.const import Platform from .entity import HiveEntity, HiveEntityDescription @@ -37,7 +35,8 @@ DEFAULT_FROST_TEMPERATURE, DEFAULT_HEATING_BOOST_MINUTES, DEFAULT_HEATING_BOOST_TEMPERATURE, - CONF_MODEL + CONF_MODEL, + MODEL_SLR2, ) PRESET_MAP = { @@ -45,6 +44,7 @@ PRESET_BOOST: HIVE_BOOST, } + @dataclass class HiveClimateEntityDescription( HiveEntityDescription, @@ -54,10 +54,10 @@ class HiveClimateEntityDescription( async def async_setup_entry( - hass: HomeAssistant, - config_entry: ConfigEntry, - async_add_entities: AddEntitiesCallback - ): + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +): """Set up the sensor platform.""" _entities = {} @@ -71,14 +71,13 @@ async def async_setup_entry( model=config_entry.options[CONF_MODEL], ) - _entities = [HiveClimateEntity(entity_description=hive_climate_entity_description) ] + _entities = [HiveClimateEntity(entity_description=hive_climate_entity_description)] - async_add_entities( - climateEntity for climateEntity in _entities - ) + async_add_entities(climateEntity for climateEntity in _entities) hass.data[DOMAIN][config_entry.entry_id][Platform.CLIMATE] = _entities + class HiveClimateEntity(HiveEntity, ClimateEntity): """hive_local_thermostat Climate class.""" @@ -89,7 +88,9 @@ def __init__( """Initialize the sensor class.""" self.entity_description = entity_description - self._attr_unique_id = f"{DOMAIN}_{entity_description.name}_{entity_description.key}".lower() + self._attr_unique_id = ( + f"{DOMAIN}_{entity_description.name}_{entity_description.key}".lower() + ) self._attr_has_entity_name = True self._topic = entity_description.topic @@ -123,14 +124,36 @@ def hvac_mode(self): if not self._mqtt_data: return - if self._mqtt_data["system_mode_heat"] == "heat" and self._mqtt_data["temperature_setpoint_hold_duration_heat"] !=65535: - return HVACMode.AUTO - if self._mqtt_data["system_mode_heat"] == "emergency_heating": - return HVACMode.HEAT - if self._mqtt_data["system_mode_heat"] == "heat" and self._mqtt_data["temperature_setpoint_hold_duration_heat"] ==65535: - return HVACMode.HEAT - if self._mqtt_data["system_mode_heat"] == "off": - return HVACMode.OFF + if self.entity_description.model == MODEL_SLR2: + if ( + self._mqtt_data["system_mode_heat"] == "heat" + and self._mqtt_data["temperature_setpoint_hold_duration_heat"] != 65535 + ): + return HVACMode.AUTO + if self._mqtt_data["system_mode_heat"] == "emergency_heating": + return HVACMode.HEAT + if ( + self._mqtt_data["system_mode_heat"] == "heat" + and self._mqtt_data["temperature_setpoint_hold_duration_heat"] == 65535 + ): + return HVACMode.HEAT + if self._mqtt_data["system_mode_heat"] == "off": + return HVACMode.OFF + else: + if ( + self._mqtt_data["system_mode"] == "heat" + and self._mqtt_data["temperature_setpoint_hold_duration"] != 65535 + ): + return HVACMode.AUTO + if self._mqtt_data["system_mode"] == "emergency_heating": + return HVACMode.HEAT + if ( + self._mqtt_data["system_mode"] == "heat" + and self._mqtt_data["temperature_setpoint_hold_duration"] == 65535 + ): + return HVACMode.HEAT + if self._mqtt_data["system_mode"] == "off": + return HVACMode.OFF async def async_set_hvac_mode(self, hvac_mode): """Set the hvac mode.""" @@ -142,27 +165,86 @@ async def async_set_hvac_mode(self, hvac_mode): return if hvac_mode == HVACMode.AUTO: - payload = r'{"system_mode_heat":"heat","temperature_setpoint_hold_heat":"0","temperature_setpoint_hold_duration_heat":"0"}' + if self.entity_description.model == MODEL_SLR2: + payload = r'{"system_mode_heat":"heat","temperature_setpoint_hold_heat":"0","temperature_setpoint_hold_duration_heat":"0"}' + else: + payload = r'{"system_mode":"heat","temperature_setpoint_hold":"0","temperature_setpoint_hold_duration":"0"}' LOGGER.debug("Sending to {self._topic}/set message {payload}") await mqtt_client.async_publish(self.hass, self._topic + "/set", payload) elif hvac_mode == HVACMode.HEAT: if self._pre_boost_occupied_heating_setpoint_heat: - payload = r'{"system_mode_heat":"heat","occupied_heating_setpoint_heat":' + str(self._pre_boost_occupied_heating_setpoint_heat) + r',"temperature_setpoint_hold_heat":"1","temperature_setpoint_hold_duration_heat":"0"}' + if self.entity_description.model == MODEL_SLR2: + payload = ( + r'{"system_mode_heat":"heat","occupied_heating_setpoint_heat":' + + str(self._pre_boost_occupied_heating_setpoint_heat) + + r',"temperature_setpoint_hold_heat":"1","temperature_setpoint_hold_duration_heat":"0"}' + ) + else: + payload = ( + r'{"system_mode":"heat","occupied_heating_setpoint":' + + str(self._pre_boost_occupied_heating_setpoint_heat) + + r',"temperature_setpoint_hold":"1","temperature_setpoint_hold_duration":"0"}' + ) else: - payload = r'{"system_mode_heat":"heat","occupied_heating_setpoint_heat":' + str(self.get_entity_value("heating_default_temperature", DEFAULT_HEATING_TEMPERATURE)) + r',"temperature_setpoint_hold_heat":"1","temperature_setpoint_hold_duration_heat":"0"}' + if self.entity_description.model == MODEL_SLR2: + payload = ( + r'{"system_mode_heat":"heat","occupied_heating_setpoint_heat":' + + str( + self.get_entity_value( + "heating_default_temperature", + DEFAULT_HEATING_TEMPERATURE, + ) + ) + + r',"temperature_setpoint_hold_heat":"1","temperature_setpoint_hold_duration_heat":"0"}' + ) + else: + payload = ( + r'{"system_mode":"heat","occupied_heating_setpoint":' + + str( + self.get_entity_value( + "heating_default_temperature", + DEFAULT_HEATING_TEMPERATURE, + ) + ) + + r',"temperature_setpoint_hold":"1","temperature_setpoint_hold_duration":"0"}' + ) LOGGER.debug("Sending to {self._topic}/set message {payload}") await mqtt_client.async_publish(self.hass, self._topic + "/set", payload) elif hvac_mode == HVACMode.OFF: - payload = r'{"system_mode_heat":"off","temperature_setpoint_hold_heat":"0"}' + if self.entity_description.model == MODEL_SLR2: + payload = ( + r'{"system_mode_heat":"off","temperature_setpoint_hold_heat":"0"}' + ) + else: + payload = r'{"system_mode":"off","temperature_setpoint_hold":"0"}' LOGGER.debug("Sending to {self._topic}/set message {payload}") await mqtt_client.async_publish(self.hass, self._topic + "/set", payload) sleep(0.5) - payload = r'{"occupied_heating_setpoint_heat":' + str(self.get_entity_value("heating_frost_prevention", DEFAULT_FROST_TEMPERATURE)) + r',"temperature_setpoint_hold_heat":"1","temperature_setpoint_hold_duration_heat:"65535"}' + if self.entity_description.model == MODEL_SLR2: + payload = ( + r'{"occupied_heating_setpoint_heat":' + + str( + self.get_entity_value( + "heating_frost_prevention", DEFAULT_FROST_TEMPERATURE + ) + ) + + r',"temperature_setpoint_hold_heat":"1","temperature_setpoint_hold_duration_heat:"65535"}' + ) + else: + payload = ( + r'{"occupied_heating_setpointt":' + + str( + self.get_entity_value( + "heating_frost_prevention", DEFAULT_FROST_TEMPERATURE + ) + ) + + r',"temperature_setpoint_hold":"1","temperature_setpoint_hold_duration:"65535"}' + ) LOGGER.debug("Sending to {self._topic}/set message {payload}") await mqtt_client.async_publish(self.hass, self._topic + "/set", payload) @@ -178,17 +260,30 @@ def hvac_action(self): if not self._mqtt_data: return - if "running_state_heat" not in self._mqtt_data: - return HVACAction.PREHEATING - - if self._mqtt_data["running_state_heat"] == "idle": - return HVACAction.IDLE - if self._mqtt_data["running_state_heat"] == "": - return HVACAction.PREHEATING - if self._mqtt_data["running_state_heat"] == "heat": - return HVACAction.HEATING - if self._mqtt_data["running_state_heat"] == "off": - return HVACAction.OFF + if self.entity_description.model == MODEL_SLR2: + if "running_state_heat" not in self._mqtt_data: + return HVACAction.PREHEATING + + if self._mqtt_data["running_state_heat"] == "idle": + return HVACAction.IDLE + if self._mqtt_data["running_state_heat"] == "": + return HVACAction.PREHEATING + if self._mqtt_data["running_state_heat"] == "heat": + return HVACAction.HEATING + if self._mqtt_data["running_state_heat"] == "off": + return HVACAction.OFF + else: + if "running_state" not in self._mqtt_data: + return HVACAction.PREHEATING + + if self._mqtt_data["running_state"] == "idle": + return HVACAction.IDLE + if self._mqtt_data["running_state"] == "": + return HVACAction.PREHEATING + if self._mqtt_data["running_state"] == "heat": + return HVACAction.HEATING + if self._mqtt_data["running_state"] == "off": + return HVACAction.OFF @property def current_temperature(self): @@ -196,8 +291,12 @@ def current_temperature(self): if not self._mqtt_data: return - if "local_temperature_heat" in self._mqtt_data: - return self._mqtt_data["local_temperature_heat"] + if self.entity_description.model == MODEL_SLR2: + if "local_temperature_heat" in self._mqtt_data: + return self._mqtt_data["local_temperature_heat"] + else: + if "local_temperature" in self._mqtt_data: + return self._mqtt_data["local_temperature"] @property def target_temperature(self): @@ -205,15 +304,25 @@ def target_temperature(self): if not self._mqtt_data: return - if "occupied_heating_setpoint_heat" in self._mqtt_data: - if self._mqtt_data["occupied_heating_setpoint_heat"] == 1: - return self.get_entity_value("heating_frost_prevention") - return self._mqtt_data["occupied_heating_setpoint_heat"] + + if self.entity_description.model == MODEL_SLR2: + if "occupied_heating_setpoint_heat" in self._mqtt_data: + if self._mqtt_data["occupied_heating_setpoint_heat"] == 1: + return self.get_entity_value("heating_frost_prevention") + return self._mqtt_data["occupied_heating_setpoint_heat"] + else: + if "occupied_heating_setpoint" in self._mqtt_data: + if self._mqtt_data["occupied_heating_setpoint"] == 1: + return self.get_entity_value("heating_frost_prevention") + return self._mqtt_data["occupied_heating_setpoint"] async def async_set_temperature(self, temperature, **kwargs): """Set the target temperature.""" - payload = r'{"occupied_heating_setpoint_heat":' + str(temperature) + r'}' + if self.entity_description.model == MODEL_SLR2: + payload = r'{"occupied_heating_setpoint_heat":' + str(temperature) + r"}" + else: + payload = r'{"occupied_heating_setpoint":' + str(temperature) + r"}" LOGGER.debug("Sending to {self._topic}/set message {payload}") await mqtt_client.async_publish(self.hass, self._topic + "/set", payload) @@ -221,7 +330,9 @@ async def async_set_temperature(self, temperature, **kwargs): def _climate_preset(self, mode): """Get the current preset.""" - return next((k for k, v in PRESET_MAP.items() if v == mode), PRESET_MAP[PRESET_NONE]) + return next( + (k for k, v in PRESET_MAP.items() if v == mode), PRESET_MAP[PRESET_NONE] + ) @property def preset_mode(self): @@ -229,8 +340,13 @@ def preset_mode(self): if not self._mqtt_data: return - if "system_mode_heat" in self._mqtt_data: - return self._climate_preset(self._mqtt_data["system_mode_heat"]) + + if self.entity_description.model == MODEL_SLR2: + if "system_mode_heat" in self._mqtt_data: + return self._climate_preset(self._mqtt_data["system_mode_heat"]) + else: + if "system_mode" in self._mqtt_data: + return self._climate_preset(self._mqtt_data["system_mode"]) async def async_set_preset_mode(self, preset_mode): """Set the preset mode.""" @@ -238,7 +354,45 @@ async def async_set_preset_mode(self, preset_mode): if preset_mode == "boost": self._pre_boost_hvac_mode = self.hvac_mode self._pre_boost_occupied_heating_setpoint_heat = self.target_temperature - payload = r'{"system_mode_heat":"emergency_heating","temperature_setpoint_hold_duration_heat":' + str(int(self.get_entity_value("heating_boost_duration", DEFAULT_HEATING_BOOST_MINUTES))) + r',"temperature_setpoint_hold_heat":1,"occupied_heating_setpoint_heat":' + str(self.get_entity_value("heating_boost_temperature", DEFAULT_HEATING_BOOST_TEMPERATURE)) + r'}' + + if self.entity_description.model == MODEL_SLR2: + payload = ( + r'{"system_mode_heat":"emergency_heating","temperature_setpoint_hold_duration_heat":' + + str( + int( + self.get_entity_value( + "heating_boost_duration", DEFAULT_HEATING_BOOST_MINUTES + ) + ) + ) + + r',"temperature_setpoint_hold_heat":1,"occupied_heating_setpoint_heat":' + + str( + self.get_entity_value( + "heating_boost_temperature", + DEFAULT_HEATING_BOOST_TEMPERATURE, + ) + ) + + r"}" + ) + else: + payload = ( + r'{"system_mode":"emergency_heating","temperature_setpoint_hold_duration":' + + str( + int( + self.get_entity_value( + "heating_boost_duration", DEFAULT_HEATING_BOOST_MINUTES + ) + ) + ) + + r',"temperature_setpoint_hold":1,"occupied_heating_setpoint":' + + str( + self.get_entity_value( + "heating_boost_temperature", + DEFAULT_HEATING_BOOST_TEMPERATURE, + ) + ) + + r"}" + ) LOGGER.debug("Sending to {self._topic}/set message {payload}") await mqtt_client.async_publish(self.hass, self._topic + "/set", payload) @@ -252,5 +406,7 @@ def process_update(self, mqtt_data) -> None: """Update the state of the sensor.""" self._mqtt_data = mqtt_data - if (self.hass is not None): # this is a hack to get around the fact that the entity is not yet initialized at first + if ( + self.hass is not None + ): # this is a hack to get around the fact that the entity is not yet initialized at first self.async_schedule_update_ha_state() diff --git a/custom_components/hive_local_thermostat/number.py b/custom_components/hive_local_thermostat/number.py index 75438fa..e2e5021 100644 --- a/custom_components/hive_local_thermostat/number.py +++ b/custom_components/hive_local_thermostat/number.py @@ -38,6 +38,7 @@ MODEL_SLR2, ) + @dataclass class HiveNumberEntityDescription( HiveEntityDescription, @@ -47,11 +48,12 @@ class HiveNumberEntityDescription( default_value: float | None = None + async def async_setup_entry( - hass: HomeAssistant, - config_entry: ConfigEntry, - async_add_entities: AddEntitiesCallback - ): + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +): """Set up the sensor platform.""" entity_descriptions = [ @@ -121,32 +123,37 @@ async def async_setup_entry( ] if config_entry.options[CONF_MODEL] == MODEL_SLR2: - entity_descriptions.append(HiveNumberEntityDescription( - key="water_boost_duration", - translation_key="water_boost_duration", - name=config_entry.title, - icon="mdi:timer", - topic=None, - entity_category=EntityCategory.CONFIG, - native_min_value=30, - native_max_value=180, - native_step=1, - default_value=DEFAULT_WATER_BOOST_MINUTES, - entry_id=config_entry.entry_id, - model=config_entry.options[CONF_MODEL], + entity_descriptions.append( + HiveNumberEntityDescription( + key="water_boost_duration", + translation_key="water_boost_duration", + name=config_entry.title, + icon="mdi:timer", + topic=None, + entity_category=EntityCategory.CONFIG, + native_min_value=30, + native_max_value=180, + native_step=1, + default_value=DEFAULT_WATER_BOOST_MINUTES, + entry_id=config_entry.entry_id, + model=config_entry.options[CONF_MODEL], ) ) _entities = {} - _entities = [HiveNumber(entity_description=entity_description,) for entity_description in entity_descriptions] + _entities = [ + HiveNumber( + entity_description=entity_description, + ) + for entity_description in entity_descriptions + ] - async_add_entities( - sensorEntity for sensorEntity in _entities - ) + async_add_entities(sensorEntity for sensorEntity in _entities) hass.data[DOMAIN][config_entry.entry_id][Platform.NUMBER] = _entities + class HiveNumber(HiveEntity, RestoreNumber): """hive_local_thermostat Number class.""" @@ -157,7 +164,9 @@ def __init__( """Initialize the sensor class.""" self.entity_description = entity_description - self._attr_unique_id = f"{DOMAIN}_{entity_description.name}_{entity_description.key}".lower() + self._attr_unique_id = ( + f"{DOMAIN}_{entity_description.name}_{entity_description.key}".lower() + ) self._attr_has_entity_name = True self._topic = entity_description.topic self._state = None @@ -171,10 +180,12 @@ async def async_added_to_hass(self) -> None: """Handle entity which will be added.""" await super().async_added_to_hass() - if ((last_state := await self.async_get_last_state()) and - (last_number_data := await self.async_get_last_number_data()) + if (last_state := await self.async_get_last_state()) and ( + last_number_data := await self.async_get_last_number_data() ): - self._attributes = dict_to_typed_dict(last_state.attributes, ["min", "max", "step"]) + self._attributes = dict_to_typed_dict( + last_state.attributes, ["min", "max", "step"] + ) if last_state.state not in (STATE_UNAVAILABLE, STATE_UNKNOWN): self._state = last_number_data.native_value else: @@ -185,9 +196,11 @@ async def async_added_to_hass(self) -> None: if self.entity_description.entry_id not in self.hass.data[DOMAIN]: self.hass.data[DOMAIN][self.entity_description.entry_id] = [] - self.hass.data[DOMAIN][self.entity_description.entry_id][self.entity_description.key] = self._state + self.hass.data[DOMAIN][self.entity_description.entry_id][ + self.entity_description.key + ] = self._state - LOGGER.debug(f'Restored {self.entity_description.key} state: {self._state}') + LOGGER.debug(f"Restored {self.entity_description.key} state: {self._state}") @property def native_value(self) -> float | None: @@ -202,7 +215,9 @@ async def async_set_native_value(self, value: float) -> None: if self.entity_description.entry_id not in self.hass.data[DOMAIN]: self.hass.data[DOMAIN][self.entity_description.entry_id] = [] - self.hass.data[DOMAIN][self.entity_description.entry_id][self.entity_description.key] = value + self.hass.data[DOMAIN][self.entity_description.entry_id][ + self.entity_description.key + ] = value self.async_write_ha_state() @@ -213,5 +228,7 @@ def extra_state_attributes(self): def process_update(self, mqtt_data) -> None: """Update the state of the sensor.""" - if (self.hass is not None): # this is a hack to get around the fact that the entity is not yet initialized at first + if ( + self.hass is not None + ): # this is a hack to get around the fact that the entity is not yet initialized at first self.async_schedule_update_ha_state() diff --git a/custom_components/hive_local_thermostat/sensor.py b/custom_components/hive_local_thermostat/sensor.py index 4b52309..459beb8 100644 --- a/custom_components/hive_local_thermostat/sensor.py +++ b/custom_components/hive_local_thermostat/sensor.py @@ -8,7 +8,11 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.temperature import display_temp as show_temp -from homeassistant.components.sensor import SensorEntity, SensorEntityDescription, SensorDeviceClass +from homeassistant.components.sensor import ( + SensorEntity, + SensorEntityDescription, + SensorDeviceClass, +) from homeassistant.const import ( Platform, UnitOfTemperature, @@ -25,6 +29,7 @@ MODEL_SLR2, ) + @dataclass class HiveSensorEntityDescription( HiveEntityDescription, @@ -35,52 +40,51 @@ class HiveSensorEntityDescription( func: any | None = None running_state: bool = False + async def async_setup_entry( - hass: HomeAssistant, - config_entry: ConfigEntry, - async_add_entities: AddEntitiesCallback - ): + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +): """Set up the sensor platform.""" - entity_descriptions = [ - HiveSensorEntityDescription( - key="running_state_heat", - translation_key="running_state_heat", - icon="mdi:radiator-disabled", - icons_by_state = { - "heat": "mdi:radiator", - "idle": "mdi:radiator-off", - "off": "mdi:radiator-off", - "preheating": "mdi:radiator", - }, - name=config_entry.title, - func=lambda js: js["running_state_heat"], - topic=config_entry.options[CONF_MQTT_TOPIC], - entry_id=config_entry.entry_id, - model=config_entry.options[CONF_MODEL], - running_state = True - ), - HiveSensorEntityDescription( - key="local_temperature_heat", - translation_key="local_temperature_heat", - icon="mdi:thermometer", - name=config_entry.title, - device_class=SensorDeviceClass.TEMPERATURE, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - suggested_display_precision = 1, - func=lambda js: js["local_temperature_heat"], - topic=config_entry.options[CONF_MQTT_TOPIC], - entry_id=config_entry.entry_id, - model=config_entry.options[CONF_MODEL], - ), - ] - if config_entry.options[CONF_MODEL] == MODEL_SLR2: - entity_descriptions.append(HiveSensorEntityDescription( + entity_descriptions = [ + HiveSensorEntityDescription( + key="running_state_heat", + translation_key="running_state_heat", + icon="mdi:radiator-disabled", + icons_by_state={ + "heat": "mdi:radiator", + "idle": "mdi:radiator-off", + "off": "mdi:radiator-off", + "preheating": "mdi:radiator", + }, + name=config_entry.title, + func=lambda js: js["running_state_heat"], + topic=config_entry.options[CONF_MQTT_TOPIC], + entry_id=config_entry.entry_id, + model=config_entry.options[CONF_MODEL], + running_state=True, + ), + HiveSensorEntityDescription( + key="local_temperature_heat", + translation_key="local_temperature_heat", + icon="mdi:thermometer", + name=config_entry.title, + device_class=SensorDeviceClass.TEMPERATURE, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=1, + func=lambda js: js["local_temperature_heat"], + topic=config_entry.options[CONF_MQTT_TOPIC], + entry_id=config_entry.entry_id, + model=config_entry.options[CONF_MODEL], + ), + HiveSensorEntityDescription( key="running_state_water", translation_key="running_state_water", icon="mdi:water-boiler", - icons_by_state = { + icons_by_state={ "heat": "mdi:water-boiler", "idle": "mdi:water-boiler-off", "off": "mdi:water-boiler-off", @@ -90,20 +94,57 @@ async def async_setup_entry( topic=config_entry.options[CONF_MQTT_TOPIC], entry_id=config_entry.entry_id, model=config_entry.options[CONF_MODEL], - running_state = True - ) - ) + running_state=True, + ), + ] + else: + entity_descriptions = [ + HiveSensorEntityDescription( + key="running_state_heat", + translation_key="running_state_heat", + icon="mdi:radiator-disabled", + icons_by_state={ + "heat": "mdi:radiator", + "idle": "mdi:radiator-off", + "off": "mdi:radiator-off", + "preheating": "mdi:radiator", + }, + name=config_entry.title, + func=lambda js: js["running_state"], + topic=config_entry.options[CONF_MQTT_TOPIC], + entry_id=config_entry.entry_id, + model=config_entry.options[CONF_MODEL], + running_state=True, + ), + HiveSensorEntityDescription( + key="local_temperature_heat", + translation_key="local_temperature_heat", + icon="mdi:thermometer", + name=config_entry.title, + device_class=SensorDeviceClass.TEMPERATURE, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=1, + func=lambda js: js["local_temperature"], + topic=config_entry.options[CONF_MQTT_TOPIC], + entry_id=config_entry.entry_id, + model=config_entry.options[CONF_MODEL], + ), + ] _entities = {} - _entities = [HiveSensor(entity_description=entity_description,) for entity_description in entity_descriptions] + _entities = [ + HiveSensor( + entity_description=entity_description, + ) + for entity_description in entity_descriptions + ] - async_add_entities( - sensorEntity for sensorEntity in _entities - ) + async_add_entities(sensorEntity for sensorEntity in _entities) hass.data[DOMAIN][config_entry.entry_id][Platform.SENSOR] = _entities + class HiveSensor(HiveEntity, SensorEntity): """hive_local_thermostat Sensor class.""" @@ -114,7 +155,9 @@ def __init__( """Initialize the sensor class.""" self.entity_description = entity_description - self._attr_unique_id = f"{DOMAIN}_{entity_description.name}_{entity_description.key}".lower() + self._attr_unique_id = ( + f"{DOMAIN}_{entity_description.name}_{entity_description.key}".lower() + ) self._attr_has_entity_name = True self._func = entity_description.func self._topic = entity_description.topic @@ -132,11 +175,20 @@ def process_update(self, mqtt_data) -> None: if self.entity_description.running_state: if new_value == "": new_value = "preheating" - self._attr_icon = self.entity_description.icons_by_state.get(new_value, ICON_UNKNOWN) + self._attr_icon = self.entity_description.icons_by_state.get( + new_value, ICON_UNKNOWN + ) if self.entity_description.device_class == SensorDeviceClass.TEMPERATURE: - new_value = show_temp(self.hass, new_value, self.entity_description.native_unit_of_measurement, PRECISION_TENTHS) + new_value = show_temp( + self.hass, + new_value, + self.entity_description.native_unit_of_measurement, + PRECISION_TENTHS, + ) self._attr_native_value = new_value - if (self.hass is not None): # this is a hack to get around the fact that the entity is not yet initialized at first + if ( + self.hass is not None + ): # this is a hack to get around the fact that the entity is not yet initialized at first self.async_schedule_update_ha_state()