From c22e03eef7517592526e8a69092d0709d888104f Mon Sep 17 00:00:00 2001 From: JT Knabenschuh Date: Fri, 28 Jul 2023 01:56:58 +0200 Subject: [PATCH 1/7] Adds serial stream read delimiter and flush option --- mqtt_io/modules/stream/serial.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/mqtt_io/modules/stream/serial.py b/mqtt_io/modules/stream/serial.py index bd913c68..cabac6bb 100644 --- a/mqtt_io/modules/stream/serial.py +++ b/mqtt_io/modules/stream/serial.py @@ -37,6 +37,17 @@ "empty": False, "allowed": STOPBITS_CHOICES, }, + "delimiter": { + "type": "string", + "required": False, + "empty": True + }, + "reset_before_read": { + "type": "boolean", + "required": False, + "empty": False, + "default": False + } } # pylint: disable=no-member @@ -80,7 +91,16 @@ def setup_module(self) -> None: self.ser.flushInput() def read(self) -> Optional[bytes]: - return self.ser.read(self.ser.in_waiting) or None + if("delimiter" in self.config): + if("reset_before_read" in self.config): + if(self.config["reset_before_read"]): + self.ser.reset_input_buffer() + data = self.ser.read_until(self.config["delimiter"].encode("utf-8")) + if data: + data = data[:-len(self.config["delimiter"])] + return data or None + else: + return self.ser.read(self.ser.in_waiting) or None def write(self, data: bytes) -> None: self.ser.write(data) From 755720152c094daaa04b051d7b12d679ed410828 Mon Sep 17 00:00:00 2001 From: jtkDvlp Date: Fri, 28 Jul 2023 02:27:09 +0200 Subject: [PATCH 2/7] Add ha_discovery support for serial stream --- mqtt_io/home_assistant.py | 34 +++++++++++++++++++++++++++++++- mqtt_io/modules/stream/serial.py | 4 ++++ mqtt_io/server.py | 7 +++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/mqtt_io/home_assistant.py b/mqtt_io/home_assistant.py index 6e405cf0..54c0abc4 100644 --- a/mqtt_io/home_assistant.py +++ b/mqtt_io/home_assistant.py @@ -8,7 +8,7 @@ import logging from typing import Any, Dict -from .constants import INPUT_TOPIC, OUTPUT_TOPIC, SENSOR_TOPIC, SET_SUFFIX +from .constants import INPUT_TOPIC, OUTPUT_TOPIC, SENSOR_TOPIC, STREAM_TOPIC, SET_SUFFIX, SEND_SUFFIX from .mqtt import MQTTClientOptions, MQTTMessageSend from .types import ConfigType from . import VERSION @@ -148,3 +148,35 @@ def hass_announce_sensor_input( json.dumps(sensor_config).encode("utf8"), retain=True, ) + +def hass_announce_stream( + in_conf: ConfigType, mqtt_conf: ConfigType, mqtt_options: MQTTClientOptions +) -> MQTTMessageSend: + """ + Create a message which announces stream as text to Home Assistant. + """ + disco_conf: ConfigType = mqtt_conf["ha_discovery"] + name: str = in_conf["name"] + disco_prefix: str = disco_conf["prefix"] + stream_config = get_common_config(in_conf, mqtt_conf, mqtt_options) + stream_config.update( + dict( + unique_id=f"{mqtt_options.client_id}_{in_conf['module']}_input_{name}", + state_topic="/".join((mqtt_conf["topic_prefix"], STREAM_TOPIC, name)), + command_topic="/".join((mqtt_conf["topic_prefix"], STREAM_TOPIC, name, SEND_SUFFIX)) + ) + ) + return MQTTMessageSend( + "/".join( + ( + disco_prefix, + stream_config.pop("component", "text"), + mqtt_options.client_id, + name, + "config", + ) + ), + json.dumps(stream_config).encode("utf8"), + retain=True, + ) + diff --git a/mqtt_io/modules/stream/serial.py b/mqtt_io/modules/stream/serial.py index cabac6bb..a9eb9823 100644 --- a/mqtt_io/modules/stream/serial.py +++ b/mqtt_io/modules/stream/serial.py @@ -47,6 +47,10 @@ "required": False, "empty": False, "default": False + }, + "ha_discovery": { + "type": "dict", + "allow_unknown": True } } diff --git a/mqtt_io/server.py b/mqtt_io/server.py index f026cefc..23535d5e 100644 --- a/mqtt_io/server.py +++ b/mqtt_io/server.py @@ -56,6 +56,7 @@ hass_announce_digital_input, hass_announce_digital_output, hass_announce_sensor_input, + hass_announce_stream ) from .modules import install_missing_module_requirements from .modules.gpio import GenericGPIO, InterruptEdge, InterruptSupport, PinDirection @@ -636,6 +637,12 @@ def _ha_discovery_announce(self) -> None: sens_conf, mqtt_config, self.mqtt_client_options ) ) + for stream_conf in self.stream_configs.values(): + messages.append( + hass_announce_stream( + stream_conf, mqtt_config, self.mqtt_client_options + ) + ) for msg in messages: self.mqtt_task_queue.put_nowait( PriorityCoro(self._mqtt_publish(msg), MQTT_ANNOUNCE_PRIORITY) From 944d2caa90e61d3b933874a67643f1b93190ef22 Mon Sep 17 00:00:00 2001 From: jtkDvlp Date: Tue, 1 Aug 2023 01:08:30 +0200 Subject: [PATCH 3/7] Betters some stuff for the linter --- mqtt_io/home_assistant.py | 1 - mqtt_io/modules/stream/serial.py | 14 +++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/mqtt_io/home_assistant.py b/mqtt_io/home_assistant.py index 54c0abc4..71d0f1a9 100644 --- a/mqtt_io/home_assistant.py +++ b/mqtt_io/home_assistant.py @@ -179,4 +179,3 @@ def hass_announce_stream( json.dumps(stream_config).encode("utf8"), retain=True, ) - diff --git a/mqtt_io/modules/stream/serial.py b/mqtt_io/modules/stream/serial.py index a9eb9823..16d933a8 100644 --- a/mqtt_io/modules/stream/serial.py +++ b/mqtt_io/modules/stream/serial.py @@ -95,18 +95,22 @@ def setup_module(self) -> None: self.ser.flushInput() def read(self) -> Optional[bytes]: - if("delimiter" in self.config): - if("reset_before_read" in self.config): - if(self.config["reset_before_read"]): + data = None + if "delimiter" in self.config: + if "reset_before_read" in self.config: + if self.config["reset_before_read"]: self.ser.reset_input_buffer() data = self.ser.read_until(self.config["delimiter"].encode("utf-8")) if data: data = data[:-len(self.config["delimiter"])] - return data or None else: - return self.ser.read(self.ser.in_waiting) or None + data = self.ser.read(self.ser.in_waiting) or None + + return data def write(self, data: bytes) -> None: + if("delimiter" in self.config): + data = data + self.config["delimiter"].encode("utf-8") self.ser.write(data) def cleanup(self) -> None: From 2a5a6e1231a407fa84690fc593324f294baade0f Mon Sep 17 00:00:00 2001 From: jtkDvlp Date: Tue, 1 Aug 2023 01:15:28 +0200 Subject: [PATCH 4/7] Adds serial stream module example config --- config.example.yml | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/config.example.yml b/config.example.yml index 5ead074e..62e8819c 100644 --- a/config.example.yml +++ b/config.example.yml @@ -42,7 +42,7 @@ digital_inputs: off_payload: "OFF" pullup: no pulldown: yes - + - name: button_left module: raspberrypi pin: 23 @@ -76,3 +76,16 @@ sensor_inputs: module: ds18b22 interval: 10 digits: 2 + +stream_modules: + - name: mmwave_uart + module: serial + device: /dev/serial0 + baud: 115200 + delimiter: "\r\n" + reset_before_read: True + read_interval: 5 + timeout: 60 + ha_discovery: + unique_id: mmwave_uart + name: MMWave UART From 44b4cbb77872e774455fc39b0a5da3a08a1b7f53 Mon Sep 17 00:00:00 2001 From: jtkDvlp Date: Wed, 2 Aug 2023 23:20:42 +0200 Subject: [PATCH 5/7] Betters some stuff for the linter, again --- mqtt_io/home_assistant.py | 4 +++- mqtt_io/modules/stream/serial.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/mqtt_io/home_assistant.py b/mqtt_io/home_assistant.py index 71d0f1a9..2c4adcad 100644 --- a/mqtt_io/home_assistant.py +++ b/mqtt_io/home_assistant.py @@ -8,7 +8,9 @@ import logging from typing import Any, Dict -from .constants import INPUT_TOPIC, OUTPUT_TOPIC, SENSOR_TOPIC, STREAM_TOPIC, SET_SUFFIX, SEND_SUFFIX +from .constants import ( + INPUT_TOPIC, OUTPUT_TOPIC, SENSOR_TOPIC, STREAM_TOPIC, + SET_SUFFIX, SEND_SUFFIX) from .mqtt import MQTTClientOptions, MQTTMessageSend from .types import ConfigType from . import VERSION diff --git a/mqtt_io/modules/stream/serial.py b/mqtt_io/modules/stream/serial.py index 16d933a8..a800eebf 100644 --- a/mqtt_io/modules/stream/serial.py +++ b/mqtt_io/modules/stream/serial.py @@ -109,7 +109,7 @@ def read(self) -> Optional[bytes]: return data def write(self, data: bytes) -> None: - if("delimiter" in self.config): + if "delimiter" in self.config: data = data + self.config["delimiter"].encode("utf-8") self.ser.write(data) From 82cbe26b450a175e54aa3f67935e81a8543d982b Mon Sep 17 00:00:00 2001 From: Benji <46675043+BenjiU@users.noreply.github.com> Date: Mon, 26 Feb 2024 21:16:42 +0100 Subject: [PATCH 6/7] retrigger actions From af9c564c6af5c89324f9bb1ff98f1abfd5a6b1a6 Mon Sep 17 00:00:00 2001 From: Benji <46675043+BenjiU@users.noreply.github.com> Date: Wed, 31 Jul 2024 19:13:55 +0200 Subject: [PATCH 7/7] retrigger actions