From d4c34c6b0298eef13a545d0a67f5697eaa25c84d Mon Sep 17 00:00:00 2001 From: "David F. Mulcahey" Date: Thu, 7 Feb 2019 03:23:01 -0500 Subject: [PATCH] Cleanup zha listener lifecycle (#20789) --- homeassistant/components/zha/__init__.py | 2 -- homeassistant/components/zha/core/device.py | 4 ---- homeassistant/components/zha/core/gateway.py | 7 ------ .../components/zha/core/listeners.py | 23 ++----------------- tests/components/zha/common.py | 1 - tests/components/zha/test_fan.py | 1 - tests/components/zha/test_light.py | 1 - tests/components/zha/test_sensor.py | 1 - tests/components/zha/test_switch.py | 1 - 9 files changed, 2 insertions(+), 39 deletions(-) diff --git a/homeassistant/components/zha/__init__.py b/homeassistant/components/zha/__init__.py index 2e693907769..ae08b2cac40 100644 --- a/homeassistant/components/zha/__init__.py +++ b/homeassistant/components/zha/__init__.py @@ -147,8 +147,6 @@ async def async_setup_entry(hass, config_entry): ) zha_gateway = ZHAGateway(hass, config) - hass.bus.async_listen_once( - ha_const.EVENT_HOMEASSISTANT_START, zha_gateway.accept_zigbee_messages) # Patch handle_message until zigpy can provide an event here def handle_message(sender, is_reply, profile, cluster, diff --git a/homeassistant/components/zha/core/device.py b/homeassistant/components/zha/core/device.py index 292f9817671..2322df5452c 100644 --- a/homeassistant/components/zha/core/device.py +++ b/homeassistant/components/zha/core/device.py @@ -170,10 +170,6 @@ class ZHADevice: await self._execute_listener_tasks('async_initialize', from_cache) _LOGGER.debug('%s: completed initialization', self.name) - async def async_accept_messages(self): - """Start accepting messages from the zigbee network.""" - await self._execute_listener_tasks('accept_messages') - async def _execute_listener_tasks(self, task_name, *args): """Gather and execute a set of listener tasks.""" listener_tasks = [] diff --git a/homeassistant/components/zha/core/gateway.py b/homeassistant/components/zha/core/gateway.py index 2722f6720ce..dca6b60ccc5 100644 --- a/homeassistant/components/zha/core/gateway.py +++ b/homeassistant/components/zha/core/gateway.py @@ -126,13 +126,6 @@ class ZHAGateway: self._devices[zigpy_device.ieee] = zha_device return zha_device - async def accept_zigbee_messages(self, _service_or_event): - """Allow devices to accept zigbee messages.""" - accept_messages_calls = [] - for device in self.devices.values(): - accept_messages_calls.append(device.async_accept_messages()) - await asyncio.gather(*accept_messages_calls) - async def async_device_initialized(self, device, is_new_join): """Handle device joined and basic information discovered (async).""" zha_device = await self._get_or_create_device(device) diff --git a/homeassistant/components/zha/core/listeners.py b/homeassistant/components/zha/core/listeners.py index 916319b2d98..d7c46bdfb3a 100644 --- a/homeassistant/components/zha/core/listeners.py +++ b/homeassistant/components/zha/core/listeners.py @@ -83,7 +83,6 @@ class ListenerStatus(Enum): CREATED = 1 CONFIGURED = 2 INITIALIZED = 3 - LISTENING = 4 class ClusterListener: @@ -99,6 +98,7 @@ class ClusterListener: [{'attr': 0, 'config': REPORT_CONFIG_DEFAULT}] ) self._status = ListenerStatus.CREATED + self._cluster.add_listener(self) @property def unique_id(self): @@ -156,11 +156,6 @@ class ClusterListener: """Initialize listener.""" self._status = ListenerStatus.INITIALIZED - async def accept_messages(self): - """Attach to the cluster so we can receive messages.""" - self._cluster.add_listener(self) - self._status = ListenerStatus.LISTENING - @callback def cluster_command(self, tsn, command_id, args): """Handle commands received to this cluster.""" @@ -354,12 +349,6 @@ class IASZoneListener(ClusterListener): name = 'zone' - def __init__(self, cluster, device): - """Initialize IASZoneListener.""" - super().__init__(cluster, device) - self._cluster.add_listener(self) - self._status = ListenerStatus.LISTENING - @callback def cluster_command(self, tsn, command_id, args): """Handle commands received to this cluster.""" @@ -429,10 +418,6 @@ class IASZoneListener(ClusterListener): await self.get_attribute_value('zone_state', from_cache=from_cache) await super().async_initialize(from_cache) - async def accept_messages(self): - """Attach to the cluster so we can receive messages.""" - self._status = ListenerStatus.LISTENING - class ActivePowerListener(AttributeListener): """Listener that polls active power level.""" @@ -625,6 +610,7 @@ class ZDOListener: self._zha_device = device self._status = ListenerStatus.CREATED self._unique_id = "{}_ZDO".format(device.name) + self._cluster.add_listener(self) @property def unique_id(self): @@ -651,11 +637,6 @@ class ZDOListener: """Permit handler.""" pass - async def accept_messages(self): - """Attach to the cluster so we can receive messages.""" - self._cluster.add_listener(self) - self._status = ListenerStatus.LISTENING - async def async_initialize(self, from_cache): """Initialize listener.""" self._status = ListenerStatus.INITIALIZED diff --git a/tests/components/zha/common.py b/tests/components/zha/common.py index f0b03a4b40b..1a923849ce5 100644 --- a/tests/components/zha/common.py +++ b/tests/components/zha/common.py @@ -161,7 +161,6 @@ def make_entity_id(domain, device, cluster, use_suffix=True): async def async_enable_traffic(hass, zha_gateway, zha_devices): """Allow traffic to flow through the gateway and the zha device.""" - await zha_gateway.accept_zigbee_messages({}) for zha_device in zha_devices: zha_device.update_available(True) await hass.async_block_till_done() diff --git a/tests/components/zha/test_fan.py b/tests/components/zha/test_fan.py index a67c8572072..6beafc6ca8e 100644 --- a/tests/components/zha/test_fan.py +++ b/tests/components/zha/test_fan.py @@ -26,7 +26,6 @@ async def test_fan(hass, config_entry, zha_gateway): # load up fan domain await hass.config_entries.async_forward_entry_setup( config_entry, DOMAIN) - await zha_gateway.accept_zigbee_messages({}) await hass.async_block_till_done() cluster = zigpy_device.endpoints.get(1).fan diff --git a/tests/components/zha/test_light.py b/tests/components/zha/test_light.py index 4712cac28d0..9c5e69d1347 100644 --- a/tests/components/zha/test_light.py +++ b/tests/components/zha/test_light.py @@ -40,7 +40,6 @@ async def test_light(hass, config_entry, zha_gateway): # load up light domain await hass.config_entries.async_forward_entry_setup( config_entry, DOMAIN) - await zha_gateway.accept_zigbee_messages({}) await hass.async_block_till_done() # on off light diff --git a/tests/components/zha/test_sensor.py b/tests/components/zha/test_sensor.py index bc367e15533..d16cafb7df8 100644 --- a/tests/components/zha/test_sensor.py +++ b/tests/components/zha/test_sensor.py @@ -103,7 +103,6 @@ async def async_build_devices(hass, zha_gateway, config_entry, cluster_ids): # load up sensor domain await hass.config_entries.async_forward_entry_setup( config_entry, DOMAIN) - await zha_gateway.accept_zigbee_messages({}) await hass.async_block_till_done() # put the other relevant info in the device info dict diff --git a/tests/components/zha/test_switch.py b/tests/components/zha/test_switch.py index 8b2553aae7a..32c8ee64e67 100644 --- a/tests/components/zha/test_switch.py +++ b/tests/components/zha/test_switch.py @@ -24,7 +24,6 @@ async def test_switch(hass, config_entry, zha_gateway): # load up switch domain await hass.config_entries.async_forward_entry_setup( config_entry, DOMAIN) - await zha_gateway.accept_zigbee_messages({}) await hass.async_block_till_done() cluster = zigpy_device.endpoints.get(1).on_off