subscribe to updates

This commit is contained in:
Andre Basche 2024-03-25 02:14:17 +01:00
parent f108005a4d
commit 7e59f76784
2 changed files with 18 additions and 9 deletions

View file

@ -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)

View file

@ -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: