Each time a device becomes available again, ZHA adds another firmware update listener that is never released

Open Beginner friendly
#1,107 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@zigpy-review-bot is already working on this.

Since Nov 28, 2026.

  • #1108 by @zigpy-review-bot — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
90/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend

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

bot: code audit
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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from zigpy/zha

All issues in zigpy/zha

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.