From 8a028ff8b715988c7b1a63bc6944d036ddab7144 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Thu, 28 Nov 2024 22:57:23 +0100 Subject: [PATCH 1/2] py: match sync/async code --- py/miniconf-mqtt/miniconf/async_.py | 35 ++++++++++++++++------------- py/miniconf-mqtt/miniconf/sync.py | 9 ++++++-- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/py/miniconf-mqtt/miniconf/async_.py b/py/miniconf-mqtt/miniconf/async_.py index b3bf1ba5..1d943921 100644 --- a/py/miniconf-mqtt/miniconf/async_.py +++ b/py/miniconf-mqtt/miniconf/async_.py @@ -10,9 +10,9 @@ import uuid from typing import Dict, Any -from aiomqtt import Client, Message, MqttError -from paho.mqtt.properties import Properties, PacketTypes import paho.mqtt +from paho.mqtt.properties import Properties, PacketTypes +from aiomqtt import Client, Message, MqttError MQTTv5 = paho.mqtt.enums.MQTTProtocolVersion.MQTTv5 @@ -111,18 +111,17 @@ def _dispatch(self, message: Message): LOGGER.warning("Discarding message without response code user property") return - response = message.payload.decode("utf-8") + resp = message.payload.decode("utf-8") if code == "Continue": - ret.append(response) - return - - if code == "Ok": - if response: - ret.append(response) + ret.append(resp) + elif code == "Ok": + if resp: + ret.append(resp) fut.set_result(ret) + del self._inflight[cd] else: - fut.set_exception(MiniconfException(code, response)) - del self._inflight[cd] + fut.set_exception(MiniconfException(code, resp)) + del self._inflight[cd] async def _do(self, topic: str, *, response=1, **kwargs): response = int(response) @@ -150,6 +149,7 @@ async def _do(self, topic: str, *, response=1, **kwargs): return ret[0] assert ret return ret + return None async def set(self, path: str, value, retain=False, response=True, **kwargs): """Write the provided data to the specified path. @@ -206,7 +206,10 @@ async def clear(self, path: str, response=True, **kwargs): path: The path to clear. Must be a leaf node. """ return await self._do( - f"{self.prefix}/settings{path}", retain=True, response=response, **kwargs + f"{self.prefix}/settings{path}", + retain=True, + response=response, + **kwargs, ) @@ -234,10 +237,6 @@ async def discover( suffix = "/alive" topic = f"{prefix}{suffix}" - t_start = asyncio.get_running_loop().time() - await client.subscribe(topic) - t_subscribe = asyncio.get_running_loop().time() - t_start - async def listen(): async for message in client.messages: peer = message.topic.value.removesuffix(suffix) @@ -249,6 +248,10 @@ async def listen(): logging.info(f"Discovered {peer} alive") discovered[peer] = payload + t_start = asyncio.get_running_loop().time() + await client.subscribe(topic) + t_subscribe = asyncio.get_running_loop().time() - t_start + try: await asyncio.wait_for( listen(), timeout=rel_timeout * t_subscribe + abs_timeout diff --git a/py/miniconf-mqtt/miniconf/sync.py b/py/miniconf-mqtt/miniconf/sync.py index 869b5ec1..fe8756ce 100644 --- a/py/miniconf-mqtt/miniconf/sync.py +++ b/py/miniconf-mqtt/miniconf/sync.py @@ -24,6 +24,11 @@ class Miniconf: """Miniconf over MQTT (synchronous)""" def __init__(self, client: Client, prefix: str): + """ + Args: + client: A connected MQTT5 client. + prefix: The MQTT toptic prefix of the device to control. + """ self.client = client self.prefix = prefix self.response_topic = f"{prefix}/response" @@ -173,6 +178,8 @@ def get(self, path: str, **kwargs): def clear(self, path: str, response=True, **kwargs): """Clear retained value from a path. + This does not change (`set()`) or reset/clear the value on the device. + Args: path: The path to clear. Must be a leaf node. """ @@ -208,8 +215,6 @@ def discover( suffix = "/alive" topic = f"{prefix}{suffix}" - discovered = {} - def on_message(_client, _userdata, message): logging.debug(f"Got message from {message.topic}: {message.payload}") peer = message.topic.removesuffix(suffix) From 699514c742e6f6705eab28c549bfd1570c23cee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Fri, 29 Nov 2024 14:50:15 +0100 Subject: [PATCH 2/2] README: spelling --- miniconf/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/miniconf/README.md b/miniconf/README.md index f472fb55..e1bac3b9 100644 --- a/miniconf/README.md +++ b/miniconf/README.md @@ -6,7 +6,7 @@ [![Continuous Integration](https://github.com/vertigo-designs/miniconf/workflows/Continuous%20Integration/badge.svg)](https://github.com/quartiq/miniconf/actions) `miniconf` enables lightweight (`no_std`/no alloc) serialization, deserialization, -and access within a tree of heretogeneous types by keys. +and access within a tree of heterogeneous types by keys. ## Example