added 10s status broadcast interval
This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
# streamdeckapi
|
# streamdeckapi
|
||||||
Stream Deck API Library for Home Assistant Stream Deck Integration
|
Stream Deck API Library for Home Assistant Stream Deck Integration
|
||||||
|
|
||||||
Only compatible with separate [Stream Deck Plugin](https://github.com/Patrick762/streamdeckapi-plugin)
|
Only compatible with separate [Stream Deck Plugin](https://github.com/Patrick762/streamdeckapi-plugin) or the bundled server.
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
- Python 3.10 or higher
|
- Python 3.10 or higher
|
||||||
|
|||||||
@@ -319,6 +319,30 @@ async def websocket_broadcast(message: str):
|
|||||||
await connection.send_str(message)
|
await connection.send_str(message)
|
||||||
|
|
||||||
|
|
||||||
|
async def broadcast_status():
|
||||||
|
"""Broadcast the current status of the streamdeck."""
|
||||||
|
|
||||||
|
# Collect data
|
||||||
|
data = {
|
||||||
|
"event": "status",
|
||||||
|
"args": {
|
||||||
|
"devices": devices,
|
||||||
|
"application": application,
|
||||||
|
"buttons": get_buttons()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data_str = encode(data, unpicklable=False)
|
||||||
|
data_str = (
|
||||||
|
data_str.replace('"x_pos"', '"x"')
|
||||||
|
.replace('"y_pos"', '"y"')
|
||||||
|
.replace('"platform_version"', '"platformVersion"')
|
||||||
|
)
|
||||||
|
|
||||||
|
# Broadcast
|
||||||
|
await websocket_broadcast(data_str)
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Functions
|
# Functions
|
||||||
#
|
#
|
||||||
@@ -346,6 +370,8 @@ async def start_server_async(host: str = "0.0.0.0", port: int = PLUGIN_PORT):
|
|||||||
await site.start()
|
await site.start()
|
||||||
print("Started Stream Deck API server on port", PLUGIN_PORT)
|
print("Started Stream Deck API server on port", PLUGIN_PORT)
|
||||||
|
|
||||||
|
Timer(10, broadcast_status)
|
||||||
|
|
||||||
|
|
||||||
def get_position(deck: StreamDeck, key: int) -> SDButtonPosition:
|
def get_position(deck: StreamDeck, key: int) -> SDButtonPosition:
|
||||||
"""Get the position of a key."""
|
"""Get the position of a key."""
|
||||||
@@ -479,6 +505,24 @@ def start_ssdp_server():
|
|||||||
server.serve_forever()
|
server.serve_forever()
|
||||||
|
|
||||||
|
|
||||||
|
class Timer:
|
||||||
|
"""Timer class."""
|
||||||
|
def __init__(self, interval, callback):
|
||||||
|
"""Init timer."""
|
||||||
|
self._interval = interval
|
||||||
|
self._callback = callback
|
||||||
|
self._task = asyncio.ensure_future(self._job())
|
||||||
|
|
||||||
|
async def _job(self):
|
||||||
|
await asyncio.sleep(self._interval)
|
||||||
|
await self._callback()
|
||||||
|
self._task = asyncio.ensure_future(self._job())
|
||||||
|
|
||||||
|
def cancel(self):
|
||||||
|
"""Cancel timer."""
|
||||||
|
self._task.cancel()
|
||||||
|
|
||||||
|
|
||||||
def start():
|
def start():
|
||||||
"""Entrypoint."""
|
"""Entrypoint."""
|
||||||
init_all()
|
init_all()
|
||||||
|
|||||||
Reference in New Issue
Block a user