Introduces a simple, generic publish/subscribe event bus system.
This decouples hardware components (like the LED controller) from the core application logic, allowing for a more modular and extensible design.
This commit is contained in:
@@ -15,29 +15,28 @@ class EventBus:
|
|||||||
"""
|
"""
|
||||||
Subscribes a listener to a topic.
|
Subscribes a listener to a topic.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# _LOGGER.debug(f'EventBus subscribe {topic}')
|
|
||||||
|
|
||||||
if topic not in self.topics:
|
if topic not in self.topics:
|
||||||
self.topics[topic] = []
|
self.topics[topic] = []
|
||||||
self.topics[topic].append(listener)
|
self.topics[topic].append(listener)
|
||||||
|
|
||||||
def publish(self, topic: str, data: [dict, None]) -> None:
|
def publish(self, topic: str, data: Optional[Dict[str, Any]] = None) -> None:
|
||||||
"""
|
"""
|
||||||
Publishes an event to all subscribed listeners.
|
Publishes an event to all subscribed listeners.
|
||||||
"""
|
"""
|
||||||
|
if data is None:
|
||||||
|
data = {}
|
||||||
|
|
||||||
# _LOGGER.debug(f'EventBus publish {topic}')
|
|
||||||
|
|
||||||
data['__topic'] = topic
|
data['__topic'] = topic
|
||||||
|
|
||||||
listeners = self.topics.get(topic, [])
|
listeners = self.topics.get(topic, [])
|
||||||
for listener in listeners:
|
for listener in listeners:
|
||||||
listener(data)
|
try:
|
||||||
|
listener(data)
|
||||||
|
except Exception:
|
||||||
|
_LOGGER.exception("Error in event listener for topic %s", topic)
|
||||||
|
|
||||||
# Client helpers for subscriptions
|
# Client helpers for subscriptions
|
||||||
|
|
||||||
# The decorator to mark methods for subscription.
|
|
||||||
def subscribe(func: Callable) -> Callable:
|
def subscribe(func: Callable) -> Callable:
|
||||||
"""Decorator to mark a method for event bus subscription."""
|
"""Decorator to mark a method for event bus subscription."""
|
||||||
func._event_bus_subscribe = True
|
func._event_bus_subscribe = True
|
||||||
@@ -46,15 +45,10 @@ def subscribe(func: Callable) -> Callable:
|
|||||||
class EventHandler:
|
class EventHandler:
|
||||||
"""
|
"""
|
||||||
A base class for components that subscribe to events.
|
A base class for components that subscribe to events.
|
||||||
|
|
||||||
Subclasses should define event handlers as methods decorated with `@subscribe`.
|
|
||||||
The method name will automatically be used as the event topic.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, state: Any):
|
def __init__(self, state: Any):
|
||||||
self.state = state
|
self.state = state
|
||||||
self._subscribe_all_methods()
|
self._subscribe_all_methods()
|
||||||
_LOGGER.debug(f"EventHandler {self.__class__.__name__} has subscribed to all decorated methods.")
|
|
||||||
|
|
||||||
def _subscribe_all_methods(self):
|
def _subscribe_all_methods(self):
|
||||||
"""Finds and subscribes all methods decorated with @subscribe."""
|
"""Finds and subscribes all methods decorated with @subscribe."""
|
||||||
@@ -64,4 +58,4 @@ class EventHandler:
|
|||||||
if hasattr(method, '_event_bus_subscribe'):
|
if hasattr(method, '_event_bus_subscribe'):
|
||||||
# The topic is the name of the method itself.
|
# The topic is the name of the method itself.
|
||||||
self.state.event_bus.subscribe(method_name, method)
|
self.state.event_bus.subscribe(method_name, method)
|
||||||
_LOGGER.debug(f"Subscribed method '{method_name}' to topic '{method_name}'")
|
_LOGGER.debug(f"Subscribed method '{method_name}' to topic '{method_name}'")
|
||||||
|
|||||||
Reference in New Issue
Block a user