Re-paired device stays unavailable in Home Assistant until Z2M restart (availability never published after remove)

Aperta Adatta ai principianti
#33,855 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

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
typescript
Ambito
backend

Direzione di ricerca

Inizia da lib/extension/availability.ts, in particolare dai percorsi di rimozione del dispositivo e di pubblicazione della disponibilità, poi esamina test/extensions/availability.test.ts per la copertura esistente sulla disponibilità. Riproduci la sequenza di rimozione e ri-accoppiamento con un test che si aspetta che lo stato di disponibilità conservato del dispositivo venga pubblicato di nuovo come online. Il lavoro è completo quando il test di regressione passa, il comportamento di disponibilità è ripristinato senza un riavvio di Z2M e i controlli del progetto passano.

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

Descrizione

What happened?

I removed a contact sensor (SNZB-04P) from Z2M and paired the same device again without restarting Z2M. Pairing and interview went fine and the device kept sending contact/tamper/battery updates, but all of its entities in Home Assistant stayed unavailable.

Checking the MQTT debug info in HA, zigbee2mqtt/0xa4c138121812ffff/availability had no message at all. The removal clears the retained topic, and after the device joined again nothing was published there. Another device (Aqara MCCGQ12LM) that I paired a few minutes earlier got {"state":"online"} right away. It had not been in Z2M before.

After restarting the add-on, publishAvailabilityForAllEntities() published online and the sensor showed up correctly in HA.

Looking at lib/extension/availability.ts, publishAvailability() skips publishing when lastPublishedAvailabilities already holds the same value. When a device is removed, clearDevice() clears the timer and the debouncer but keeps that entry. So the re-paired device, which was online before removal, is never published as online again until a restart.

What did you expect to happen?

After removing a device and pairing it again, <device>/availability should be published as online once the device is seen, the same as for a new device.

How to reproduce it (minimal and precise)
  1. Enable availability. Have a device that is currently online.
  2. Remove it (frontend, or zigbee2mqtt/bridge/request/device/remove).
  3. Pair the same device again, without restarting Z2M.
  4. zigbee2mqtt/<ieee>/availability stays empty, so entities in HA that use it remain unavailable.

I have a small fix with a test for this: clearDevice() also deletes the device from lastPublishedAvailabilities. Two lines in availability.ts plus a test in test/extensions/availability.test.ts that fails before the change. The full test suite and pnpm run check pass. I used an AI assistant to help find the cause and write the fix and test, and I've reviewed and tested it myself. Would a PR for this be welcome?

Zigbee2MQTT version

2.14.2 (the same code is on dev at 3a0a038)

Adapter firmware version

20210708

Adapter

ZStack3x0

Setup

Home Assistant OS add-on

Lingua principale
TypeScript
Stelle
15.8k
Fork
2.1k
Merge medio
1g 4h
PR unite (30g)
40

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

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 Koenkk/zigbee2mqtt

Tutte le issue di Koenkk/zigbee2mqtt

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.