Propagate service call `Context` from ZHA group entities to member entities
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
Research direction
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.
Written by the indexing model from the issue text.
Description
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:
- 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.
- Manual/Physical Override Detection: Automations that inspect
trigger.to_state.context.user_idortrigger.to_state.context.parent_idon 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:
- 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
zhalibrary group entity'sgroup.membersormember_entity_unique_ids). - Look up the corresponding registered Home Assistant
ZHAEntityinstances viazha_gateway_proxy. - Call
member_ha_entity.async_set_context(self._context)on each member entity. - When the physical devices respond with
Report_Attributeswithin the 5-second window (or when thezhalibrary performs its optimistic/post-command group member state sync),async_write_ha_state()on each member entity will automatically attach the pre-seededContext.
Edge Cases & Considerations
- Layer Boundaries (
homeassistant/components/zhavszigpy/zha): Becausecore.Contextis a Home Assistant construct and does not exist inside the standalonezhalibrary, this propagation can stay entirely withinhomeassistant/components/zhaby resolving member entities in the HA component layer. - Already-Matching States: If a member bulb is already
offwhenlight.turn_offis called on the group, pre-seedingasync_set_contextis harmless—either no state write occurs, orcontext_recent_timenaturally 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_SECONDSwindow, standardasync_set_contextshould cover both immediatezhagroup state updates and incomingReport_Attributesframes without needing custom timers.
Alternatives Considered
- Using Home Assistant Light Groups (
groupintegration) instead of ZHA Groups: PreservesContextbecause 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
- 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
-
Each time a device becomes available again, ZHA adds another firmware update listener that is never releasedPossibly taken @zigpy-review-bot claimed this 1 day ago. Openbot: code audit
Difficulty 2/5 1-3 hours Newbie friendliness 90/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
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