From ef8fc1f2018412749fdb29d109245fe06bcbfc10 Mon Sep 17 00:00:00 2001 From: damarco Date: Fri, 11 May 2018 07:32:16 +0200 Subject: [PATCH] Update sensor state before adding device (#14357) --- homeassistant/components/sensor/zha.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/sensor/zha.py b/homeassistant/components/sensor/zha.py index 41dab282997..6979690708d 100644 --- a/homeassistant/components/sensor/zha.py +++ b/homeassistant/components/sensor/zha.py @@ -25,7 +25,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): return sensor = yield from make_sensor(discovery_info) - async_add_devices([sensor]) + async_add_devices([sensor], update_before_add=True) @asyncio.coroutine @@ -61,6 +61,11 @@ class Sensor(zha.Entity): value_attribute = 0 min_reportable_change = 1 + @property + def should_poll(self) -> bool: + """State gets pushed from device.""" + return False + @property def state(self) -> str: """Return the state of the entity.""" @@ -75,6 +80,14 @@ class Sensor(zha.Entity): self._state = value self.async_schedule_update_ha_state() + async def async_update(self): + """Retrieve latest state.""" + result = await zha.safe_read( + list(self._in_clusters.values())[0], + [self.value_attribute] + ) + self._state = result.get(self.value_attribute, self._state) + class TemperatureSensor(Sensor): """ZHA temperature sensor."""