From a1ea6ff50754d6b7b314ebe73885568c4be29da0 Mon Sep 17 00:00:00 2001 From: Arjen Bos <1064589+arjenbos@users.noreply.github.com> Date: Tue, 31 Oct 2023 08:25:25 +0100 Subject: [PATCH] Update Battery Sensor and Binary Sensor --- .../alpha_innotec/binary_sensor.py | 27 ++++++++++++------- custom_components/alpha_innotec/climate.py | 10 +++---- custom_components/alpha_innotec/sensor.py | 24 ++++++++++++----- 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/custom_components/alpha_innotec/binary_sensor.py b/custom_components/alpha_innotec/binary_sensor.py index 47edc20..9c29817 100644 --- a/custom_components/alpha_innotec/binary_sensor.py +++ b/custom_components/alpha_innotec/binary_sensor.py @@ -6,7 +6,7 @@ from homeassistant.components.binary_sensor import BinarySensorEntity, BinarySensorEntityDescription, \ BinarySensorDeviceClass -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.typing import UndefinedType from homeassistant.helpers.update_coordinator import ( @@ -28,7 +28,7 @@ async def async_setup_entry(hass, entry, async_add_entities): gateway_api = hass.data[DOMAIN][entry.entry_id]['gateway_api'] - coordinator = AlphaCoordinator(hass, gateway_api) + coordinator = AlphaInnotecBinarySensorCoordinator(hass, gateway_api) await coordinator.async_config_entry_first_refresh() @@ -45,7 +45,7 @@ async def async_setup_entry(hass, entry, async_add_entities): async_add_entities(entities) -class AlphaCoordinator(DataUpdateCoordinator): +class AlphaInnotecBinarySensorCoordinator(DataUpdateCoordinator): """My custom coordinator.""" def __init__(self, hass: HomeAssistant, gateway_api: GatewayAPI): @@ -60,11 +60,7 @@ def __init__(self, hass: HomeAssistant, gateway_api: GatewayAPI): self.gateway_api: GatewayAPI = gateway_api async def _async_update_data(self) -> list[Valve]: - """Fetch data from API endpoint. - - This is the place to pre-process the data to lookup tables - so entities can quickly look up their data. - """ + """Fetch data from API endpoint.""" db_modules: dict = await self.hass.async_add_executor_job(self.gateway_api.db_modules) @@ -94,14 +90,15 @@ async def _async_update_data(self) -> list[Valve]: class AlphaHomeBinarySensor(CoordinatorEntity, BinarySensorEntity): - """Representation of a Sensor.""" + """Representation of a Binary Sensor.""" - def __init__(self, coordinator: AlphaCoordinator, name: str, description: BinarySensorEntityDescription, valve: Valve) -> None: + def __init__(self, coordinator: AlphaInnotecBinarySensorCoordinator, name: str, description: BinarySensorEntityDescription, valve: Valve) -> None: """Pass coordinator to CoordinatorEntity.""" super().__init__(coordinator, context=valve.identifier) self.entity_description = description self._attr_name = name self.valve = valve + self._attr_is_on = valve.status @property def device_info(self) -> DeviceInfo: @@ -130,3 +127,13 @@ def is_on(self) -> bool | None: @property def device_class(self) -> BinarySensorDeviceClass | None: return BinarySensorDeviceClass.OPENING + + @callback + def _handle_coordinator_update(self) -> None: + for valve in self.coordinator.data: + if valve.identifier == self.valve.identifier: + self.valve = valve + break + + _LOGGER.debug("Updating binary sensor: %s", self.valve.identifier) + self.async_write_ha_state() diff --git a/custom_components/alpha_innotec/climate.py b/custom_components/alpha_innotec/climate.py index 3cd0c5d..608f183 100644 --- a/custom_components/alpha_innotec/climate.py +++ b/custom_components/alpha_innotec/climate.py @@ -31,14 +31,14 @@ async def async_setup_entry(hass, entry, async_add_entities): controller_api = hass.data[DOMAIN][entry.entry_id]['controller_api'] gateway_api = hass.data[DOMAIN][entry.entry_id]['gateway_api'] - coordinator = AlphaCoordinator(hass, controller_api, gateway_api) + coordinator = AlphaInnotecClimateCoordinator(hass, controller_api, gateway_api) await coordinator.async_config_entry_first_refresh() entities = [] for thermostat in coordinator.data: - entities.append(AlphaHomeSensor( + entities.append(AlphaInnotecClimateSensor( coordinator=coordinator, api=controller_api, name=thermostat.name, @@ -49,7 +49,7 @@ async def async_setup_entry(hass, entry, async_add_entities): async_add_entities(entities) -class AlphaCoordinator(DataUpdateCoordinator, BaseCoordinator): +class AlphaInnotecClimateCoordinator(DataUpdateCoordinator, BaseCoordinator): """My custom coordinator.""" def __init__(self, hass: HomeAssistant, controller_api: ControllerAPI, gateway_api: GatewayAPI) -> None: @@ -73,7 +73,7 @@ async def _async_update_data(self) -> list[Thermostat]: return await self.get_thermostats(self.hass, self.gateway_api, self.controller_api) -class AlphaHomeSensor(CoordinatorEntity, ClimateEntity): +class AlphaInnotecClimateSensor(CoordinatorEntity, ClimateEntity): """Representation of a Sensor.""" _attr_precision = 0.1 @@ -82,7 +82,7 @@ class AlphaHomeSensor(CoordinatorEntity, ClimateEntity): ClimateEntityFeature.TARGET_TEMPERATURE ) - def __init__(self, coordinator: AlphaCoordinator, api: ControllerAPI, name: str, description: ClimateEntityDescription, thermostat: Thermostat) -> None: + def __init__(self, coordinator: AlphaInnotecClimateCoordinator, api: ControllerAPI, name: str, description: ClimateEntityDescription, thermostat: Thermostat) -> None: """Pass coordinator to CoordinatorEntity.""" super().__init__(coordinator, context=thermostat.identifier) self.api = api diff --git a/custom_components/alpha_innotec/sensor.py b/custom_components/alpha_innotec/sensor.py index cb9d392..07e630e 100644 --- a/custom_components/alpha_innotec/sensor.py +++ b/custom_components/alpha_innotec/sensor.py @@ -5,7 +5,7 @@ from datetime import timedelta from homeassistant.components.sensor import SensorEntity, SensorEntityDescription, SensorDeviceClass -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.update_coordinator import ( CoordinatorEntity, @@ -26,7 +26,7 @@ async def async_setup_entry(hass, entry, async_add_entities): controller_api = hass.data[DOMAIN][entry.entry_id]['controller_api'] gateway_api = hass.data[DOMAIN][entry.entry_id]['gateway_api'] - coordinator = AlphaCoordinator(hass, controller_api, gateway_api) + coordinator = AlphaInnotecSensorCoordinator(hass, controller_api, gateway_api) await coordinator.async_config_entry_first_refresh() @@ -37,7 +37,7 @@ async def async_setup_entry(hass, entry, async_add_entities): _LOGGER.warning("Skipping %s because battery status is unknown.", thermostat.name) continue - entities.append(AlphaHomeBatterySensor( + entities.append(AlphaInnotecBatterySensor( coordinator=coordinator, name=thermostat.name, description=SensorEntityDescription(""), @@ -47,7 +47,7 @@ async def async_setup_entry(hass, entry, async_add_entities): async_add_entities(entities) -class AlphaCoordinator(DataUpdateCoordinator, BaseCoordinator): +class AlphaInnotecSensorCoordinator(DataUpdateCoordinator, BaseCoordinator): """My custom coordinator.""" def __init__(self, hass: HomeAssistant, controller_api: ControllerAPI, gateway_api: GatewayAPI): @@ -71,13 +71,13 @@ async def _async_update_data(self) -> list[Thermostat]: return await self.get_thermostats(self.hass, self.gateway_api, self.controller_api) -class AlphaHomeBatterySensor(CoordinatorEntity, SensorEntity): +class AlphaInnotecBatterySensor(CoordinatorEntity, SensorEntity): """Representation of a Sensor.""" _attr_device_class = SensorDeviceClass.BATTERY _attr_native_unit_of_measurement = "%" - def __init__(self, coordinator: AlphaCoordinator, name: str, description: SensorEntityDescription, thermostat: Thermostat) -> None: + def __init__(self, coordinator: AlphaInnotecSensorCoordinator, name: str, description: SensorEntityDescription, thermostat: Thermostat) -> None: """Pass coordinator to CoordinatorEntity.""" super().__init__(coordinator, context=thermostat.identifier) self.entity_description = description @@ -100,3 +100,15 @@ def device_info(self) -> DeviceInfo: def unique_id(self) -> str: """Return unique ID for this device.""" return self.thermostat.identifier + + @callback + def _handle_coordinator_update(self) -> None: + for thermostat in self.coordinator.data: + if thermostat.identifier == self.thermostat.identifier: + self.thermostat = thermostat + break + + _LOGGER.debug("Updating sensor: %s", self.thermostat.identifier) + + self._attr_native_value = self.thermostat.battery_percentage + self.async_write_ha_state()