Devices that left the network are re-initialized on every availability check, reading attributes from a device that is gone
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 88/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- python
- Domain
- embedded-iot
Research direction
Start with BaseDevice._update_available and ZigbeeDevice._check_available in zha/zigbee/device.py, then review how Gateway.device_left changes on_network in zha/application/gateway.py. Reproduce the behavior using the supplied test for a mains-powered device that leaves after its last message. Done means repeated availability checks neither reinitialize the departed device nor read its cluster attributes, while a device returning to the network is still reinitialized once.
Written by the indexing model from the issue text.
Description
Problem
Severity: medium (the audit's estimate)
When a device leaves the network (it was factory reset to be paired again, or it sent a Leave on its own), ZHA keeps the device, marks it as not on the network and makes it unavailable. Its last message is still recent, though. Until that message is older than the "consider unavailable" time (2 hours for mains powered devices, 6 hours for battery powered ones by default), every pass of the availability checker, every 30 to 45 seconds, finds the device "seen recently" and re-initializes it: its entities are discovered again and their attributes are read from the device, which is no longer there to answer. The device stays unavailable the whole time, so Home Assistant shows nothing unusual, but the debug log shows "Device availability changed and device became available, reinitializing cluster handlers" for the device on every check, and the coordinator keeps sending it requests that can only time out.
The cause is in BaseDevice._update_available (https://github.com/zigpy/zha/blob/4d8e640c877c998e66b74ad2059c9e1ed1b07510/zha/zigbee/device.py#L780-L804). It compares the requested availability with the device's availability before the call, was_available, which includes on_network. When the device has left, Gateway.device_left sets on_network = False (https://github.com/zigpy/zha/blob/4d8e640c877c998e66b74ad2059c9e1ed1b07510/zha/application/gateway.py#L653-L657), and ZigbeeDevice._check_available still calls update_available(True) while the last message is recent (https://github.com/zigpy/zha/blob/4d8e640c877c998e66b74ad2059c9e1ed1b07510/zha/zigbee/device.py#L1475-L1482). was_available is False and the requested value is True, so every call takes the "became available" branch and starts _async_became_available, which runs async_initialize(False) (https://github.com/zigpy/zha/blob/4d8e640c877c998e66b74ad2059c9e1ed1b07510/zha/zigbee/device.py#L826-L828), although the device's availability doesn't change: available is still False afterwards, as on_network is.
Evidence
A test that lets a mains powered switch leave the network right after its last message, then runs three availability checks. It fails at 4d8e640c877c998e66b74ad2059c9e1ed1b07510: the three checks re-initialized the device three times and read attributes from its OnOff cluster six times.
Test and output
async def test_check_available_does_not_reinitialize_device_off_network(
zha_gateway: Gateway,
) -> None:
"""Test a device that left the network isn't re-initialized by every check."""
zigpy_dev = zigpy_device_mains(zha_gateway, with_basic_cluster=True)
zha_device = await join_zigpy_device(zha_gateway, zigpy_dev)
on_off_cluster = zigpy_dev.endpoints[3].on_off
# we want to test the device availability handling alone
zha_gateway.global_updater.stop()
# The device leaves the network right after it was last seen
zigpy_dev.last_seen = time.time()
zha_gateway.device_left(zigpy_dev)
await zha_gateway.async_block_till_done()
assert zha_device.available is False
on_off_cluster.read_attributes.reset_mock()
with patch.object(
zha_device, "async_initialize", wraps=zha_device.async_initialize
) as async_initialize:
# Its last message is still recent for the next availability checks
for _ in range(3):
await zha_device._check_available()
await zha_gateway.async_block_till_done()
# It stays unavailable, and is not re-initialized while it is gone
assert zha_device.available is False
assert on_off_cluster.read_attributes.await_count == 0
assert async_initialize.await_count == 0
...
> assert on_off_cluster.read_attributes.await_count == 0
E AssertionError: assert 6 == 0
E + where 6 = <AsyncMock id='135438639022496'>.await_count
E + where <AsyncMock id='135438639022496'> = <zigpy.zcl.clusters.general.OnOff object at 0x7b2e448e34d0>.read_attributes
With the reads assertion left out, the next one shows assert 3 == 0 for async_initialize.await_count.
Proposed fix
Set the new availability first and compare the resulting available, which includes on_network, with was_available. A device that left then stays unavailable without being re-initialized, and a device coming back on the network (a message from it sets on_network again) is still re-initialized once.
- Dominant language
- Python
- Stars
- 96
- Forks
- 84
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 30
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from zigpy/zha
-
Device override creates the generic entity instead of the vendor-specific one (Light instead of HueLight, ForceOnLight, MinTransitionLight)Possibly taken @zigpy-review-bot claimed this today. Openbot: code audit
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
Each time a device becomes available again, ZHA adds another firmware update listener that is never releasedPossibly taken @zigpy-review-bot claimed this 1 day ago. Openbot: code audit
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
Maintainers usually reply within 1 day
-
device specific quirks
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
entities
Difficulty 1/5 Under an hour Newbie friendliness 85/100
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
pimutils/vdirsyncer#1242 ·
-
priority:P2
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
QuantConnect/lean-cli#792 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
open-webui/open-webui#35360 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Suggest link: Destiny Verify (MIT zero-dependency pre-commit security, citation & link hygiene CLI)Open
Difficulty 1/5 Under an hour Newbie friendliness 68/100