Configure ZHA entity on new ZHA device join (#19470)

* Address PR#19177 comments.

* Make 'new_join' part of ZhaEntity.

Call async_configure() automatically when new device ZHA device joins.
pull/19557/head
Alexei Chetroi 2018-12-23 14:47:06 -05:00 committed by Martin Hjelmare
parent 0b84eefa2e
commit 4a1da0b041
6 changed files with 19 additions and 40 deletions

View File

@ -60,20 +60,19 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
async def _async_setup_entities(hass, config_entry, async_add_entities,
discovery_infos):
"""Set up the ZHA binary sensors."""
from zigpy.zcl.clusters.general import OnOff
from zigpy.zcl.clusters.measurement import OccupancySensing
from zigpy.zcl.clusters.security import IasZone
entities = []
for discovery_info in discovery_infos:
from zigpy.zcl.clusters.general import OnOff
from zigpy.zcl.clusters.measurement import OccupancySensing
from zigpy.zcl.clusters.security import IasZone
if IasZone.cluster_id in discovery_info['in_clusters']:
entities.append(await _async_setup_iaszone(discovery_info))
elif OccupancySensing.cluster_id in discovery_info['in_clusters']:
entities.append(await _async_setup_occupancy(
DEVICE_CLASS_OCCUPANCY,
discovery_info
))
entities.append(
BinarySensor(DEVICE_CLASS_OCCUPANCY, **discovery_info))
elif OnOff.cluster_id in discovery_info['out_clusters']:
entities.append(await _async_setup_remote(discovery_info))
entities.append(Remote(**discovery_info))
async_add_entities(entities, update_before_add=True)
@ -97,21 +96,6 @@ async def _async_setup_iaszone(discovery_info):
return IasZoneSensor(device_class, **discovery_info)
async def _async_setup_remote(discovery_info):
remote = Remote(**discovery_info)
if discovery_info['new_join']:
await remote.async_configure()
return remote
async def _async_setup_occupancy(device_class, discovery_info):
sensor = BinarySensor(device_class, **discovery_info)
if discovery_info['new_join']:
await sensor.async_configure()
return sensor
class IasZoneSensor(RestoreEntity, ZhaEntity, BinarySensorDevice):
"""The IasZoneSensor Binary Sensor."""

View File

@ -70,10 +70,7 @@ async def _async_setup_entities(hass, config_entry, async_add_entities,
"""Set up the ZHA fans."""
entities = []
for discovery_info in discovery_infos:
fan = ZhaFan(**discovery_info)
if discovery_info['new_join']:
await fan.async_configure()
entities.append(fan)
entities.append(ZhaFan(**discovery_info))
async_add_entities(entities, update_before_add=True)

View File

@ -79,8 +79,6 @@ async def _async_setup_entities(hass, config_entry, async_add_entities,
CAPABILITIES_COLOR_TEMP
zha_light = Light(**discovery_info)
if discovery_info['new_join']:
await zha_light.async_configure()
entities.append(zha_light)
async_add_entities(entities, update_before_add=True)

View File

@ -84,8 +84,6 @@ async def make_sensor(discovery_info):
else:
sensor = Sensor(**discovery_info)
if discovery_info['new_join']:
await sensor.async_configure()
return sensor

View File

@ -46,10 +46,7 @@ async def _async_setup_entities(hass, config_entry, async_add_entities,
"""Set up the ZHA switches."""
entities = []
for discovery_info in discovery_infos:
switch = Switch(**discovery_info)
if discovery_info['new_join']:
await switch.async_configure()
entities.append(switch)
entities.append(Switch(**discovery_info))
async_add_entities(entities, update_before_add=True)
@ -119,7 +116,7 @@ class Switch(ZhaEntity, SwitchDevice):
async def async_update(self):
"""Retrieve latest state."""
result = await helpers.safe_read(self._endpoint.on_off,
result = await helpers.safe_read(self.cluster,
['on_off'],
allow_cache=False,
only_cache=(not self._initialized))

View File

@ -4,7 +4,7 @@ Entity for Zigbee Home Automation.
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/zha/
"""
from asyncio import sleep
import asyncio
import logging
from random import uniform
@ -25,7 +25,8 @@ class ZhaEntity(entity.Entity):
_domain = None # Must be overridden by subclasses
def __init__(self, endpoint, in_clusters, out_clusters, manufacturer,
model, application_listener, unique_id, **kwargs):
model, application_listener, unique_id, new_join=False,
**kwargs):
"""Init ZHA entity."""
self._device_state_attributes = {}
ieee = endpoint.device.ieee
@ -54,6 +55,7 @@ class ZhaEntity(entity.Entity):
self._endpoint = endpoint
self._in_clusters = in_clusters
self._out_clusters = out_clusters
self._new_join = new_join
self._state = None
self._unique_id = unique_id
@ -79,6 +81,9 @@ class ZhaEntity(entity.Entity):
self._endpoint.device.zdo.add_listener(self)
if self._new_join:
self.hass.async_create_task(self.async_configure())
self._initialized = True
async def async_configure(self):
@ -104,7 +109,7 @@ class ZhaEntity(entity.Entity):
manufacturer=manufacturer
)
skip_bind = True
await sleep(uniform(0.1, 0.5))
await asyncio.sleep(uniform(0.1, 0.5))
_LOGGER.debug("%s: finished configuration", self.entity_id)
def _get_cluster_from_report_config(self, cluster_key):
@ -152,7 +157,7 @@ class ZhaEntity(entity.Entity):
}
}
"""
return dict()
return {}
@property
def unique_id(self) -> str: