Skip to content

Commit 72c526a

Browse files
committed
Remove 'Event' suffix that was on some event class names. Create mixin classes for event models that have action-specific or device-specific attributes
1 parent 3dd0955 commit 72c526a

File tree

7 files changed

+73
-151
lines changed

7 files changed

+73
-151
lines changed

streamdeck/manager.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from streamdeck.actions import ActionRegistry
88
from streamdeck.command_sender import StreamDeckCommandSender
9-
from streamdeck.models.events import event_adapter
9+
from streamdeck.models.events import ContextualEventMixin, event_adapter
1010
from streamdeck.utils.logging import configure_streamdeck_logger
1111
from streamdeck.websocket import WebSocketClient
1212

@@ -83,7 +83,7 @@ def run(self) -> None:
8383
logger.debug("Event received: %s", data.event)
8484

8585
# If the event is action-specific, we'll pass the action's uuid to the handler to ensure only the correct action is triggered.
86-
event_action_uuid: str | None = cast(str, data.action) if data.is_action_specific() else None
86+
event_action_uuid = data.action if isinstance(data, ContextualEventMixin) else None
8787

8888
for handler in self._registry.get_action_handlers(event_name=data.event, event_action_uuid=event_action_uuid):
8989
# TODO: from contextual event occurences, save metadata to the action's properties.

streamdeck/models/events.py

+47-128
Original file line numberDiff line numberDiff line change
@@ -16,154 +16,94 @@ class EventBase(BaseModel, ABC):
1616
event: str
1717
"""Name of the event used to identify what occurred."""
1818

19-
@classmethod
20-
def is_action_specific(cls) -> bool:
21-
"""Check if the event is specific to an action instance (i.e. the event has an "action" field)."""
22-
return "action" in cls.model_fields
2319

24-
@classmethod
25-
def is_device_specific(cls) -> bool:
26-
"""Check if the event is specific to a device instance (i.e. the event has a "device" field)."""
27-
return "device" in cls.model_fields
20+
class ContextualEventMixin:
21+
action: str
22+
"""Unique identifier of the action"""
23+
context: str
24+
"""Identifies the instance of an action that caused the event, i.e. the specific key or dial."""
2825

29-
@classmethod
30-
def is_action_instance_specific(cls) -> bool:
31-
"""Check if the event is specific to an action instance (i.e. the event has a "context" field)."""
32-
return "context" in cls.model_fields
26+
class DeviceSpecificEventMixin:
27+
device: str
28+
"""Unique identifier of the Stream Deck device that this event is associated with."""
3329

3430

35-
class ApplicationDidLaunchEvent(EventBase):
31+
class ApplicationDidLaunch(EventBase):
3632
event: Literal["applicationDidLaunch"]
3733
payload: dict[Literal["application"], str]
3834
"""Payload containing the name of the application that triggered the event."""
3935

4036

41-
class ApplicationDidTerminateEvent(EventBase):
37+
class ApplicationDidTerminate(EventBase):
4238
event: Literal["applicationDidTerminate"]
4339
payload: dict[Literal["application"], str]
4440
"""Payload containing the name of the application that triggered the event."""
4541

4642

47-
class DeviceDidConnectEvent(EventBase):
43+
class DeviceDidConnect(EventBase, DeviceSpecificEventMixin):
4844
event: Literal["deviceDidConnect"]
49-
device: str
50-
"""Unique identifier of the Stream Deck device that this event is associated with."""
5145
deviceInfo: dict[str, Any]
5246
"""Information about the newly connected device."""
5347

5448

55-
class DeviceDidDisconnectEvent(EventBase):
49+
class DeviceDidDisconnect(EventBase, DeviceSpecificEventMixin):
5650
event: Literal["deviceDidDisconnect"]
57-
device: str
58-
"""Unique identifier of the Stream Deck device that this event is associated with."""
5951

6052

61-
class DialDownEvent(EventBase):
53+
class DialDown(EventBase, ContextualEventMixin, DeviceSpecificEventMixin):
6254
event: Literal["dialDown"]
63-
context: str
64-
"""Identifies the instance of an action that caused the event, i.e. the specific key or dial."""
65-
device: str
66-
"""Unique identifier of the Stream Deck device that this event is associated with."""
67-
action: str
68-
"""Unique identifier of the action"""
6955
payload: dict[str, Any]
7056

7157

72-
class DialRotateEvent(EventBase):
58+
class DialRotate(EventBase, ContextualEventMixin, DeviceSpecificEventMixin):
7359
event: Literal["dialRotate"]
74-
context: str
75-
"""Identifies the instance of an action that caused the event, i.e. the specific key or dial."""
76-
device: str
77-
"""Unique identifier of the Stream Deck device that this event is associated with."""
78-
action: str
79-
"""Unique identifier of the action"""
8060
payload: dict[str, Any]
8161

8262

83-
class DialUpEvent(EventBase):
63+
class DialUp(EventBase, ContextualEventMixin, DeviceSpecificEventMixin):
8464
event: Literal["dialUp"]
85-
context: str
86-
"""Identifies the instance of an action that caused the event, i.e. the specific key or dial."""
87-
device: str
88-
"""Unique identifier of the Stream Deck device that this event is associated with."""
89-
action: str
90-
"""Unique identifier of the action"""
9165
payload: dict[str, Any]
9266

