summaryrefslogtreecommitdiff
path: root/src/home/mqtt/temphum.py
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2023-05-11 04:18:08 +0300
committerEvgeny Zinoviev <me@ch1p.io>2023-05-11 04:18:12 +0300
commit0aba139aeff8ff80757c5d36502413299a0b449e (patch)
tree2b8e760ff14d4691783eb7c7d341f093199aab82 /src/home/mqtt/temphum.py
parent586d84b0c0a8b4dc1b5057733892b754397234ec (diff)
mqtt, esp: add new esp8266-based device
Diffstat (limited to 'src/home/mqtt/temphum.py')
-rw-r--r--src/home/mqtt/temphum.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/home/mqtt/temphum.py b/src/home/mqtt/temphum.py
new file mode 100644
index 0000000..b9b2eb9
--- /dev/null
+++ b/src/home/mqtt/temphum.py
@@ -0,0 +1,33 @@
+import paho.mqtt.client as mqtt
+import re
+
+from .payload.temphum import (
+ TempHumDataPayload
+)
+from .esp import MqttEspBase
+
+
+class MqttTempHum(MqttEspBase):
+ TOPIC_LEAF = 'temphum'
+
+ def on_message(self, client: mqtt.Client, userdata, msg):
+ if super().on_message(client, userdata, msg):
+ return
+
+ try:
+ match = re.match(self.get_mqtt_topics(['data']), msg.topic)
+ if not match:
+ return
+
+ device_id = match.group(1)
+ subtopic = match.group(2)
+
+ message = None
+ if subtopic == 'data':
+ message = TempHumDataPayload.unpack(msg.payload)
+
+ if message and self._message_callback:
+ self._message_callback(device_id, message)
+
+ except Exception as e:
+ self._logger.exception(str(e))