Get zha switch and binary_sensor state on startup (#11672)

* Get zha switch and binary_sensor state on startup

* Removed unused var

* Make zha switch report status

* Use right method name

* Formatting fix

* Updates to match latest dev

* PR feedback updates

* Use async for cluster commands
This commit is contained in:
Steve Easley authored and Paulus Schoutsen committed 2018-03-08 19:31:52 -08:00
1 parent c5228cbc64
commit 5e2296f2a4
2 files changed
+57 -19

No files matched your search

+33 -10
View File
@@ -4,7 +4,6 @@ Switches on Zigbee Home Automation networks.
For more details on this platform, please refer to the documentation
at https://home-assistant.io/components/switch.zha/
"""
import asyncio
import logging
from homeassistant.components.switch import DOMAIN, SwitchDevice
@@ -15,19 +14,39 @@ _LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['zha']
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up Zigbee Home Automation switches."""
async def async_setup_platform(hass, config, async_add_devices,
discovery_info=None):
"""Set up the Zigbee Home Automation switches."""
discovery_info = zha.get_discovery_info(hass, discovery_info)
if discovery_info is None:
return
add_devices([Switch(**discovery_info)])
from zigpy.zcl.clusters.general import OnOff
in_clusters = discovery_info['in_clusters']
cluster = in_clusters[OnOff.cluster_id]
await cluster.bind()
await cluster.configure_reporting(0, 0, 600, 1,)
async_add_devices([Switch(**discovery_info)], update_before_add=True)
class Switch(zha.Entity, SwitchDevice):
"""ZHA switch."""
_domain = DOMAIN
value_attribute = 0
def attribute_updated(self, attribute, value):
"""Handle attribute update from device."""
_LOGGER.debug("Attribute updated: %s %s %s", self, attribute, value)
if attribute == self.value_attribute:
self._state = value
self.async_schedule_update_ha_state()
@property
def should_poll(self) -> bool:
"""Let zha handle polling."""
return False
@property
def is_on(self) -> bool:
@@ -36,14 +55,18 @@ class Switch(zha.Entity, SwitchDevice):
return False
return bool(self._state)
@asyncio.coroutine
def async_turn_on(self, **kwargs):
async def async_turn_on(self, **kwargs):
"""Turn the entity on."""
yield from self._endpoint.on_off.on()
await self._endpoint.on_off.on()
self._state = 1
@asyncio.coroutine
def async_turn_off(self, **kwargs):
async def async_turn_off(self, **kwargs):
"""Turn the entity off."""
yield from self._endpoint.on_off.off()
await self._endpoint.on_off.off()
self._state = 0
async def async_update(self):
"""Retrieve latest state."""
result = await zha.safe_read(self._endpoint.on_off,
['on_off'])
self._state = result.get('on_off', self._state)