Skip to content

Commit

Permalink
Zha switch schedule update state (#16621)
Browse files Browse the repository at this point in the history
* switch.zha: Schedule state update.

if turning switch On or Off operation was successful, then schedule
state update

* switch.zha: Update debug logging.
  • Loading branch information
Adminiuga authored and balloob committed Sep 21, 2018
1 parent aeaf694 commit 9fdf123
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions homeassistant/components/switch/zha.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ class Switch(zha.Entity, SwitchDevice):

def attribute_updated(self, attribute, value):
"""Handle attribute update from device."""
_LOGGER.debug("Attribute updated: %s %s %s", self, attribute, value)
cluster = self._endpoint.on_off
attr_name = cluster.attributes.get(attribute, [attribute])[0]
_LOGGER.debug("%s: Attribute '%s' on cluster '%s' updated to %s",
self.entity_id, attr_name, cluster.ep_attribute, value)
if attribute == self.value_attribute:
self._state = value
self.async_schedule_update_ha_state()
Expand All @@ -65,23 +68,29 @@ async def async_turn_on(self, **kwargs):
"""Turn the entity on."""
from zigpy.exceptions import DeliveryError
try:
await self._endpoint.on_off.on()
res = await self._endpoint.on_off.on()
_LOGGER.debug("%s: turned 'on': %s", self.entity_id, res[1])
except DeliveryError as ex:
_LOGGER.error("Unable to turn the switch on: %s", ex)
_LOGGER.error("%s: Unable to turn the switch on: %s",
self.entity_id, ex)
return

self._state = 1
self.async_schedule_update_ha_state()

async def async_turn_off(self, **kwargs):
"""Turn the entity off."""
from zigpy.exceptions import DeliveryError
try:
await self._endpoint.on_off.off()
res = await self._endpoint.on_off.off()
_LOGGER.debug("%s: turned 'off': %s", self.entity_id, res[1])
except DeliveryError as ex:
_LOGGER.error("Unable to turn the switch off: %s", ex)
_LOGGER.error("%s: Unable to turn the switch off: %s",
self.entity_id, ex)
return

self._state = 0
self.async_schedule_update_ha_state()

async def async_update(self):
"""Retrieve latest state."""
Expand Down

0 comments on commit 9fdf123

Please sign in to comment.