Propagate service call `Context` from ZHA group entities to member entities

Aperta
#1,006 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Start in homeassistant/components/zha/ and trace how group entity service calls reach the Zigbee multicast command, then find how the gateway proxy maps group members to registered Home Assistant entities. Check the base Entity.async_set_context behavior and the existing ZHA entity tests; done means member state updates after a group command retain its context while later physical updates remain independently attributed.

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

Descrizione

HA core
Context & Problem Description

When calling a service (such as light.turn_on, light.turn_off, switch.turn_on, etc.) on a native ZHA Group entity, the service call's Context (user_id, parent_id, id) is properly recorded on the ZHA Group entity itself. However, the resulting state changes on the individual member entities of that Zigbee group are recorded in the Home Assistant state machine and Activity logbook with a brand-new, empty Context().

Because native Zigbee groups operate via multicast RF frames to a 16-bit group address rather than calling Home Assistant services on individual member entities, the physical member devices update their state in Home Assistant asynchronously when their Report_Attributes frames arrive back at the coordinator (~50–500ms later). When the zha library processes those incoming attribute reports and fires the state update callback to the Home Assistant entity wrapper, Entity.async_write_ha_state() sees no active context on the member entity and generates an unattributed context.

This breaks:

  1. Activity / Logbook Attribution: Member lights/switches appear to have turned on or off by themselves (no user icon or automation link), even when toggled via a ZHA group in the UI or an automation.
  2. Manual/Physical Override Detection: Automations that inspect trigger.to_state.context.user_id or trigger.to_state.context.parent_id on individual lights to distinguish between HA-initiated changes and physical wall-switch presses falsely classify ZHA group commands as physical/external hardware changes.
Proposed Solution: Optimistic Context Seeding

Home Assistant's base Entity class already includes a built-in rolling TTL mechanism for pre-seeding context via Entity.async_set_context(context) (which remains valid for CONTEXT_RECENT_TIME_SECONDS = 5 seconds).

When a service call is executed on a ZHA group entity wrapper in homeassistant/components/zha/ (e.g., Light, Switch, Fan, Cover), the group entity wrapper can propagate its active self._context to the Home Assistant entities of its group members before (or immediately after) issuing the Zigbee multicast command:

  1. In the ZHA group entity service handler (or a helper method on the ZHA base entity / gateway proxy), identify the member entities belonging to the group (available via the underlying zha library group entity's group.members or member_entity_unique_ids).
  2. Look up the corresponding registered Home Assistant ZHAEntity instances via zha_gateway_proxy.
  3. Call member_ha_entity.async_set_context(self._context) on each member entity.
  4. When the physical devices respond with Report_Attributes within the 5-second window (or when the zha library performs its optimistic/post-command group member state sync), async_write_ha_state() on each member entity will automatically attach the pre-seeded Context.
Edge Cases & Considerations
  • Layer Boundaries (homeassistant/components/zha vs zigpy/zha): Because core.Context is a Home Assistant construct and does not exist inside the standalone zha library, this propagation can stay entirely within homeassistant/components/zha by resolving member entities in the HA component layer.
  • Already-Matching States: If a member bulb is already off when light.turn_off is called on the group, pre-seeding async_set_context is harmless—either no state write occurs, or context_recent_time naturally expires after 5 seconds.
  • Sub-second Race Conditions: Since almost all mains-powered Zigbee routers (bulbs, plugs, relays) report attribute changes well within the 5-second CONTEXT_RECENT_TIME_SECONDS window, standard async_set_context should cover both immediate zha group state updates and incoming Report_Attributes frames without needing custom timers.
Alternatives Considered
  • Using Home Assistant Light Groups (group integration) instead of ZHA Groups: Preserves Context because it issues sequential service calls over the HA event bus, but sacrifices Zigbee multicast, flooding the Zigbee mesh with unicast packets and causing the "popcorn effect".
  • **Tracking `Co
Lingua principale
Python
Stelle
96
Fork
84
Merge medio
3g 1h
PR unite (30g)
13

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.