Skip to content

Commit 9c42571

Browse files
committed
Add callback support to unfurled websocket
1 parent e37a802 commit 9c42571

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

  • custom_components/unfoldedcircle/unfurled/unfurled

custom_components/unfoldedcircle/unfurled/unfurled/remote.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import asyncio
66
import logging
77
import re
8+
from collections.abc import Callable, Coroutine
9+
from typing import Any
810
from urllib.parse import urljoin, urlparse
911

1012
import aiohttp
@@ -126,6 +128,7 @@ def __init__(
126128
# WebSocket
127129
self._ws_client: RemoteWebSocketClient | None = None
128130
self._last_update_type: UpdateType = UpdateType.NONE
131+
self._state_change_callbacks: list[Callable[[], Coroutine[Any, Any, None]]] = []
129132

130133
# Entities cache (media players referenced by activities)
131134
self._entities: dict[str, MediaPlayerEntity] = {}
@@ -291,6 +294,10 @@ async def resolve_discovery(
291294
# WebSocket
292295
# ------------------------------------------------------------------
293296

297+
def on_state_change(self, callback: Callable[[], Coroutine[Any, Any, None]]) -> None:
298+
"""Register a coroutine callback to be called after any WebSocket state update."""
299+
self._state_change_callbacks.append(callback)
300+
294301
async def connect_websocket(self, *, reconnect_delay: float = 10.0) -> None:
295302
"""Start a WebSocket connection and keep it alive automatically.
296303
@@ -343,6 +350,9 @@ async def _handle_ws_message(self, raw: str) -> None:
343350
case IRLearningEvent():
344351
self._on_ir_learning(event)
345352

353+
for cb in self._state_change_callbacks:
354+
await cb()
355+
346356
# WS event handlers (private, synchronous)
347357

348358
def _on_battery(self, event: BatteryEvent) -> None:

0 commit comments

Comments
 (0)