Skip to content

Commit 31f527d

Browse files
authored
Merge pull request #2356 from AppDaemon:custom-events
fixed match/case structure in fire_event
2 parents c2b0785 + 53145b8 commit 31f527d

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

appdaemon/events.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ async def info_event_callback(self, name: str, handle: str):
171171
else:
172172
raise ValueError(f"Invalid handle: {handle}")
173173

174-
async def fire_event(self, namespace: str, event: str, **kwargs):
174+
async def fire_event(self, namespace: str, event: str, **kwargs: Any) -> dict[str, Any] | None:
175175
"""Fires an event.
176176
177177
If the namespace does not have a plugin associated with it, the event will be fired locally.
@@ -190,15 +190,18 @@ async def fire_event(self, namespace: str, event: str, **kwargs):
190190
"""
191191

192192
self.logger.debug("fire_plugin_event() %s %s %s", namespace, event, kwargs)
193-
plugin = self.AD.plugins.get_plugin_object(namespace)
194-
match plugin:
195-
case PluginBase() as plugin:
196-
if hasattr(plugin, "fire_plugin_event"):
197-
# We assume that the event will come back to us via the plugin
198-
return await plugin.fire_plugin_event(event, namespace, **kwargs)
199-
else:
200-
# Just fire the event locally
201-
await self.AD.events.process_event(namespace, {"event_type": event, "data": kwargs})
193+
match self.AD.plugins.get_plugin_object(namespace):
194+
case PluginBase() as plugin if hasattr(plugin, "fire_plugin_event"):
195+
# In the case that the namespace has a PluginBase associated (both Hass and MQTT plugins do), we check
196+
# that the plugin actually has a method called `fire_plugin_event`. If both of these conditions are met,
197+
# we call that method to fire the event, and assume that the plugin when the plugin fires the event, it
198+
# will make it back to AppDaemon via the normal plugin event processing mechanism.
199+
return await plugin.fire_plugin_event(event, namespace, **kwargs)
200+
case _:
201+
# If anything else comes out of get_plugin_object, we assume that the namespace does not have a plugin
202+
# associated with it, and we can fire the event locally.
203+
# This is the case for the admin namespace, and any other namespaces that do not have a plugin.
204+
return await self.AD.events.process_event(namespace, {"event_type": event, "data": kwargs})
202205

203206
async def process_event(self, namespace: str, data: dict[str, Any]):
204207
"""Processes an event that has been received either locally or from a plugin.

0 commit comments

Comments
 (0)