Skip to content

Commit f9610c3

Browse files
committed
Avoid putting cluster handler info in base event
1 parent 79d847c commit f9610c3

File tree

1 file changed

+44
-33
lines changed

1 file changed

+44
-33
lines changed

zha/application/platforms/event.py

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
from typing import TYPE_CHECKING, Any
77

88
from zha.application import Platform
9-
from zha.application.platforms import EntityStateChangedEvent, PlatformEntity
9+
from zha.application.platforms import (
10+
EntityStateChangedEvent,
11+
PlatformEntity,
12+
BaseEntity,
13+
)
1014
from zha.application.registries import PLATFORM_ENTITIES
1115
from zha.const import STATE_CHANGED
1216
from zha.zigbee.cluster_handlers.const import (
@@ -35,8 +39,8 @@ class EventDeviceClass(StrEnum):
3539
MOTION = "motion"
3640

3741

38-
class BaseEvent(PlatformEntity):
39-
"""Base representation of a ZHA fan."""
42+
class BaseEvent(BaseEntity):
43+
"""Base representation of a ZHA events."""
4044

4145
PLATFORM = Platform.EVENT
4246

@@ -45,26 +49,6 @@ class BaseEvent(PlatformEntity):
4549
_attr_device_class: EventDeviceClass | None = None
4650
_attr_primary_weight = 10
4751
_state: dict[str, Any] = {}
48-
_cluster_handler_name: str
49-
_cluster_handler: ClusterHandler
50-
51-
def __init__(
52-
self,
53-
cluster_handlers: list[ClusterHandler],
54-
endpoint: Endpoint,
55-
device: Device,
56-
**kwargs,
57-
) -> None:
58-
"""Initialize the fan."""
59-
super().__init__(cluster_handlers, endpoint, device, **kwargs)
60-
self._cluster_handler = self.cluster_handlers[self._cluster_handler_name]
61-
62-
@property
63-
def state(self) -> dict[str, Any]:
64-
"""Return the state of the select."""
65-
response = super().state
66-
response.update(self._state)
67-
return response
6852

6953
@functools.cached_property
7054
def device_class(self) -> EventDeviceClass | None:
@@ -76,6 +60,41 @@ def event_types(self) -> list[str]:
7660
"""Return a list of possible events."""
7761
return self._attr_event_types
7862

63+
@property
64+
def state(self) -> dict[str, Any]:
65+
"""Return the state of the select."""
66+
response = super().state
67+
response.update(self._state)
68+
return response
69+
70+
def _trigger_event(self, event_type: str, event_attributes: dict[str, Any]) -> None:
71+
"""Handle state update from cluster handler."""
72+
if event_type not in self._attr_event_types:
73+
self.debug("Event type %s ignored", event_type)
74+
return
75+
76+
self._state = {
77+
"event_attributes": event_attributes,
78+
"event_type": event_type,
79+
}
80+
self.emit(STATE_CHANGED, EntityStateChangedEvent(**self.identifiers.__dict__))
81+
82+
83+
class ClusterHandlerEvent(BaseEvent, PlatformEntity):
84+
"""Base representation of a ZHA events using cluster handler."""
85+
86+
_cluster_handler_name: str
87+
_cluster_handler: ClusterHandler
88+
89+
def __init__(
90+
self,
91+
*args,
92+
**kwargs,
93+
) -> None:
94+
"""Initialize the fan."""
95+
super().__init__(*args, **kwargs)
96+
self._cluster_handler = self.cluster_handlers[self._cluster_handler_name]
97+
7998
def on_add(self) -> None:
8099
"""Run when entity is added."""
81100
super().on_add()
@@ -91,19 +110,11 @@ def handle_cluster_handler_command(
91110
event: ClusterHandlerCommandEvent,
92111
) -> None:
93112
"""Handle state update from cluster handler."""
94-
if event.event not in self._attr_event_types:
95-
self.debug("Event type %s ignored", event.event)
96-
return
97-
98-
self._state = {
99-
"event_attributes": event.data,
100-
"event_type": event.event,
101-
}
102-
self.emit(STATE_CHANGED, EntityStateChangedEvent(**self.identifiers.__dict__))
113+
self._trigger_event(event.event, event.data)
103114

104115

105116
@STRICT_MATCH(cluster_handler_names=CLIENT_CLUSTER_HANDLER_LEVEL)
106-
class LevelControlEvent(BaseEvent, PlatformEntity):
117+
class LevelControlEvent(ClusterHandlerEvent):
107118
"""Representation of a ZHA fan."""
108119

109120
_attr_translation_key = "level_control"

0 commit comments

Comments
 (0)