[WIP] Events, LED#3
Conversation
add python-mpv dependency to .venv
|
Completely outsider that just landed on the project... I think the bus idea is great and can be used for a lot of things! I see there is another pr with thinking sound, etc that too can go through the bus... It would be awesome if this bus can be accessed externally... kind of the way rhasspy does it... but maybe not, meaning exposing a server to where several clients can connect and read events from the bus... so that we can have a 2mics led client, a 4mics led client but also a thinking_sound client... etc... but this channel can also be bidirectional, so a push_button client can also be implemented to trigger the listening... Don't know... my two cents... for now I'm focussed on build the bare project onto my buildroot image :) |
| def subscribe(func: Callable) -> Callable: | ||
| """Decorator to mark a method for event bus subscription.""" | ||
| func._event_bus_subscribe = True | ||
| return func |
There was a problem hiding this comment.
[Suggestion] Configurable handler metadata:
| def subscribe(func: Callable) -> Callable: | |
| """Decorator to mark a method for event bus subscription.""" | |
| func._event_bus_subscribe = True | |
| return func | |
| @dataclass | |
| class EventBusMetaData: | |
| priority: int = 0 | |
| # Any other info related to the handler | |
| def subscribe(priority: int=0, **kwargs): | |
| """Decorator to mark a method for event bus subscription.""" | |
| def wrapper(func: Callable) -> Callable: | |
| """Function wrapper.""" | |
| func._event_bus_subscribe = True | |
| func._event_bus_meta = EventBusMetaData( | |
| priority=priority, | |
| ... | |
| ) | |
| return func | |
| return wrapper |
There was a problem hiding this comment.
[Nitpick] An event bus is based off events - not topics, it would scale far more easily to have event handlers react to VoiceAssistantEvents
| def publish(self, topic: str, data: [dict, None]) -> None: | ||
| """ | ||
| Publishes an event to all subscribed listeners. | ||
| """ | ||
|
|
||
| # _LOGGER.debug(f'EventBus publish {topic}') | ||
|
|
||
| data['__topic'] = topic | ||
|
|
||
| listeners = self.topics.get(topic, []) | ||
| for listener in listeners: | ||
| listener(data) |
There was a problem hiding this comment.
[Suggestion] Based on below suggestion:
| def publish(self, topic: str, data: [dict, None]) -> None: | |
| """ | |
| Publishes an event to all subscribed listeners. | |
| """ | |
| # _LOGGER.debug(f'EventBus publish {topic}') | |
| data['__topic'] = topic | |
| listeners = self.topics.get(topic, []) | |
| for listener in listeners: | |
| listener(data) | |
| async def publish(self, topic: str, data: [dict, None]) -> None: | |
| """ | |
| Publishes an event to all subscribed listeners. | |
| """ | |
| # _LOGGER.debug(f'EventBus publish {topic}') | |
| data['__topic'] = topic | |
| listeners = self.topics.get(topic, []) | |
| for listener in sorted(listeners, key=lambda l: l._event_bus_priority): | |
| if inspect.iscoroutine(listener): | |
| await listener(data) | |
| else: | |
| listener(data) |
When publishing events, use asyncio.create_task to publish the event.
|
Can you specify if this is for the v1 or the v2 of the 2MicHat? |
|
We have discussions now. It would be great, if you can add one for the LED part and hope you give some input from what you already did, maybe future ideas etc. |
|
Hello, we have a plan now for exposing events via api to use with any external script that can work with leds&buttons for such custom boards and we have took some examples for different boards (including this one for respeaker 2mic pi hat), check this PR we're working on now and let us know if you have any notes/suggestions regarding that: #266 |
This is a work in progress to add features for LED and button(s) on the 2mic pi hat. This PR will also include some other house keeping and general quality of life concerns
Completed
Todo
events_led.pycode so it will also work with the 4mic. The code can be abstracted to work with any LED/light via pub/sub.I would love comments on this PR, and also some guidance/links to working with the ESPhome package and API.