Migrate event consumers from handle_<event> methods to typed on_event subscriptions
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 22/100
- Issue type
- Refactor
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- python
- Domain
- embedded-iot
Research direction
Start with EventBase and _handle_event_protocol in zha/event.py, then the event classes in zha/zigbee/device.py and zha/application/gateway.py, where the event_type and event keys disagree. The Home Assistant side is homeassistant/components/zha/helpers.py, where the two proxies register on_all_events. Done means the proxies use typed on_event subscriptions and the key convention is settled. Section 1 needs a maintainer decision on the key before the rest can proceed.
Written by the indexing model from the issue text.
Description
Follow-up to #923 and the discussion in home-assistant/core#184549. This tracks moving every consumer off the legacy handle_<event> protocol and onto typed on_event subscriptions, so the protocol can be removed afterwards.
Background
EventBase in zha/event.py offers two ways to consume events:
- Legacy protocol: the subscriber registers
on_all_events(self._handle_event_protocol)and receives every event the object emits._handle_event_protocolthen looks up a method namedhandle_<event.event>by name. Since #923, events without such a method are silently ignored instead of logging "Received unknown event". - Typed subscription (the new system):
on_event(<event name>, callback)registers one explicit callback for one event. Home Assistant already uses this for firmware version updates (DeviceFirmwareInfoUpdatedEvent), entity state (subscribe_state), event entities and infrared receivers.
In zha dev (0ea5e1ad) and HA Core dev, the only remaining users of the legacy protocol are Home Assistant's two ZHA proxy classes. zha itself only defines it (plus tests in tests/test_event.py). The handle_attribute_updated methods on zha entities are plain callbacks passed to cluster.on_event(...), so they are not affected.
1. zha: make the subscription key consistent per event class
The event_type and event fields of zha's event dataclasses don't mean the same thing across classes, so on_event(SomeEvent.event_type, ...) only works for some of them:
- Device events (
zha/zigbee/device.py:ZHAEvent,DeviceFirmwareInfoUpdatedEvent,DeviceEntityAddedEvent,DeviceEntityRemovedEvent,DeviceConfiguredEvent,ClusterBindEvent,ClusterConfigureReportingEvent):event_type == event, and they are emitted under that name. Both keys work. - Gateway events (
zha/application/gateway.py:ConnectionLostEvent,DeviceJoinedEvent,DeviceLeftEvent,RawDeviceInitializedEvent,DeviceFullInitEvent,DeviceRemovedEvent): every class hasevent_type = ZHA_GW_MSG("zha_gateway_message"), but each is emitted under itseventname (device_joined,device_left, ...). Nothing is ever emitted under"zha_gateway_message", sogateway.on_event(DeviceJoinedEvent.event_type, cb)subscribes successfully but never fires. GroupEventis one class for four events.eventis a per-instance field set inGateway._emit_group_gateway_message(group_added,group_removed,group_member_added,group_member_removed), so there is no class-level key to subscribe with. Subscribers have to use theZHA_GW_MSG_GROUP_*constants.- Entity events (
EntityStateChangedEvent,EntityEventTriggeredEvent,EntityInfraredSignalReceivedEvent):event_type = "entity", emitted underevent. HA subscribes with.event.
Tasks:
- Decide on one canonical subscription key per event class (e.g. a unique
event_typeper class that is also the emitted name, or alwaysevent), and apply it to the gateway and entity events. Check first whether anything depends on the currentevent_typevalues. - Split
GroupEventinto one class per event (or otherwise give each a class-level key).
HA can migrate (section 2) before this, by subscribing with the names that are actually emitted. This section is about making on_event(SomeEvent.<key>, ...) the one obvious pattern.
2. Home Assistant: migrate the ZHA proxies (homeassistant/components/zha/helpers.py)
ZHADeviceProxy registers self.device.on_all_events(self._handle_event_protocol) in __init__ and again in async_rebind_device. Its handlers:
-
handle_zha_event(ZHAEvent) -
handle_zha_cluster_bind(ClusterBindEvent) -
handle_zha_cluster_configure_reporting(ClusterConfigureReportingEvent) -
handle_zha_device_configured(DeviceConfiguredEvent) -
handle_zha_device_entity_added_event(DeviceEntityAddedEvent) -
handle_zha_device_entity_removed_event(DeviceEntityRemovedEvent)
ZHAGatewayProxy registers self.gateway.on_all_events(self._handle_event_protocol). Its handlers:
-
handle_connection_lost(ConnectionLostEvent) -
handle_device_joined(DeviceJoinedEvent) -
handle_raw_device_initialized(RawDeviceInitializedEvent) -
handle_device_fully_initialized(DeviceFullInitEvent) -
handle_device_left(DeviceLeftEvent) -
handle_device_removed(DeviceRemovedEvent) -
handle_group_added,handle_group_removed,handle_group_member_added,handle_group_member_removed(GroupEvent)
Both proxies subclass zha's EventBase only to inherit _handle_event_protocol. They never emit, and all of the handlers above are synchronous. After the migration they can stop subclassing EventBase.
3. zha: remove the legacy protocol
- Once a Home Assistant release no longer calls it, remove
EventBase._handle_event_protocolfromzha/event.py, along with its tests intests/test_event.py. - Re-evaluate whether the per-instance
eventfield is still needed once nothing dispatches on it. It depends on the key chosen in section 1.
Related, outside zha's EventBase
-
zigpy has its own copy of
EventBase(zigpy/event/event_base.py), and its_handle_event_protocolstill logs "Received unknown event". Nothing in zigpy, zha or HA's ZHA integration calls zigpy's copy (zha and HA usezha.event.EventBase), so it can be removed there too. -
One layer down, zha still consumes zigpy through the name-dispatched
ListenableMixin.listener_event/add_listenerpattern:- the gateway is registered as a listener on the application controller and on its groups (
connection_lost,device_joined,raw_device_initialized,device_initialized,device_reinterviewed,device_left,device_removed,group_*,handle_message); cluster_commandon the virtual entities and the IAS ACE alarm control panel;- quirks'
zha_send_event, received by_ClusterEventForwarderinzha/zigbee/endpoint.py.
So far zigpy emits typed events only for ZCL attribute events, the OTA query cache and image-available events, and Green Power commands, so moving these needs new typed events in zigpy first. That may deserve its own zigpy issue.
- the gateway is registered as a listener on the application controller and on its groups (
- 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