From 41a4d40b71af8626f8a6aef4c0d9f30da4fb0f86 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Tue, 8 Feb 2022 19:54:10 +0100 Subject: [PATCH] Reuse existing coordinator entity update in Plugwise platforms (#66079) Co-authored-by: Martin Hjelmare --- .../components/plugwise/binary_sensor.py | 14 +++++++------- homeassistant/components/plugwise/climate.py | 6 +++--- homeassistant/components/plugwise/entity.py | 12 ++---------- homeassistant/components/plugwise/sensor.py | 16 ++++++++-------- homeassistant/components/plugwise/switch.py | 8 ++++---- 5 files changed, 24 insertions(+), 32 deletions(-) diff --git a/homeassistant/components/plugwise/binary_sensor.py b/homeassistant/components/plugwise/binary_sensor.py index 1f5cfacb37e..35d351b4094 100644 --- a/homeassistant/components/plugwise/binary_sensor.py +++ b/homeassistant/components/plugwise/binary_sensor.py @@ -99,11 +99,11 @@ class PlugwiseBinarySensorEntity(PlugwiseEntity, BinarySensorEntity): ).lstrip() @callback - def _async_process_data(self) -> None: - """Update the entity.""" + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" if not (data := self.coordinator.data.devices.get(self._dev_id)): LOGGER.error("Received no data for device %s", self._dev_id) - self.async_write_ha_state() + super()._handle_coordinator_update() return self._attr_is_on = data["binary_sensors"].get(self.entity_description.key) @@ -113,15 +113,15 @@ class PlugwiseBinarySensorEntity(PlugwiseEntity, BinarySensorEntity): if self.entity_description.key == "slave_boiler_state": self._attr_icon = FLAME_ICON if self._attr_is_on else IDLE_ICON - self.async_write_ha_state() + super()._handle_coordinator_update() class PlugwiseNotifyBinarySensorEntity(PlugwiseBinarySensorEntity): """Representation of a Plugwise Notification binary_sensor.""" @callback - def _async_process_data(self) -> None: - """Update the entity.""" + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" notify = self.coordinator.data.gateway["notifications"] self._attr_extra_state_attributes = {} @@ -144,4 +144,4 @@ class PlugwiseNotifyBinarySensorEntity(PlugwiseBinarySensorEntity): msg ) - self.async_write_ha_state() + super()._handle_coordinator_update() diff --git a/homeassistant/components/plugwise/climate.py b/homeassistant/components/plugwise/climate.py index 1cfb4ab7c67..89d7249ef38 100644 --- a/homeassistant/components/plugwise/climate.py +++ b/homeassistant/components/plugwise/climate.py @@ -150,8 +150,8 @@ class PlugwiseClimateEntity(PlugwiseEntity, ClimateEntity): LOGGER.error("Error while communicating to device") @callback - def _async_process_data(self) -> None: - """Update the data for this climate device.""" + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" data = self.coordinator.data.devices[self._dev_id] heater_central_data = self.coordinator.data.devices[ self.coordinator.data.gateway["heater_id"] @@ -192,4 +192,4 @@ class PlugwiseClimateEntity(PlugwiseEntity, ClimateEntity): "selected_schema": data.get("selected_schedule"), } - self.async_write_ha_state() + super()._handle_coordinator_update() diff --git a/homeassistant/components/plugwise/entity.py b/homeassistant/components/plugwise/entity.py index 2f73d38e5e3..7fdad9f4a91 100644 --- a/homeassistant/components/plugwise/entity.py +++ b/homeassistant/components/plugwise/entity.py @@ -2,7 +2,6 @@ from __future__ import annotations from homeassistant.const import ATTR_NAME, ATTR_VIA_DEVICE, CONF_HOST -from homeassistant.core import callback from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.update_coordinator import CoordinatorEntity @@ -49,12 +48,5 @@ class PlugwiseEntity(CoordinatorEntity[PlugwiseData]): async def async_added_to_hass(self) -> None: """Subscribe to updates.""" - self._async_process_data() - self.async_on_remove( - self.coordinator.async_add_listener(self._async_process_data) - ) - - @callback - def _async_process_data(self) -> None: - """Interpret and process API data.""" - raise NotImplementedError + self._handle_coordinator_update() + await super().async_added_to_hass() diff --git a/homeassistant/components/plugwise/sensor.py b/homeassistant/components/plugwise/sensor.py index 47b4f898c25..c99a5304ead 100644 --- a/homeassistant/components/plugwise/sensor.py +++ b/homeassistant/components/plugwise/sensor.py @@ -346,15 +346,15 @@ class PlugwiseSensorEnity(PlugwiseEntity, SensorEntity): ).lstrip() @callback - def _async_process_data(self) -> None: - """Update the entity.""" + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" if not (data := self.coordinator.data.devices.get(self._dev_id)): LOGGER.error("Received no data for device %s", self._dev_id) - self.async_write_ha_state() + super()._handle_coordinator_update() return self._attr_native_value = data["sensors"].get(self.entity_description.key) - self.async_write_ha_state() + super()._handle_coordinator_update() class PlugwiseAuxSensorEntity(PlugwiseSensorEnity): @@ -364,11 +364,11 @@ class PlugwiseAuxSensorEntity(PlugwiseSensorEnity): _heating_state = False @callback - def _async_process_data(self) -> None: - """Update the entity.""" + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" if not (data := self.coordinator.data.devices.get(self._dev_id)): LOGGER.error("Received no data for device %s", self._dev_id) - self.async_write_ha_state() + super()._handle_coordinator_update() return if data.get("heating_state") is not None: @@ -385,4 +385,4 @@ class PlugwiseAuxSensorEntity(PlugwiseSensorEnity): self._attr_native_value = "cooling" self._attr_icon = COOL_ICON - self.async_write_ha_state() + super()._handle_coordinator_update() diff --git a/homeassistant/components/plugwise/switch.py b/homeassistant/components/plugwise/switch.py index 6079862fe42..98288ca44b5 100644 --- a/homeassistant/components/plugwise/switch.py +++ b/homeassistant/components/plugwise/switch.py @@ -87,12 +87,12 @@ class PlugwiseSwitchEntity(PlugwiseEntity, SwitchEntity): self.async_write_ha_state() @callback - def _async_process_data(self) -> None: - """Update the data from the Plugs.""" + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" if not (data := self.coordinator.data.devices.get(self._dev_id)): LOGGER.error("Received no data for device %s", self._dev_id) - self.async_write_ha_state() + super()._handle_coordinator_update() return self._attr_is_on = data["switches"].get("relay") - self.async_write_ha_state() + super()._handle_coordinator_update()