9367

94-
class DidReceiveDeepLinkEvent(EventBase):
68+
class DidReceiveDeepLink(EventBase):
9569
event: Literal["didReceiveDeepLink"]
9670
payload: dict[Literal["url"], str]
9771

9872

99-
class DidReceiveGlobalSettingsEvent(EventBase):
73+
class DidReceiveGlobalSettings(EventBase):
10074
event: Literal["didReceiveGlobalSettings"]
10175
payload: dict[Literal["settings"], dict[str, Any]]
10276

10377

104-
class DidReceivePropertyInspectorMessageEvent(EventBase):
78+
class DidReceivePropertyInspectorMessage(EventBase, ContextualEventMixin):
10579
event: Literal["sendToPlugin"]
106-
context: str
107-
"""Identifies the instance of an action that caused the event, i.e. the specific key or dial."""
108-
action: str
109-
"""Unique identifier of the action"""
11080
payload: dict[str, Any]
11181

11282

113-
class DidReceiveSettingsEvent(EventBase):
83+
class DidReceiveSettings(EventBase, ContextualEventMixin, DeviceSpecificEventMixin):
11484
event: Literal["didReceiveSettings"]
115-
context: str
116-
"""UUID of the instance of an action that caused the event."""
117-
device: str
118-
"""UUID of the Stream Deck device that this event is associated with."""
119-
action: str
120-
"""UUID of the action."""
12185
payload: dict[str, Any]
12286

12387

124-
class KeyDownEvent(EventBase):
88+
class KeyDown(EventBase, ContextualEventMixin, DeviceSpecificEventMixin):
12589
event: Literal["keyDown"]
126-
context: str
127-
"""Identifies the instance of an action that caused the event, i.e. the specific key or dial."""
128-
device: str
129-
"""Unique identifier of the Stream Deck device that this event is associated with."""
130-
action: str
131-
"""Unique identifier of the action"""
13290
payload: dict[str, Any]
13391

13492

135-
class KeyUpEvent(EventBase):
93+
class KeyUp(EventBase, ContextualEventMixin, DeviceSpecificEventMixin):
13694
event: Literal["keyUp"]
137-
context: str
138-
"""Identifies the instance of an action that caused the event, i.e. the specific key or dial."""
139-
device: str
140-
"""Unique identifier of the Stream Deck device that this event is associated with."""
141-
action: str
142-
"""Unique identifier of the action"""
14395
payload: dict[str, Any]
14496

14597

146-
class PropertyInspectorDidAppearEvent(EventBase):
98+
class PropertyInspectorDidAppear(EventBase, ContextualEventMixin, DeviceSpecificEventMixin):
14799
event: Literal["propertyInspectorDidAppear"]
148-
context: str
149-
"""Identifies the instance of an action that caused the event, i.e. the specific key or dial."""
150-
device: str
151-
"""Unique identifier of the Stream Deck device that this event is associated with."""
152-
action: str
153-
"""Unique identifier of the action"""
154100

155101

156-
class PropertyInspectorDidDisappearEvent(EventBase):
102+
class PropertyInspectorDidDisappear(EventBase, ContextualEventMixin, DeviceSpecificEventMixin):
157103
event: Literal["propertyInspectorDidDisappear"]
158-
context: str
159-
"""Identifies the instance of an action that caused the event, i.e. the specific key or dial."""
160-
device: str
161-
"""Unique identifier of the Stream Deck device that this event is associated with."""
162-
action: str
163-
"""Unique identifier of the action"""
164104

165105

166-
class SystemDidWakeUpEvent(EventBase):
106+
class SystemDidWakeUp(EventBase):
167107
event: Literal["systemDidWakeUp"]
168108

169109

@@ -186,46 +126,25 @@ class TitleParametersDidChangePayload(TypedDict):
186126
titleParameters: TitleParametersDict
187127

188128

189-
class TitleParametersDidChangeEvent(EventBase):
129+
class TitleParametersDidChange(EventBase, DeviceSpecificEventMixin):
190130
event: Literal["titleParametersDidChange"]
191131
context: str
192132
"""Identifies the instance of an action that caused the event, i.e. the specific key or dial."""
193-
device: str
194-
"""Unique identifier of the Stream Deck device that this event is associated with."""
195-
# payload: dict[str, Any]
196133
payload: TitleParametersDidChangePayload
197134

198135

199-
class TouchTap(EventBase):
136+
class TouchTap(EventBase, ContextualEventMixin, DeviceSpecificEventMixin):
200137
event: Literal["touchTap"]
201-
context: str
202-
"""Identifies the instance of an action that caused the event, i.e. the specific key or dial."""
203-
device: str
204-
"""Unique identifier of the Stream Deck device that this event is associated with."""
205-
action: str
206-
"""Unique identifier of the action"""
207138
payload: dict[str, Any]
208139

209140

