summaryrefslogtreecommitdiff
path: root/src/home/mqtt/relay.py
blob: a90f19c59dcbc79246988cb41aeabf566a7decd8 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import paho.mqtt.client as mqtt
import re
import datetime

from .payload.relay import (
    PowerPayload,
)
from .esp import MqttEspBase


class MqttRelay(MqttEspBase):
    TOPIC_LEAF = 'relay'

    def set_power(self, device_id, enable: bool, secret=None):
        device = next(d for d in self._devices if d.id == device_id)
        secret = secret if secret else device.secret

        assert secret is not None, 'device secret not specified'

        payload = PowerPayload(secret=secret,
                               state=enable)
        self._client.publish(f'hk/{device.id}/{self.TOPIC_LEAF}/power',
                             payload=payload.pack(),
                             qos=1)
        self._client.loop_write()

    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(['power']), msg.topic)
            if not match:
                return

            device_id = match.group(1)
            subtopic = match.group(2)

            message = None
            if subtopic == 'power':
                message = PowerPayload.unpack(msg.payload)

            if message and self._message_callback:
                self._message_callback(device_id, message)

        except Exception as e:
            self._logger.exception(str(e))


class MqttRelayState:
    enabled: bool
    update_time: datetime.datetime
    rssi: int
    fw_version: int
    ever_updated: bool

    def __init__(self):
        self.ever_updated = False
        self.enabled = False
        self.rssi = 0

    def update(self,
               enabled: bool,
               rssi: int,
               fw_version=None):
        self.ever_updated = True
        self.enabled = enabled
        self.rssi = rssi
        self.update_time = datetime.datetime.now()
        if fw_version:
            self.fw_version = fw_version