Each time a device becomes available again, ZHA adds another firmware update listener that is never released
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 90/100
Research direction
Start with BaseDevice.async_initialize and _async_became_available in zha/zigbee/device.py, then reproduce the duplicate listeners using the Philips SML001 fixture in tests/data/devices/philips-sml001-0x42006bb7.json. Update initialization so the same firmware update entity is not subscribed again, while a newly discovered or replaced entity still receives a listener. The issue's listener-count test should pass, showing one listener after repeated availability cycles.
Written by the indexing model from the issue text.
Description
Problem
Severity: low (the audit's estimate)
Every time a device with a firmware update entity becomes available again, ZHA subscribes the device to that entity's state changes once more, without dropping the earlier subscription. The listeners are only released when the device is removed, re-interviewed or ZHA shuts down. A device that regularly drops out and comes back, such as a battery device that reports less often than the "consider unavailable" time, a device on a weak link or one on a switched outlet, collects one more listener per cycle, for as long as Home Assistant runs, and every change of its update entity runs all of them. There's no visible symptom beyond the growing memory and work.
BaseDevice.async_initialize registers the listener on the first update entity and appends the unsubscribe callback to _on_remove_callbacks (https://github.com/zigpy/zha/blob/4d8e640c877c998e66b74ad2059c9e1ed1b07510/zha/zigbee/device.py#L1072-L1092). async_initialize runs again whenever the device becomes available (_async_became_available, https://github.com/zigpy/zha/blob/4d8e640c877c998e66b74ad2059c9e1ed1b07510/zha/zigbee/device.py#L826-L828), and that re-initialization keeps the existing update entity (rediscovered copies of existing entities are dropped), so the same entity gets one more listener each time.
Evidence
A test that makes a Philips SML001 (which has a firmware update entity) unavailable and available again three times. It fails at 4d8e640c877c998e66b74ad2059c9e1ed1b07510: the update entity has four device listeners instead of one.
Test and output
async def test_device_firmware_version_listener_not_duplicated(
zha_gateway: Gateway,
) -> None:
"""Test re-initializing a device keeps a single firmware version listener."""
zigpy_dev = await zigpy_device_from_json(
zha_gateway.application_controller,
"tests/data/devices/philips-sml001-0x42006bb7.json",
)
zha_device = await join_zigpy_device(zha_gateway, zigpy_dev)
update_entity = get_entity(zha_device, platform=Platform.UPDATE)
num_listeners = len(update_entity._listeners[STATE_CHANGED])
# The device becomes unavailable and available again a few times, and is
# re-initialized each time
for _ in range(3):
zha_device.update_available(False)
zha_device.update_available(True)
await zha_gateway.async_block_till_done()
assert len(update_entity._listeners[STATE_CHANGED]) == num_listeners
...
> assert len(update_entity._listeners[STATE_CHANGED]) == num_listeners
E assert 4 == 1
E + where 4 = len([EventListener(callback=<function BaseDevice.async_initialize.<locals>.entity_update_listener at 0x78b395bbc9e0>, with...callback=<function BaseDevice.async_initialize.<locals>.entity_update_listener at 0x78b395a6a1f0>, with_context=False)])
Proposed fix
Remember which update entity the device listens to, and skip the subscription when a re-initialization finds the same entity. A new entity (after a re-interview, or when the update entity was replaced) still gets its listener at the next initialization.
- 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
-
Devices that left the network are re-initialized on every availability check, reading attributes from a device that is gonePossibly taken @zigpy-review-bot claimed this 1 day ago. Openbot: code audit
Difficulty 2/5 1-3 hours Newbie friendliness 88/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