Skip to content

Commit

Permalink
feat: rename message_handler -> data_handler
Browse files Browse the repository at this point in the history
  • Loading branch information
bj00rn committed Dec 24, 2024
1 parent c17d912 commit 653e423
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
25 changes: 21 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=============================
Expand All @@ -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
Expand Down
39 changes: 19 additions & 20 deletions src/pysaleryd/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -125,43 +125,42 @@ 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
:type 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
:type 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
Expand Down
4 changes: 2 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 653e423

Please sign in to comment.