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

Aperta Adatta ai principianti
#1,107 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@zigpy-review-bot ci sta già lavorando.

Dal 28/11/2026.

  • #1108 di @zigpy-review-bot — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
90/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Inizia con BaseDevice.async_initialize e _async_became_available in zha/zigbee/device.py, quindi riproduci i listener duplicati utilizzando il fixture Philips SML001 in tests/data/devices/philips-sml001-0x42006bb7.json. Aggiorna l’inizializzazione in modo che la stessa entità di aggiornamento del firmware non venga sottoscritta di nuovo, mentre un’entità appena rilevata o sostituita riceva ancora un listener. Il test di conteggio dei listener dell'issue deve superarsi e mostrare un listener dopo cicli di disponibilità ripetuti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
96
Fork
84
Merge medio
2g 4h
PR unite (30g)
30

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di zigpy/zha

Tutte le issue di zigpy/zha

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.