-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt2mqtt.py
executable file
·61 lines (51 loc) · 1.86 KB
/
mqtt2mqtt.py
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
#!/bin/python3
import datetime
import json
import paho.mqtt.client
import queue
import struct
class Monitor():
def __init__(self):
self.hostname = "192.168.1.17"
self.port = 1883
self.bindings = {}
self.q = queue.Queue()
self.topics = set()
self.connected = False
def on(self, topic, f): # Only takes full literal topics, ones like a/#/b will fail
self.topics.add(topic)
if self.connected:
self.client.subscribe(topic)
if topic not in self.bindings:
self.bindings[topic] = []
self.bindings[topic].append(f)
def start(self):
self.client = paho.mqtt.client.Client()
self.client.on_message = self.on_message
self.client.on_connect = self.on_connect
self.client.connect(self.hostname, self.port)
def on_connect(self, client, _, connect_flags, properties):
for topic in self.topics:
self.client.subscribe(topic)
self.connected = True
def wait(self):
self.client.loop_forever()
def on_message(self, client, _, message):
callbacks = self.bindings.get(message.topic, [])
#print("{}: {}".format(message.topic, repr(callbacks)))
for callback in callbacks:
callback(client, message)
def control_blinds(client, message):
j = json.loads(message.payload)
state = j["action"]
for blind, level in { 1: 28, 2: 25, 3: 25, }.items():
if state == "open":
topic, message = "zigbee2mqtt/Blinds/{}/Blind/set".format(blind), '{"state": "OPEN" }'
elif state == "close":
topic, message = "zigbee2mqtt/Blinds/{}/Blind/set".format(blind), '{{"position": {} }}'.format(level)
client.publish(topic, message)
if __name__ == '__main__':
m = Monitor()
m.start()
m.on("zigbee2mqtt/Blinds/1/Remote", control_blinds)
m.wait()