diff --git a/README.rst b/README.rst index fb22244..0d72403 100644 --- a/README.rst +++ b/README.rst @@ -27,7 +27,20 @@ Maintains a reconnecting websocket to the system. Supported devices ================== -LOKE1/Loke Basic/LS-01 using control system 4.1.5 +.. list-table:: + :widths: 25 25 50 + :header-rows: 1 + + * - Model + - Supported control system versions + - Unsupported control system versions + * - LOKE LS-01/LT-01 + - 4.1.5 + - <4.1.5 + * - LOKE LS-02/LT-02 + - unknown support + - unknown support + Example usage ============================= @@ -40,13 +53,17 @@ Example usage def handle_message(data: dict): # must be safe to call from event loop - print("message handler") - print(data) + print("got data: ", data) + + def handle_state_change(state): + # must be safe to call from event loop + print("new state: ", state) async def main(): update_interval = 10 - with Client("192.168.1.151", update_interval=update_interval) as hrv_client: + with Client(HOST, update_interval=update_interval) as hrv_client: hrv_client.add_message_handler(handle_message) + hrv_client.add_state_change_handler(handle_state_change) await asyncio.sleep(update_interval +1 ) # wait around a bit for data await hrv_client.send_command(DataKey.FIREPLACE_MODE, 1) # turn on fireplace mode diff --git a/src/pysaleryd/client.py b/src/pysaleryd/client.py index 6359dc5..79730bd 100644 --- a/src/pysaleryd/client.py +++ b/src/pysaleryd/client.py @@ -36,10 +36,10 @@ def __init__( self._port = port self._data: dict[DataKey, str] = {} self._error_cache = ErrorCache() - self._on_message_handlers: set[Callable[[dict[DataKey, str]]]] = set() - self._on_state_change_handlers: set[Callable[[dict[DataKey, str]]]] = set() + self._on_data_handlers: set[Callable[[dict[DataKey, str]]]] = set() + self._on_state_change_handlers: set[Callable[[State]]] = set() self._connect_timeout = connect_timeout - self._tasks = [asyncio.create_task(self._do_call_message_handlers())] + self._tasks = [asyncio.create_task(self._do_call_data_handlers())] self._websocket = ReconnectingWebsocketClient( host=self._ip, port=self._port, @@ -70,15 +70,15 @@ async def _send_start_message(self): """Send start message to server to begin receiving data""" await self._websocket.send("#:\r") - async def _do_call_message_handlers(self): + async def _do_call_data_handlers(self): """Call message handlers with data at update_interval""" while True: await asyncio.sleep(self._update_interval) - self._call_message_handlers() + self._call_data_handlers() - def _call_message_handlers(self): + def _call_data_handlers(self): """Call handlers with data""" - for handler in self._on_message_handlers: + for handler in self._on_data_handlers: try: handler(self.data) except Exception: @@ -125,11 +125,11 @@ async def _on_message(self, msg: str): else: self._data[key] = value if message_type == MessageType.ACK_OK: - self._call_message_handlers() + self._call_data_handlers() except ParseError as e: _LOGGER.warning(e, exc_info=1) - def add_state_change_handler(self, handler: Callable[[str], None]): + def add_state_change_handler(self, handler: Callable[[State], None]): """Add state change handler to be called when client state changes :param handler: handler to be added @@ -137,7 +137,7 @@ def add_state_change_handler(self, handler: Callable[[str], None]): """ self._on_state_change_handlers.add(handler) - def remove_state_change_handler(self, handler: Callable[[str], None]): + def remove_state_change_handler(self, handler: Callable[[State], None]): """Remove state change handler :param handler: handler to be removed @@ -145,23 +145,22 @@ def remove_state_change_handler(self, handler: Callable[[str], None]): """ self._on_state_change_handlers.remove(handler) - def add_message_handler(self, handler: Callable[[str], None]): - """Add message handler - - Message handler will be called at update interval + def add_data_handler(self, handler: Callable[[dict[DataKey, str]], None]): + """Add data handler to be called at update interval :param handler: handler function. Must be safe to call from event loop - :type handler: Callable[[str], None] + :type handler: Callable[[dict[DataKey, str]], None] """ - self._on_message_handlers.add(handler) - def remove_message_handler(self, handler: Callable[[str], None]): - """Remove message handler + self._on_data_handlers.add(handler) + + def remove_data_handler(self, handler: Callable[[dict[DataKey, str]], None]): + """Remove data handler :param handler: handler to remove - :type handler: Callable[[str], None] + :type handler: Callable[[dict[DataKey, str]], None] """ - self._on_message_handlers.remove(handler) + self._on_data_handlers.remove(handler) async def send_command(self, key: MessageType, payload: str | int): """Send command to HRV unit diff --git a/tests/test_client.py b/tests/test_client.py index a3bc96f..cb676fc 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -54,8 +54,8 @@ def handler(_data): def broken_handler(data): raise Exception() # pylint: disable=W0719 - hrv_client.add_message_handler(broken_handler) - hrv_client.add_message_handler(handler) + hrv_client.add_data_handler(broken_handler) + hrv_client.add_data_handler(handler) await asyncio.sleep(5) assert isinstance(data, dict)