diff --git a/py/miniconf-mqtt/miniconf/async_.py b/py/miniconf-mqtt/miniconf/async_.py index e0342ee3..3d494e28 100644 --- a/py/miniconf-mqtt/miniconf/async_.py +++ b/py/miniconf-mqtt/miniconf/async_.py @@ -123,7 +123,7 @@ def _dispatch(self, message: Message): fut.set_exception(MiniconfException(code, resp)) del self._inflight[cd] - async def _do(self, topic: str, *, response=1, **kwargs): + async def _do(self, path: str, *, response=1, **kwargs): response = int(response) props = Properties(PacketTypes.PUBLISH) if response: @@ -135,6 +135,7 @@ async def _do(self, topic: str, *, response=1, **kwargs): assert cd not in self._inflight self._inflight[cd] = fut, [] + topic = f"{self.prefix}/settings{path}" LOGGER.info(f"Publishing {topic}: {kwargs.get('payload')}, [{props}]") await self.client.publish( topic, @@ -160,24 +161,22 @@ async def set(self, path: str, value, retain=False, response=True, **kwargs): retain: Retain the the setting on the broker. """ return await self._do( - topic=f"{self.prefix}/settings{path}", + path, payload=json.dumps(value, separators=(",", ":")), response=response, retain=retain, **kwargs, ) - async def list(self, root: str = "", **kwargs): + async def list(self, path: str = "", **kwargs): """Get a list of all the paths below a given root. Args: - root: Path to the root node to list. + path: Path to the root node to list. """ - return await self._do( - topic=f"{self.prefix}/settings{root}", response=2, **kwargs - ) + return await self._do(path, response=2, **kwargs) - async def dump(self, root: str = "", **kwargs): + async def dump(self, path: str = "", **kwargs): """Dump all the paths at or below a given root into the settings namespace. Note that the target may be unable to respond to messages when a multipart @@ -185,9 +184,9 @@ async def dump(self, root: str = "", **kwargs): This method does not wait for completion. Args: - root: Path to the root node to dump. Can be a leaf or an internal node. + path: Path to the root node to dump. Can be a leaf or an internal node. """ - await self._do(topic=f"{self.prefix}/settings{root}", response=0, **kwargs) + await self._do(path, response=0, **kwargs) async def get(self, path: str, **kwargs): """Get the specific value of a given path. @@ -195,9 +194,7 @@ async def get(self, path: str, **kwargs): Args: path: The path to get. Must be a leaf node. """ - return json.loads( - await self._do(topic=f"{self.prefix}/settings{path}", **kwargs) - ) + return json.loads(await self._do(path, **kwargs)) async def clear(self, path: str, response=True, **kwargs): """Clear retained value from a path. @@ -209,7 +206,7 @@ async def clear(self, path: str, response=True, **kwargs): """ return json.loads( await self._do( - f"{self.prefix}/settings{path}", + path, retain=True, response=response, **kwargs, diff --git a/py/miniconf-mqtt/miniconf/sync.py b/py/miniconf-mqtt/miniconf/sync.py index 7880b350..5d27edb4 100644 --- a/py/miniconf-mqtt/miniconf/sync.py +++ b/py/miniconf-mqtt/miniconf/sync.py @@ -55,7 +55,7 @@ def close(self): self.client.on_unsubscribe = None LOGGER.info(f"Unsubscribed from {self.response_topic}") - def _do(self, topic: str, *, response=1, timeout=None, **kwargs): + def _do(self, path: str, *, response=1, timeout=None, **kwargs): response = int(response) props = Properties(PacketTypes.PUBLISH) @@ -114,6 +114,7 @@ def on_message(_client, _userdata, message): self.client.on_message = on_message + topic = f"{self.prefix}/settings{path}" LOGGER.info(f"Publishing {topic}: {kwargs.get('payload')}, [{props}]") _pub = self.client.publish(topic, properties=props, **kwargs) @@ -140,22 +141,22 @@ def set(self, path: str, value, retain=False, response=True, **kwargs): retain: Retain the the setting on the broker. """ return self._do( - topic=f"{self.prefix}/settings{path}", + path, payload=json.dumps(value, separators=(",", ":")), response=response, retain=retain, **kwargs, ) - def list(self, root: str = "", **kwargs): + def list(self, path: str = "", **kwargs): """Get a list of all the paths below a given root. Args: - root: Path to the root node to list. + path: Path to the root node to list. """ - return self._do(topic=f"{self.prefix}/settings{root}", response=2, **kwargs) + return self._do(path, response=2, **kwargs) - def dump(self, root: str = "", **kwargs): + def dump(self, path: str = "", **kwargs): """Dump all the paths at or below a given root into the settings namespace. Note that the target may be unable to respond to messages when a multipart @@ -163,9 +164,9 @@ def dump(self, root: str = "", **kwargs): This method does not wait for completion. Args: - root: Path to the root node to dump. Can be a leaf or an internal node. + path: Path to the root node to dump. Can be a leaf or an internal node. """ - return self._do(topic=f"{self.prefix}/settings{root}", response=0, **kwargs) + return self._do(path, response=0, **kwargs) def get(self, path: str, **kwargs): """Get the specific value of a given path. @@ -173,7 +174,7 @@ def get(self, path: str, **kwargs): Args: path: The path to get. Must be a leaf node. """ - return json.loads(self._do(topic=f"{self.prefix}/settings{path}", **kwargs)) + return json.loads(self._do(path, **kwargs)) def clear(self, path: str, response=True, **kwargs): """Clear retained value from a path. @@ -185,7 +186,7 @@ def clear(self, path: str, response=True, **kwargs): """ return json.loads( self._do( - f"{self.prefix}/settings{path}", + path, retain=True, response=response, **kwargs,