diff --git a/homeassistant/components/binary_sensor/zha.py b/homeassistant/components/binary_sensor/zha.py index 5f8e1baaec2..6bacabf73d8 100644 --- a/homeassistant/components/binary_sensor/zha.py +++ b/homeassistant/components/binary_sensor/zha.py @@ -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.""" diff --git a/homeassistant/components/fan/zha.py b/homeassistant/components/fan/zha.py index 0f302164689..1649f2e57ca 100644 --- a/homeassistant/components/fan/zha.py +++ b/homeassistant/components/fan/zha.py @@ -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) diff --git a/homeassistant/components/light/zha.py b/homeassistant/components/light/zha.py index 1dc61d87fda..5b06e8fa321 100644 --- a/homeassistant/components/light/zha.py +++ b/homeassistant/components/light/zha.py @@ -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) diff --git a/homeassistant/components/sensor/zha.py b/homeassistant/components/sensor/zha.py index d87ac623042..41499cddcb2 100644 --- a/homeassistant/components/sensor/zha.py +++ b/homeassistant/components/sensor/zha.py @@ -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 diff --git a/homeassistant/components/switch/zha.py b/homeassistant/components/switch/zha.py index 3f91ce767fb..0e1080664be 100644 --- a/homeassistant/components/switch/zha.py +++ b/homeassistant/components/switch/zha.py @@ -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)) diff --git a/homeassistant/components/zha/entities/entity.py b/homeassistant/components/zha/entities/entity.py index a3d9292ccbc..41493b487a3 100644 --- a/homeassistant/components/zha/entities/entity.py +++ b/homeassistant/components/zha/entities/entity.py @@ -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: