Skip to content

Commit

Permalink
add global listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
dmulcahey committed Mar 24, 2024
1 parent 5814fe3 commit 16f0fe5
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion zha/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self._listeners: dict[str, list[Callable]] = {}
self._event_tasks: list[asyncio.Task] = []
self._golbal_listeners: list[Callable] = []

def on_event( # pylint: disable=invalid-name
self, event_name: str, callback: Callable
Expand All @@ -34,6 +35,19 @@ def unsubscribe() -> None:

return unsubscribe

def on_all_events( # pylint: disable=invalid-name
self, callback: Callable
) -> Callable:
"""Register a callback for all events."""
self._golbal_listeners.append(callback)

def unsubscribe() -> None:
"""Unsubscribe listeners."""
if callback in self._golbal_listeners:
self._golbal_listeners.remove(callback)

return unsubscribe

def once(self, event_name: str, callback: Callable) -> Callable:
"""Listen for an event exactly once."""

Expand All @@ -47,7 +61,7 @@ def event_listener(data: dict) -> None:

def emit(self, event_name: str, data=None) -> None:
"""Run all callbacks for an event."""
for listener in self._listeners.get(event_name, []):
for listener in [*self._listeners.get(event_name, []), *self._golbal_listeners]:
if inspect.iscoroutinefunction(listener):
if data is None:
task = asyncio.create_task(listener())
Expand Down

0 comments on commit 16f0fe5

Please sign in to comment.