diff --git a/watt42_viewlib/__init__.py b/watt42_viewlib/__init__.py index ac1a9a0..7f733d0 100644 --- a/watt42_viewlib/__init__.py +++ b/watt42_viewlib/__init__.py @@ -8,7 +8,7 @@ from websockets.asyncio.client import connect logger = logging.getLogger(__name__) -def attach_w42_state(rx_var: panel.rx, system_id: str): +def attach_w42_state(rx_var: panel.rx, system_id: str, token: str): WS_URL = "ws://localhost:8000/ws/systems" # TODO: make configurable @@ -23,12 +23,17 @@ def attach_w42_state(rx_var: panel.rx, system_id: str): logger.info(f"Connected to {WS_URL}") send_response = await websocket.send(json.dumps({ "action": "subscribe", - "system_id": system_id + "system_id": system_id, + "token": token })) logger.info(f"Subscribed to system {system_id}, waiting for messages..., send_response={send_response}") async for message in websocket: as_json = json.loads(message) - rx_var.rx.value = as_json['change']['state'] + if as_json.get('error'): + logger.error(f"Error from websocket: {as_json['error']}") + rx_var.rx.value = as_json + else: + rx_var.rx.value = as_json['change']['state'] except ConnectionClosed: if must_reconnect: logger.info("connection closed, retrying...")