From 37af2170ece90a21f8f49963bfa104631c6a5ac3 Mon Sep 17 00:00:00 2001 From: Alexei Chetroi Date: Tue, 28 Jan 2020 21:21:33 -0500 Subject: [PATCH] Mark device unavailable when it leaves Zigbee network. (#31264) --- homeassistant/components/zha/core/gateway.py | 10 ++++--- tests/components/zha/test_gateway.py | 29 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 tests/components/zha/test_gateway.py diff --git a/homeassistant/components/zha/core/gateway.py b/homeassistant/components/zha/core/gateway.py index 72b5aa87329..106b77d6602 100644 --- a/homeassistant/components/zha/core/gateway.py +++ b/homeassistant/components/zha/core/gateway.py @@ -12,6 +12,8 @@ import logging import os import traceback +import zigpy.device as zigpy_dev + from homeassistant.components.system_log import LogEntry, _figure_out_source from homeassistant.core import callback from homeassistant.helpers.device_registry import ( @@ -176,9 +178,9 @@ class ZHAGateway: """Handle device joined and basic information discovered.""" self._hass.async_create_task(self.async_device_initialized(device)) - def device_left(self, device): + def device_left(self, device: zigpy_dev.Device): """Handle device leaving the network.""" - pass + self.async_update_device(device, False) async def _async_remove_device(self, device, entity_refs): if entity_refs is not None: @@ -315,13 +317,13 @@ class ZHAGateway: self.async_update_device(sender) @callback - def async_update_device(self, sender): + def async_update_device(self, sender: zigpy_dev.Device, available: bool = True): """Update device that has just become available.""" if sender.ieee in self.devices: device = self.devices[sender.ieee] # avoid a race condition during new joins if device.status is DeviceStatus.INITIALIZED: - device.update_available(True) + device.update_available(available) async def async_update_device_storage(self): """Update the devices in the store.""" diff --git a/tests/components/zha/test_gateway.py b/tests/components/zha/test_gateway.py new file mode 100644 index 00000000000..5c6e8ecfe7a --- /dev/null +++ b/tests/components/zha/test_gateway.py @@ -0,0 +1,29 @@ +"""Test ZHA Gateway.""" +import zigpy.zcl.clusters.general as general + +import homeassistant.components.zha.core.const as zha_const + +from .common import async_enable_traffic, async_init_zigpy_device + + +async def test_device_left(hass, config_entry, zha_gateway): + """Test zha fan platform.""" + + # create zigpy device + zigpy_device = await async_init_zigpy_device( + hass, [general.Basic.cluster_id], [], None, zha_gateway + ) + + # load up fan domain + await hass.config_entries.async_forward_entry_setup(config_entry, zha_const.SENSOR) + await hass.async_block_till_done() + + zha_device = zha_gateway.get_device(zigpy_device.ieee) + + assert zha_device.available is False + + await async_enable_traffic(hass, zha_gateway, [zha_device]) + assert zha_device.available is True + + zha_gateway.device_left(zigpy_device) + assert zha_device.available is False