]> the.earth.li Git - energenie-attiny.git/blob - mqtt-power
Add periodic uptime information to MQTT listener
[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 uptime():
66     with open('/proc/uptime', 'r') as f:
67         seconds = int(float(f.readline().split()[0]))
68
69     days = seconds // 86400
70     seconds %= 86400
71     hours = seconds // 3600
72     seconds %= 3600
73     minutes = seconds // 60
74     seconds %= 60
75
76     return "{}T{}:{}:{}".format(days, hours, minutes, seconds)
77
78 relay = None
79 for dev in hid.enumerate(0x16c0, 0x05df):
80     if dev['manufacturer_string'] == 'www.dcttech.com':
81         relay = hid.device()
82         relay.open_path(dev['path'])
83         break
84
85 if relay is None:
86     raise ValueError('Relay device not found')
87
88 relay.set_nonblocking(1)
89 serno = relay.get_feature_report(1, 9)
90 serno_str = bytes(serno[0:5]).decode('ascii')
91
92 client = mqtt.Client("P1Client")
93 client.tls_set(ca_certs='/etc/ssl/certs/ca-certificates.crt')
94 client.username_pw_set(auth['username'], auth['password'])
95 client.user_data_set(relay)
96 client.on_message = mqtt_message
97 client.connect(Broker, port=8883)
98 print("Subscribing to %s" % prefix.format(serno_str))
99 client.subscribe(prefix.format(serno_str))
100 client.loop_start()
101
102 while True:
103     state = {
104         'Time': time.strftime('%Y-%m-%dT%H:%M:%S', time.gmtime()),
105         'Uptime': uptime()
106     }
107     client.publish("relay/{}/state".format(serno_str), json.dumps(state))
108     time.sleep(300)