From 7e59f76784c064d1b9556b258466d97c68473b55 Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Mon, 25 Mar 2024 02:14:17 +0100 Subject: [PATCH] subscribe to updates --- pyhon/appliance.py | 11 ++++++++++- pyhon/connection/mqtt.py | 16 ++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/pyhon/appliance.py b/pyhon/appliance.py index 3754aca..73992c1 100644 --- a/pyhon/appliance.py +++ b/pyhon/appliance.py @@ -3,7 +3,7 @@ import logging import re from datetime import datetime, timedelta from pathlib import Path -from typing import Optional, Dict, Any, TYPE_CHECKING, List, TypeVar, overload +from typing import Optional, Dict, Any, TYPE_CHECKING, List, TypeVar, overload, Callable from pyhon import diagnose, exceptions from pyhon.appliances.base import ApplianceBase @@ -43,6 +43,7 @@ class HonAppliance: self._additional_data: Dict[str, Any] = {} self._last_update: Optional[datetime] = None self._default_setting = HonParameter("", {}, "") + self._notify_function: Optional[Callable[[Any], None]] = None try: self._extra: Optional[ApplianceBase] = importlib.import_module( @@ -312,3 +313,11 @@ class HonAppliance: elif isinstance(target, HonParameterEnum): target.values = main.values target.value = main.value + + def subscribe(self, notify_function: Callable[[Any], None]) -> None: + self._notify_function = notify_function + + def notify(self) -> None: + self.sync_params_to_command("settings") + if self._notify_function: + self._notify_function(self.attributes) diff --git a/pyhon/connection/mqtt.py b/pyhon/connection/mqtt.py index 389a5aa..0a3aeef 100644 --- a/pyhon/connection/mqtt.py +++ b/pyhon/connection/mqtt.py @@ -17,16 +17,15 @@ appliances: list[HonAppliance] = [] def on_lifecycle_stopped(lifecycle_stopped_data: mqtt5.LifecycleStoppedData) -> None: - print("Lifecycle Stopped") - print(lifecycle_stopped_data) + _LOGGER.info("Lifecycle Stopped: %s", str(lifecycle_stopped_data)) def on_lifecycle_connection_success( lifecycle_connect_success_data: mqtt5.LifecycleConnectSuccessData, ) -> None: - print("Lifecycle Connection Success") - print(lifecycle_connect_success_data.connack_packet) - print(lifecycle_connect_success_data.negotiated_settings) + _LOGGER.info( + "Lifecycle Connection Success: %s", str(lifecycle_connect_success_data) + ) def on_publish_received(data: mqtt5.PublishReceivedData) -> None: @@ -40,9 +39,10 @@ def on_publish_received(data: mqtt5.PublishReceivedData) -> None: ) for parameter in payload["parameters"]: appliance.attributes["parameters"][parameter["parName"]].update(parameter) - print(parameter) + appliance.notify() + _LOGGER.debug("%s - %s", topic, payload) else: - print(topic, payload) + _LOGGER.debug("%s - %s", topic, payload) async def create_mqtt_client(api: "HonAPI") -> mqtt5.Client: @@ -64,7 +64,7 @@ async def create_mqtt_client(api: "HonAPI") -> mqtt5.Client: def subscribe(client: mqtt5.Client, appliance: HonAppliance) -> None: for topic in appliance.info.get("topics", {}).get("subscribe", []): client.subscribe(mqtt5.SubscribePacket([mqtt5.Subscription(topic)])).result(10) - _LOGGER.error("Subscribed to topic %s", topic) + _LOGGER.info("Subscribed to topic %s", topic) async def start(api: "HonAPI", app: list[HonAppliance]) -> mqtt5.Client: