zha: Clean up binary_sensor listener registration/state updates (#14197)

- Instead of registering listeners in the entity __init__, do it in
   async_added_to_hass to avoid errors updating an entity which isn't fully
   set up yet
 - Change from schedule_update_ha_state to async_schedule_update_ha_state
This commit is contained in:
Russell Cloran authored and Paulus Schoutsen committed 2018-05-01 08:55:25 -04:00
1 parent a4e0c9c251
commit 9d4d1c8233
3 files changed
+21 -16

No files matched your search

@@ -164,7 +164,6 @@ class Switch(zha.Entity, BinarySensorDevice):
"""Handle attribute updates on this cluster."""
if attrid == 0:
self._entity.set_state(value)
self._entity.schedule_update_ha_state()
def zdo_command(self, *args, **kwargs):
"""Handle ZDO commands on this cluster."""
@@ -202,6 +201,7 @@ class Switch(zha.Entity, BinarySensorDevice):
def __init__(self, **kwargs):
"""Initialize Switch."""
super().__init__(**kwargs)
self._state = True
self._level = 255
from zigpy.zcl.clusters import general
@@ -209,7 +209,6 @@ class Switch(zha.Entity, BinarySensorDevice):
general.OnOff.cluster_id: self.OnOffListener(self),
general.LevelControl.cluster_id: self.LevelListener(self),
}
super().__init__(**kwargs)
@property
def is_on(self) -> bool:
@@ -227,20 +226,20 @@ class Switch(zha.Entity, BinarySensorDevice):
self._level = 0
self._level = min(255, max(0, self._level + change))
self._state = bool(self._level)
self.schedule_update_ha_state()
self.async_schedule_update_ha_state()
def set_level(self, level):
"""Set the level, setting state if appropriate."""
self._level = level
self._state = bool(self._level)
self.schedule_update_ha_state()
self.async_schedule_update_ha_state()
def set_state(self, state):
"""Set the state."""
self._state = state
if self._level == 0:
self._level = 255
self.schedule_update_ha_state()
self.async_schedule_update_ha_state()
async def async_update(self):
"""Retrieve latest state."""