Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 28 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,19 @@ def handle_will_appear(event_data):

A handler must have a required positional parameter named `event_data` first; the `.on()` decorator checks this when applied. At startup, before opening the WebSocket, the manager checks that any other required parameters have a stored injectable value. The SDK provides `command_sender`; plugins can define additional injected parameters in `pyproject.toml`. The SDK passes injectable values by keyword, so declare them as positional-or-keyword or keyword-only.

Handlers may be synchronous or asynchronous. Synchronous handlers run directly
on the event loop, so keep them brief and non-blocking. Use an async handler
when it needs to await I/O or send commands:

```python
from streamdeck.command_sender import StreamDeckCommandSender


@my_action.on("willAppear")
def handle_will_appear(event_data, command_sender: StreamDeckCommandSender):
async def handle_will_appear(event_data, command_sender: StreamDeckCommandSender):
# Use command_sender to interact with Stream Deck
command_sender.set_title(context=event_data.context, title="Hello!")
command_sender.set_state(context=event_data.context, state=0)
await command_sender.set_title(context=event_data.context, title="Hello!")
await command_sender.set_state(context=event_data.context, state=0)
```

The `command_sender` parameter is optional. If included in the handler's signature, the SDK automatically injects a `StreamDeckCommandSender` instance that can be used to send commands back to the Stream Deck (like setting titles, states, or images). Startup raises `TypeError` for signatures whose required parameters cannot be supplied by dispatch.
Expand Down Expand Up @@ -237,6 +241,10 @@ Use `streamdeck --help` without launch arguments to see the available options. H

The CLI entry point `streamdeck.__main__.main(argv=None)` accepts an optional argument list, excluding the executable name. Both settings models use that same list; omitting it uses the process arguments. Startup prepares imports, logging, and optional debugging before loading plugin settings, then calls `run_plugin(launch, config)` with both validated models. Startup and runtime failures are logged and re-raised.

The console command owns the event loop. Code that calls `PluginManager.run()`
directly must await it, and calls to `StreamDeckCommandSender` methods must also
be awaited.

In addition to these, there is an additional option to use **debug mode**, which is discussed below.

#### Debugging
Expand Down Expand Up @@ -366,8 +374,9 @@ To create a custom event listener:

```python
# custom_listener.py
from collections.abc import Generator
from typing import ClassVar, Literal
import json
from collections.abc import AsyncIterator
from typing import Literal

from streamdeck.event_listener import EventListener
from streamdeck.models.events import EventBase
Expand All @@ -381,25 +390,22 @@ class MyCustomEvent(EventBase[Literal["somethingHappened"]]):

class MyCustomEventListener(EventListener):
event_models = [MyCustomEvent]

def listen(self) -> Generator[str | bytes, None, None]:
...
# Listen/poll for something here in a loop, and yield the result.
# This will be ran in a background thread.
# Ex:
# while self._running is True:
# result = module.check_status()
# if result is not None:
# yield json.dumps({"event": "somethingHappened", "result": result})
# time.sleep(1)

def stop(self) -> None:
...
# Stop the loop or blocking call in the listen method.
# Ex:
# self._running = False

async def listen(self) -> AsyncIterator[str | bytes]:
# Replace wait_for_status() with cancellable async I/O for your source.
while True:
result = await wait_for_status()
yield json.dumps({"event": "somethingHappened", "result": result})

async def stop(self) -> None:
# Close any subscription or other resources owned by this listener.
await close_status_subscription()
```

Listeners run as asyncio tasks, so their I/O must not block the event loop.
`stop()` should be safe after partial startup and release resources even when
`listen()` is waiting for input.

### Configuring Your Custom Listener

To use your custom event listener, add it to your `pyproject.toml` file:
Expand Down Expand Up @@ -433,9 +439,6 @@ The following upcoming improvements are in the works:
- **Improved Documentation**: Expand and improve the documentation with more examples, guides, and use cases.
- **Store & Bind Settings**: Automatically store and bind action instance context-holding objects to handler function arguments if they are included in the definition.
- **Optional Event Pattern Matching on Hooks**: Add support for optional pattern-matching on event messages to further filter when hooks get called.
- **Async Support**: Introduce asynchronous features to handle WebSocket communication more efficiently.


## License

This project is licensed under the MIT License.
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@
"platformdirs >= 4.3.6",
"pydantic >= 2.11",
"pydantic_core >= 2.33",
"websockets >= 13.1",
"websockets >= 16.1",
"pydantic-settings[toml] >= 2.11",
"typing-extensions >= 4.14.0",
"exceptiongroup >= 1.3.1",
]

[project.urls]
Expand Down
12 changes: 9 additions & 3 deletions streamdeck/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import asyncio
import logging
import sys

Expand All @@ -24,6 +25,11 @@

def main(argv: list[str] | None = None) -> None:
"""Console entry point for the Pydantic Settings CLI."""
asyncio.run(async_main(argv))


async def async_main(argv: list[str] | None = None) -> None:
"""Parse launch configuration and run the plugin inside the event loop."""
args = sys.argv[1:] if argv is None else list(argv)
try:
launch = CliApp.run(LaunchArguments, cli_args=args)
Expand All @@ -44,7 +50,7 @@ def main(argv: list[str] | None = None) -> None:
setup_debug_mode(launch.debug_port)

config = CliApp.run(StreamDeckToolConfig, cli_args=args)
run_plugin(launch, config)
await run_plugin(launch, config)
except Exception:
logger.exception("Plugin startup or execution failed")
raise
Expand All @@ -60,7 +66,7 @@ def setup_debug_mode(debug_port: int) -> None:
logger.info("Debugger attached.")


def run_plugin(launch: LaunchArguments, config: StreamDeckToolConfig) -> None:
async def run_plugin(launch: LaunchArguments, config: StreamDeckToolConfig) -> None:
"""Register and run the plugin using validated launch and plugin settings."""
manager = PluginManager(
port=launch.port,
Expand Down Expand Up @@ -92,7 +98,7 @@ def run_plugin(launch: LaunchArguments, config: StreamDeckToolConfig) -> None:
manager.register_action(action)

logger.info("Connecting to Stream Deck on port %d", launch.port)
manager.run()
await manager.run()


# Also run the plugin if this script is ran as a console script.
Expand Down
Loading
Loading