Migrate event consumers from handle_<event> methods to typed on_event subscriptions
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
- 22/100
- Tipo di issue
- Refactoring
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Stack tecnologico
- python
- Ambito
- embedded-iot
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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 (
- 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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di zigpy/zha
-
Device override creates the generic entity instead of the vendor-specific one (Light instead of HueLight, ForceOnLight, MinTransitionLight)Forse già presa @zigpy-review-bot l’ha presa oggi. Apertabot: code audit
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
I maintainer di solito rispondono entro 1 giorno
-
Each time a device becomes available again, ZHA adds another firmware update listener that is never releasedForse già presa @zigpy-review-bot l’ha presa 1 giorno fa. Apertabot: code audit
Difficoltà 2/5 1-3 ore Idoneità per principianti 90/100
I maintainer di solito rispondono entro 1 giorno
-
Devices that left the network are re-initialized on every availability check, reading attributes from a device that is goneForse già presa @zigpy-review-bot l’ha presa 1 giorno fa. Apertabot: code audit
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
I maintainer di solito rispondono entro 1 giorno
-
device specific quirks
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
pimutils/vdirsyncer#1242 ·
-
priority:P2
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
QuantConnect/lean-cli#792 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
open-webui/open-webui#35360 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
-
Suggest link: Destiny Verify (MIT zero-dependency pre-commit security, citation & link hygiene CLI)Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 68/100