Allow processing data in CoordinatorEntity before writing state (#40926)

pull/41045/head
Paulus Schoutsen 2020-10-02 09:16:37 +02:00 committed by GitHub
parent 187fec9a0c
commit 22b4ad6308
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 9 deletions

View File

@ -14,6 +14,7 @@ from homeassistant.components.climate.const import (
SUPPORT_TARGET_TEMPERATURE,
)
from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS, TEMP_FAHRENHEIT
from homeassistant.core import callback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import CONF_SUPPORTED_MODES, DATA_COORDINATOR, DATA_INFO, DOMAIN
@ -66,15 +67,10 @@ class CoolmasterClimate(CoordinatorEntity, ClimateEntity):
self._hvac_modes = supported_modes
self._info = info
def _refresh_from_coordinator(self):
@callback
def _handle_coordinator_update(self):
self._unit = self.coordinator.data[self._unit_id]
self.async_write_ha_state()
async def async_added_to_hass(self):
"""When entity is added to hass."""
self.async_on_remove(
self.coordinator.async_add_listener(self._refresh_from_coordinator)
)
super()._handle_coordinator_update()
@property
def device_info(self):

View File

@ -213,9 +213,14 @@ class CoordinatorEntity(entity.Entity):
"""When entity is added to hass."""
await super().async_added_to_hass()
self.async_on_remove(
self.coordinator.async_add_listener(self.async_write_ha_state)
self.coordinator.async_add_listener(self._handle_coordinator_update)
)
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self.async_write_ha_state()
async def async_update(self) -> None:
"""Update the entity.