diff --git a/README.md b/README.md index ffecf15..46f51bf 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ Name | Description | Unit | State attributes `system_name` | control system name | `str` | `system_version` | control system version | `str` | `system_warning` | system warning | boolean | system error codes +`target_temperature` | target air temperature | `°C` | `temperature_mode` | current temperature mode setting | `str` | `ventilation_mode` | current ventilation mode setting | `str` | diff --git a/custom_components/saleryd_hrv/const.py b/custom_components/saleryd_hrv/const.py index 0002b6e..45dd625 100644 --- a/custom_components/saleryd_hrv/const.py +++ b/custom_components/saleryd_hrv/const.py @@ -37,7 +37,8 @@ """ # Other constants -CLIENT_STATE = "*HRV_CLIENT_STATE" +KEY_CLIENT_STATE = "*HRV_CLIENT_STATE" +KEY_TARGET_TEMPERATURE = "*TARGET_TEMPERATURE" TEMPERATURE_MODE_NORMAL = 0 TEMPERATURE_MODE_ECO = 1 diff --git a/custom_components/saleryd_hrv/coordinator.py b/custom_components/saleryd_hrv/coordinator.py index 44641fe..9dbd426 100644 --- a/custom_components/saleryd_hrv/coordinator.py +++ b/custom_components/saleryd_hrv/coordinator.py @@ -6,7 +6,7 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from pysaleryd.client import Client -from .const import CLIENT_STATE, DOMAIN +from .const import DOMAIN, KEY_CLIENT_STATE, KEY_TARGET_TEMPERATURE _LOGGER: logging.Logger = logging.getLogger(__package__) @@ -20,8 +20,13 @@ def __init__(self, hass: HomeAssistant, client: Client, update_interval) -> None self.client = client super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=update_interval) + def inject_virtual_keys(self, data): + """Inject additional keys for virtual sensors not present in the data set""" + data[KEY_CLIENT_STATE] = self.client.state + data[KEY_TARGET_TEMPERATURE] = None + async def _async_update_data(self): """Fetch the latest data from the source.""" data = self.client.data - data[CLIENT_STATE] = self.client.state + self.inject_virtual_keys(data) return data diff --git a/custom_components/saleryd_hrv/sensor.py b/custom_components/saleryd_hrv/sensor.py index ea44000..1217b36 100644 --- a/custom_components/saleryd_hrv/sensor.py +++ b/custom_components/saleryd_hrv/sensor.py @@ -23,7 +23,6 @@ from homeassistant.util import Throttle, slugify from .const import ( - CLIENT_STATE, DEFAULT_NAME, DOMAIN, HEATER_ACTIVE_MODE_OFF, @@ -31,6 +30,8 @@ HEATER_MODE_HIGH, HEATER_MODE_LOW, ISSUE_URL, + KEY_CLIENT_STATE, + KEY_TARGET_TEMPERATURE, SUPPORTED_FIRMWARES, SYSTEM_ACTIVE_MODE_OFF, SYSTEM_ACTIVE_MODE_ON, @@ -98,6 +99,19 @@ def _translate_value(self, value): return any(value) value = value[0] if isinstance(value, list) else value + + if self.entity_description.key == KEY_TARGET_TEMPERATURE: + try: + temperature_mode = self.coordinator.data.get("MT")[0] + if temperature_mode == TEMPERATURE_MODE_COOL: + return self.coordinator.data.get("TF")[0] + elif temperature_mode == TEMPERATURE_MODE_ECO: + return self.coordinator.data.get("TE")[0] + elif temperature_mode == TEMPERATURE_MODE_NORMAL: + return self.coordinator.data.get("TD")[0] + except TypeError as exc: + _LOGGER.debug(exc) + if self.entity_description.key == "MG": if value == HEATER_MODE_LOW: return 900 @@ -172,12 +186,15 @@ def extra_state_attributes(self) -> dict[str, Any] | None: value = value[0] if isinstance(value, list) else value if self.entity_description.key == "MT": - if value == TEMPERATURE_MODE_COOL: - attrs["target_temperature"] = self.coordinator.data.get("TF")[0] - elif value == TEMPERATURE_MODE_ECO: - attrs["target_temperature"] = self.coordinator.data.get("TE")[0] - elif value == TEMPERATURE_MODE_NORMAL: - attrs["target_temperature"] = self.coordinator.data.get("TD")[0] + try: + if value == TEMPERATURE_MODE_COOL: + attrs["target_temperature"] = self.coordinator.data.get("TF")[0] + elif value == TEMPERATURE_MODE_ECO: + attrs["target_temperature"] = self.coordinator.data.get("TE")[0] + elif value == TEMPERATURE_MODE_NORMAL: + attrs["target_temperature"] = self.coordinator.data.get("TD")[0] + except TypeError as exc: + _LOGGER.debug(exc) elif self.entity_description.key == "MF" and value == VENTILATION_MODE_BOOST: attrs["minutes_left"] = self.coordinator.data.get("*FI") @@ -277,6 +294,17 @@ def extra_state_attributes(self) -> dict[str, Any] | None: device_class=SensorDeviceClass.ENUM, ), }, + "target_temperature": { + "klass": SalerydLokeSensor, + "description": SensorEntityDescription( + key=KEY_TARGET_TEMPERATURE, + icon="mdi:home-thermometer", + name="Target temperature", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + ), + }, "temperature_mode": { "klass": SalerydLokeSensor, "description": SensorEntityDescription( @@ -342,7 +370,7 @@ def extra_state_attributes(self) -> dict[str, Any] | None: "connection_state": { "klass": SalerydLokeSensor, "description": SensorEntityDescription( - key=CLIENT_STATE, + key=KEY_CLIENT_STATE, icon="mdi:wrench-clock", name="Connection state", device_class=SensorDeviceClass.ENUM,