Mercurial > code > home > repos > homeauto
comparison service/mqtt_graph_bridge/mqtt_graph_bridge.py @ 696:c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
Ignore-this: b1ec515e6873a6841b1c31c3fb7a2a36
author | drewp@bigasterisk.com |
---|---|
date | Fri, 31 Jan 2020 23:55:27 -0800 |
parents | 925bc4137c93 |
children | a93fbf0d0daa |
comparison
equal
deleted
inserted
replaced
695:80f6b3ee9c7f | 696:c52b172c0824 |
---|---|
25 }, | 25 }, |
26 ROOM['kitchenCounterLight']: { | 26 ROOM['kitchenCounterLight']: { |
27 'root': 'h801_counter', | 27 'root': 'h801_counter', |
28 'ctx': ROOM['kitchenH801'] | 28 'ctx': ROOM['kitchenH801'] |
29 }, | 29 }, |
30 ROOM['livingLampShelf']: { | |
31 'root': 'sonoff_0/switch/sonoff_basic_relay/command', | |
32 'ctx': ROOM['sonoff_0'], | |
33 'values': 'binary', | |
34 }, | |
30 } | 35 } |
31 | 36 |
32 | 37 |
33 class OutputPage(cyclone.web.RequestHandler): | 38 class OutputPage(cyclone.web.RequestHandler): |
34 def put(self): | 39 def put(self): |
40 | 45 |
41 def _onStatement(self, stmt): | 46 def _onStatement(self, stmt): |
42 log.info(f'incoming statement: {stmt}') | 47 log.info(f'incoming statement: {stmt}') |
43 ignored = True | 48 ignored = True |
44 for dev, attrs in devs.items(): | 49 for dev, attrs in devs.items(): |
50 if stmt[0] == ROOM['frontWindow']: | |
51 ignored = ignored and self._publishFrontScreenText(stmt) | |
52 | |
45 if stmt[0:2] == (dev, ROOM['brightness']): | 53 if stmt[0:2] == (dev, ROOM['brightness']): |
46 for chan, scale in [('w1', 1), | 54 log.info(f'brightness request: {stmt}') |
47 ('r', 1), | 55 brightness = stmt[2].toPython() |
48 ('g', .8), | 56 |
49 ('b', .8)]: | 57 if attrs.get('values', '') == 'binary': |
50 out = stmt[2].toPython() * scale | 58 self._publishOnOff(attrs, brightness) |
51 topic = f"{attrs['root']}/light/kit_{chan}/command" | 59 else: |
52 self.settings.mqtt.publish( | 60 self._publishRgbw(attrs, brightness) |
53 topic.encode('ascii'), | 61 # try to stop saving this; let the device be the master usually |
54 json.dumps({ | 62 self.settings.masterGraph.patchObject( |
55 'state': 'ON', | 63 attrs['ctx'], |
56 'brightness': int(out * 255)}).encode('ascii')) | 64 stmt[0], stmt[1], stmt[2]) |
57 self.settings.masterGraph.patchObject( | |
58 attrs['ctx'], | |
59 stmt[0], stmt[1], stmt[2]) | |
60 ignored = False | 65 ignored = False |
61 if ignored: | 66 if ignored: |
62 log.warn("ignoring %s", stmt) | 67 log.warn("ignoring %s", stmt) |
63 | 68 |
69 def _publishOnOff(self, attrs, brightness): | |
70 msg = 'OFF' | |
71 if brightness > 0: | |
72 msg = 'ON' | |
73 self._publish(topic=attrs['root'], message=msg) | |
74 | |
75 def _publishRgbw(self, attrs, brightness): | |
76 for chan, scale in [('w1', 1), | |
77 ('r', 1), | |
78 ('g', .8), | |
79 ('b', .8)]: | |
80 self._publish( | |
81 topic=f"{attrs['root']}/light/kit_{chan}/command", | |
82 messageJson={ | |
83 'state': 'ON', | |
84 'brightness': int(brightness * 255) | |
85 }) | |
86 | |
87 def _publishFrontScreenText(self, stmt): | |
88 ignored = True | |
89 for line in ['line1', 'line2', 'line3', 'line4']: | |
90 if stmt[1] == ROOM[line]: | |
91 ignored = False | |
92 self.settings.mqtt.publish( | |
93 b'frontwindow/%s' % line.encode('ascii'), | |
94 stmt[2].toPython()) | |
95 return ignored | |
96 | |
97 def _publish(self, topic: str, messageJson: object=None, | |
98 message: str=None): | |
99 if messageJson is not None: | |
100 message = json.dumps(messageJson) | |
101 self.settings.mqtt.publish( | |
102 topic.encode('ascii'), | |
103 message.encode('ascii')) | |
104 | |
105 | |
64 if __name__ == '__main__': | 106 if __name__ == '__main__': |
65 arg = docopt(""" | 107 arg = docopt(""" |
66 Usage: mqtt_graph_bridge.py [options] | 108 Usage: mqtt_graph_bridge.py [options] |
67 | 109 |
68 -v Verbose | 110 -v Verbose |