Skip to content

Commit 412b4cd

Browse files
committed
Update README.md to document creating custom EventListeners
1 parent c17bd6c commit 412b4cd

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,69 @@ And a command like the following is called by the Stream Deck software:
284284
streamdeck -port 28196 -pluginUUID 63831042F4048F072B096732E0385245 -registerEvent registerPlugin -info '{"application": {...}, "plugin": {"uuid": "my-plugin-name", "version": "1.1.3"}, ...}'
285285
```
286286
287+
## Custom Event Listeners
288+
289+
The SDK allows you to create custom event listeners and events by extending the `EventListener` and `EventBase` classes. This is useful when you need to monitor data from external applications and perform specific actions in response to changes or alerts.
290+
291+
### Creating a Custom Event Listener
292+
293+
To create a custom event listener:
294+
295+
1. Create new event model that inherits from `EventBase`.
296+
2. Create a new class that inherits from `EventListener`.
297+
a. Implement the required `listen` and `stop` methods. The `listen` method should yield results as a json string that matches the new event model.
298+
b. List the new event classes in the `event_models` class variable of the new `EventListener` class.
299+
3. Configure your plugin in its `pyproject.toml` file to use your custom listener.
300+
301+
```python
302+
# custom_listener.py
303+
from collections.abc import Generator
304+
from typing import ClassVar, Literal
305+
306+
from streamdeck.event_listener import EventListener
307+
from streamdeck.models.events import EventBase
308+
309+
310+
class MyCustomEvent(EventBase):
311+
event: Literal["somethingHappened"]
312+
... # Define additional data attributes here
313+
314+
class MyCustomEventListener(EventListener):
315+
def listen(self) -> Generator[str | bytes, None, None]:
316+
...
317+
# Listen/poll for something here in a loop, and yield the result.
318+
# This will be ran in a background thread.
319+
# Ex:
320+
# while self._running is True:
321+
# result = module.check_status()
322+
# if result is not None:
323+
# yield json.dumps({"event": "somethingHappend", "result": result})
324+
# time.sleep(1)
325+
326+
def stop(self) -> None:
327+
...
328+
# Stop the loop or blocking call in the listen method.
329+
# Ex:
330+
# self._running = False
331+
```
332+
333+
### Configuring Your Custom Listener
334+
335+
To use your custom event listener, add it to your `pyproject.toml` file:
336+
337+
```toml
338+
[tools.streamdeck]
339+
action_scripts = [
340+
"main.py",
341+
]
342+
event_listener_modules = [
343+
"myplugin.custom_listener",
344+
]
345+
```
346+
347+
The `event_listeners` list should contain strings in module format for each module you want to use.
348+
349+
287350
## Creating and Packaging Plugins
288351
289352
To create a new plugin with all of the necessary files to start from and package it for use on your Stream Deck, use [the Python SDK CLI tool](https://github.com/strohganoff/python-streamdeck-plugin-sdk-cli).

0 commit comments

Comments
 (0)