Reuse existing coordinator entity update in Plugwise platforms (#66079)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Franck Nijhof 2022-02-08 19:54:10 +01:00 committed by GitHub
parent c93d389544
commit 41a4d40b71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 32 deletions

View File

@ -99,11 +99,11 @@ class PlugwiseBinarySensorEntity(PlugwiseEntity, BinarySensorEntity):
).lstrip() ).lstrip()
@callback @callback
def _async_process_data(self) -> None: def _handle_coordinator_update(self) -> None:
"""Update the entity.""" """Handle updated data from the coordinator."""
if not (data := self.coordinator.data.devices.get(self._dev_id)): if not (data := self.coordinator.data.devices.get(self._dev_id)):
LOGGER.error("Received no data for device %s", self._dev_id) LOGGER.error("Received no data for device %s", self._dev_id)
self.async_write_ha_state() super()._handle_coordinator_update()
return return
self._attr_is_on = data["binary_sensors"].get(self.entity_description.key) 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": if self.entity_description.key == "slave_boiler_state":
self._attr_icon = FLAME_ICON if self._attr_is_on else IDLE_ICON 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): class PlugwiseNotifyBinarySensorEntity(PlugwiseBinarySensorEntity):
"""Representation of a Plugwise Notification binary_sensor.""" """Representation of a Plugwise Notification binary_sensor."""
@callback @callback
def _async_process_data(self) -> None: def _handle_coordinator_update(self) -> None:
"""Update the entity.""" """Handle updated data from the coordinator."""
notify = self.coordinator.data.gateway["notifications"] notify = self.coordinator.data.gateway["notifications"]
self._attr_extra_state_attributes = {} self._attr_extra_state_attributes = {}
@ -144,4 +144,4 @@ class PlugwiseNotifyBinarySensorEntity(PlugwiseBinarySensorEntity):
msg msg
) )
self.async_write_ha_state() super()._handle_coordinator_update()

View File

@ -150,8 +150,8 @@ class PlugwiseClimateEntity(PlugwiseEntity, ClimateEntity):
LOGGER.error("Error while communicating to device") LOGGER.error("Error while communicating to device")
@callback @callback
def _async_process_data(self) -> None: def _handle_coordinator_update(self) -> None:
"""Update the data for this climate device.""" """Handle updated data from the coordinator."""
data = self.coordinator.data.devices[self._dev_id] data = self.coordinator.data.devices[self._dev_id]
heater_central_data = self.coordinator.data.devices[ heater_central_data = self.coordinator.data.devices[
self.coordinator.data.gateway["heater_id"] self.coordinator.data.gateway["heater_id"]
@ -192,4 +192,4 @@ class PlugwiseClimateEntity(PlugwiseEntity, ClimateEntity):
"selected_schema": data.get("selected_schedule"), "selected_schema": data.get("selected_schedule"),
} }
self.async_write_ha_state() super()._handle_coordinator_update()

View File

@ -2,7 +2,6 @@
from __future__ import annotations from __future__ import annotations
from homeassistant.const import ATTR_NAME, ATTR_VIA_DEVICE, CONF_HOST 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.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
@ -49,12 +48,5 @@ class PlugwiseEntity(CoordinatorEntity[PlugwiseData]):
async def async_added_to_hass(self) -> None: async def async_added_to_hass(self) -> None:
"""Subscribe to updates.""" """Subscribe to updates."""
self._async_process_data() self._handle_coordinator_update()
self.async_on_remove( await super().async_added_to_hass()
self.coordinator.async_add_listener(self._async_process_data)
)
@callback
def _async_process_data(self) -> None:
"""Interpret and process API data."""
raise NotImplementedError

View File

@ -346,15 +346,15 @@ class PlugwiseSensorEnity(PlugwiseEntity, SensorEntity):
).lstrip() ).lstrip()
@callback @callback
def _async_process_data(self) -> None: def _handle_coordinator_update(self) -> None:
"""Update the entity.""" """Handle updated data from the coordinator."""
if not (data := self.coordinator.data.devices.get(self._dev_id)): if not (data := self.coordinator.data.devices.get(self._dev_id)):
LOGGER.error("Received no data for device %s", self._dev_id) LOGGER.error("Received no data for device %s", self._dev_id)
self.async_write_ha_state() super()._handle_coordinator_update()
return return
self._attr_native_value = data["sensors"].get(self.entity_description.key) self._attr_native_value = data["sensors"].get(self.entity_description.key)
self.async_write_ha_state() super()._handle_coordinator_update()
class PlugwiseAuxSensorEntity(PlugwiseSensorEnity): class PlugwiseAuxSensorEntity(PlugwiseSensorEnity):
@ -364,11 +364,11 @@ class PlugwiseAuxSensorEntity(PlugwiseSensorEnity):
_heating_state = False _heating_state = False
@callback @callback
def _async_process_data(self) -> None: def _handle_coordinator_update(self) -> None:
"""Update the entity.""" """Handle updated data from the coordinator."""
if not (data := self.coordinator.data.devices.get(self._dev_id)): if not (data := self.coordinator.data.devices.get(self._dev_id)):
LOGGER.error("Received no data for device %s", self._dev_id) LOGGER.error("Received no data for device %s", self._dev_id)
self.async_write_ha_state() super()._handle_coordinator_update()
return return
if data.get("heating_state") is not None: if data.get("heating_state") is not None:
@ -385,4 +385,4 @@ class PlugwiseAuxSensorEntity(PlugwiseSensorEnity):
self._attr_native_value = "cooling" self._attr_native_value = "cooling"
self._attr_icon = COOL_ICON self._attr_icon = COOL_ICON
self.async_write_ha_state() super()._handle_coordinator_update()

View File

@ -87,12 +87,12 @@ class PlugwiseSwitchEntity(PlugwiseEntity, SwitchEntity):
self.async_write_ha_state() self.async_write_ha_state()
@callback @callback
def _async_process_data(self) -> None: def _handle_coordinator_update(self) -> None:
"""Update the data from the Plugs.""" """Handle updated data from the coordinator."""
if not (data := self.coordinator.data.devices.get(self._dev_id)): if not (data := self.coordinator.data.devices.get(self._dev_id)):
LOGGER.error("Received no data for device %s", self._dev_id) LOGGER.error("Received no data for device %s", self._dev_id)
self.async_write_ha_state() super()._handle_coordinator_update()
return return
self._attr_is_on = data["switches"].get("relay") self._attr_is_on = data["switches"].get("relay")
self.async_write_ha_state() super()._handle_coordinator_update()