3 # Copyright 2018 Jonathan McDowell <noodles@earth.li>
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.
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.
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/>.
24 import paho.mqtt.client as mqtt
28 prefix = 'relay-{}/cmnd/#'
30 'username': 'mqttuser',
31 'password': 'mqttpass',
35 def control_relay(device, relay=0, power=False):
46 def mqtt_message(client, device, message):
48 print("message received :", str(message.payload.decode("utf-8")))
49 print(" topic :", message.topic)
51 relay = int(message.topic[-1])
53 cmnd = message.payload.decode("utf-8").lower()
54 if cmnd in ["on", "1", "true"]:
56 elif cmnd in ["off", "0", "false"]:
59 print("Unknown command value: %s" % cmnd)
62 control_relay(device, relay, state)
65 def mqtt_connect(client, userdata, flags, rc):
67 print("Connected to MQTT server")
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))
75 with open('/proc/uptime', 'r') as f:
76 seconds = int(float(f.readline().split()[0]))
78 days = seconds // 86400
80 hours = seconds // 3600
82 minutes = seconds // 60
85 return "{}T{}:{}:{}".format(days, hours, minutes, seconds)
88 for dev in hid.enumerate(0x16c0, 0x05df):
89 if dev['manufacturer_string'] == 'www.dcttech.com':
91 relay.open_path(dev['path'])
95 raise ValueError('Relay device not found')
97 relay.set_nonblocking(1)
98 serno = relay.get_feature_report(1, 9)
99 serno_str = bytes(serno[0:5]).decode('ascii')
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)
109 client.will_set("relay/{}/LWT".format(serno_str), "Offline", retain=True)
113 'Time': time.strftime('%Y-%m-%dT%H:%M:%S', time.gmtime()),
116 client.publish("relay/{}/state".format(serno_str), json.dumps(state))