mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Mark device unavailable when it leaves Zigbee network. (#31264)
This commit is contained in:
parent
2c02334c1f
commit
37af2170ec
@ -12,6 +12,8 @@ import logging
|
|||||||
import os
|
import os
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
|
import zigpy.device as zigpy_dev
|
||||||
|
|
||||||
from homeassistant.components.system_log import LogEntry, _figure_out_source
|
from homeassistant.components.system_log import LogEntry, _figure_out_source
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.helpers.device_registry import (
|
from homeassistant.helpers.device_registry import (
|
||||||
@ -176,9 +178,9 @@ class ZHAGateway:
|
|||||||
"""Handle device joined and basic information discovered."""
|
"""Handle device joined and basic information discovered."""
|
||||||
self._hass.async_create_task(self.async_device_initialized(device))
|
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."""
|
"""Handle device leaving the network."""
|
||||||
pass
|
self.async_update_device(device, False)
|
||||||
|
|
||||||
async def _async_remove_device(self, device, entity_refs):
|
async def _async_remove_device(self, device, entity_refs):
|
||||||
if entity_refs is not None:
|
if entity_refs is not None:
|
||||||
@ -315,13 +317,13 @@ class ZHAGateway:
|
|||||||
self.async_update_device(sender)
|
self.async_update_device(sender)
|
||||||
|
|
||||||
@callback
|
@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."""
|
"""Update device that has just become available."""
|
||||||
if sender.ieee in self.devices:
|
if sender.ieee in self.devices:
|
||||||
device = self.devices[sender.ieee]
|
device = self.devices[sender.ieee]
|
||||||
# avoid a race condition during new joins
|
# avoid a race condition during new joins
|
||||||
if device.status is DeviceStatus.INITIALIZED:
|
if device.status is DeviceStatus.INITIALIZED:
|
||||||
device.update_available(True)
|
device.update_available(available)
|
||||||
|
|
||||||
async def async_update_device_storage(self):
|
async def async_update_device_storage(self):
|
||||||
"""Update the devices in the store."""
|
"""Update the devices in the store."""
|
||||||
|
29
tests/components/zha/test_gateway.py
Normal file
29
tests/components/zha/test_gateway.py
Normal 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
|
Loading…
x
Reference in New Issue
Block a user