summaryrefslogtreecommitdiff
path: root/src/home/mqtt/temphum.py
blob: b9b2eb968dc6bd3741e0814d72eb9ff5c06e9b8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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))