Mark device unavailable when it leaves Zigbee network. (#31264)

This commit is contained in:
Alexei Chetroi 2020-01-28 21:21:33 -05:00 committed by GitHub
parent 2c02334c1f
commit 37af2170ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 4 deletions

View File

@ -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."""

View File

@ -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