]> the.earth.li Git - energenie-attiny.git/blob - mqtt-power
Resubscribe to MQTT prefix when reconnecting
[energenie-attiny.git] / mqtt-power
1 #!/usr/bin/python3
2 #
3 # Copyright 2018 Jonathan McDowell <noodles@earth.li>
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 import glob
19 import time
20 import sys
21
22 import hid
23 import json
24 import paho.mqtt.client as mqtt
25
26 debug = False
27 Broker = 'mqtt.host'
28 prefix = 'relay-{}/cmnd/#'
29 auth = {
30     'username': 'mqttuser',
31     'password': 'mqttpass',
32 }
33
34
35 def control_relay(device, relay=0, power=False):
36     cmnd = [0] * 8
37     if power:
38         cmnd[1] = 0xFF
39     else:
40         cmnd[1] = 0xFD
41     cmnd[2] = relay
42     device.write(cmnd)
43     time.sleep(1)
44
45
46 def mqtt_message(client, device, message):
47     if debug:
48         print("message received :", str(message.payload.decode("utf-8")))
49         print("           topic :", message.topic)
50
51     relay = int(message.topic[-1])
52
53     cmnd = message.payload.decode("utf-8").lower()
54     if cmnd in ["on", "1", "true"]:
55         state = True
56     elif cmnd in ["off", "0", "false"]:
57         state = False
58     else:
59         print("Unknown command value: %s" % cmnd)
60         return
61
62     control_relay(device, relay, state)
63
64
65 def mqtt_connect(client, userdata, flags, rc):
66     if debug:
67         print("Connected to MQTT server")
68
69     client.publish("relay/{}/LWT".format(serno_str), "Online", retain=True)
70     print("Subscribing to %s" % prefix.format(serno_str))
71     client.subscribe(prefix.format(serno_str))
72
73
74 def uptime():
75     with open('/proc/uptime', 'r') as f:
76         seconds = int(float(f.readline().split()[0]))
77
78     days = seconds // 86400
79     seconds %= 86400
80     hours = seconds // 3600
81     seconds %= 3600
82     minutes = seconds // 60
83     seconds %= 60
84
85     return "{}T{}:{}:{}".format(days, hours, minutes, seconds)
86
87 relay = None
88 for dev in hid.enumerate(0x16c0, 0x05df):
89     if dev['manufacturer_string'] == 'www.dcttech.com':
90         relay = hid.device()
91         relay.open_path(dev['path'])
92         break
93
94 if relay is None:
95     raise ValueError('Relay device not found')
96
97 relay.set_nonblocking(1)
98 serno = relay.get_feature_report(1, 9)
99 serno_str = bytes(serno[0:5]).decode('ascii')
100
101 client = mqtt.Client("P1Client")
102 client.tls_set(ca_certs='/etc/ssl/certs/ca-certificates.crt')
103 client.username_pw_set(auth['username'], auth['password'])
104 client.user_data_set(relay)
105 client.on_message = mqtt_message
106 client.on_connect = mqtt_connect
107 client.connect(Broker, port=8883)
108 client.loop_start()
109 client.will_set("relay/{}/LWT".format(serno_str), "Offline", retain=True)
110
111 while True:
112     state = {
113         'Time': time.strftime('%Y-%m-%dT%H:%M:%S', time.gmtime()),
114         'Uptime': uptime()
115     }
116     client.publish("relay/{}/state".format(serno_str), json.dumps(state))
117     time.sleep(300)