210-
class WillAppearEvent(EventBase):
141+
class WillAppear(EventBase, ContextualEventMixin, DeviceSpecificEventMixin):
211142
event: Literal["willAppear"]
212-
context: str
213-
"""Identifies the instance of an action that caused the event, i.e. the specific key or dial."""
214-
device: str
215-
"""Unique identifier of the Stream Deck device that this event is associated with."""
216-
action: str
217-
"""Unique identifier of the action"""
218143
payload: dict[str, Any]
219144

220145

221-
class WillDisappearEvent(EventBase):
146+
class WillDisappear(EventBase, ContextualEventMixin, DeviceSpecificEventMixin):
222147
event: Literal["willDisappear"]
223-
context: str
224-
"""Identifies the instance of an action that caused the event, i.e. the specific key or dial."""
225-
device: str
226-
"""Unique identifier of the Stream Deck device that this event is associated with."""
227-
action: str
228-
"""Unique identifier of the action"""
229148
payload: dict[str, Any]
230149

231150

@@ -234,26 +153,26 @@ class WillDisappearEvent(EventBase):
234153
event_adapter: TypeAdapter[EventBase] = TypeAdapter(
235154
Annotated[
236155
Union[ # noqa: UP007
237-
ApplicationDidLaunchEvent,
238-
ApplicationDidTerminateEvent,
239-
DeviceDidConnectEvent,
240-
DeviceDidDisconnectEvent,
241-
DialDownEvent,
242-
DialRotateEvent,
243-
DialUpEvent,
244-
DidReceiveDeepLinkEvent,
245-
KeyUpEvent,
246-
KeyDownEvent,
247-
DidReceivePropertyInspectorMessageEvent,
248-
PropertyInspectorDidAppearEvent,
249-
PropertyInspectorDidDisappearEvent,
250-
DidReceiveGlobalSettingsEvent,
251-
DidReceiveSettingsEvent,
252-
SystemDidWakeUpEvent,
253-
TitleParametersDidChangeEvent,
156+
ApplicationDidLaunch,
157+
ApplicationDidTerminate,
158+
DeviceDidConnect,
159+
DeviceDidDisconnect,
160+
DialDown,
161+
DialRotate,
162+
DialUp,
163+
DidReceiveDeepLink,
164+
KeyUp,
165+
KeyDown,
166+
DidReceivePropertyInspectorMessage,
167+
PropertyInspectorDidAppear,
168+
PropertyInspectorDidDisappear,
169+
DidReceiveGlobalSettings,
170+
DidReceiveSettings,
171+
SystemDidWakeUp,
172+
TitleParametersDidChange,
254173
TouchTap,
255-
WillAppearEvent,
256-
WillDisappearEvent,
174+
WillAppear,
175+
WillDisappear,
257176
],
258177
Field(discriminator="event")
259178
]

tests/actions/test_action_registry.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
from streamdeck.models import events
77

88

9-
class DialUpEventFactory(ModelFactory[events.DialUpEvent]):
9+
class DialUpEventFactory(ModelFactory[events.DialUp]):
1010
"""Polyfactory factory for creating a fake dialUp event message based on our Pydantic model."""
1111

12-
class DialDownEventFactory(ModelFactory[events.DialDownEvent]):
12+
class DialDownEventFactory(ModelFactory[events.DialDown]):
1313
"""Polyfactory factory for creating a fake dialDown event message based on our Pydantic model."""
1414

15-
class KeyUpEventFactory(ModelFactory[events.KeyUpEvent]):
15+
class KeyUpEventFactory(ModelFactory[events.KeyUp]):
1616
"""Polyfactory factory for creating a fake keyUp event message based on our Pydantic model."""
1717

1818

@@ -37,7 +37,7 @@ def test_get_action_handlers_no_handlers():
3737

3838
registry.register(action)
3939

40-
fake_event_data: events.DialUpEvent = DialUpEventFactory.build()
40+
fake_event_data: events.DialUp = DialUpEventFactory.build()
4141
handlers = list(registry.get_action_handlers(event_name=fake_event_data.event, event_action_uuid=fake_event_data.action))
4242
assert len(handlers) == 0
4343

@@ -53,7 +53,7 @@ def dial_down_handler(event: events.EventBase):
5353

5454
registry.register(action)
5555

56-
fake_event_data: events.DialDownEvent = DialDownEventFactory.build(action=action.uuid)
56+
fake_event_data: events.DialDown = DialDownEventFactory.build(action=action.uuid)
5757
handlers = list(registry.get_action_handlers(event_name=fake_event_data.event, event_action_uuid=fake_event_data.action))
5858
# handlers = list(registry.get_action_handlers("dialDown"))
5959
assert len(handlers) == 1
@@ -78,7 +78,7 @@ def key_up_handler2(event):
7878
registry.register(action1)
7979
registry.register(action2)
8080

81-
fake_event_data: events.KeyUpEvent = KeyUpEventFactory.build(action=action1.uuid)
81+
fake_event_data: events.KeyUp = KeyUpEventFactory.build(action=action1.uuid)
8282
# Notice no action uuid is passed in here, so we should get all handlers for the event.
8383
handlers = list(registry.get_action_handlers(event_name=fake_event_data.event))
8484

0 commit comments

Comments
 (0)