Mercurial > code > home > repos > homeauto
annotate service/rdf_to_mqtt/rdf_to_mqtt.py @ 1685:6b80a6c58907
minor changes to several services
author | drewp@bigasterisk.com |
---|---|
date | Mon, 27 Sep 2021 23:12:43 -0700 |
parents | e7eb3fc8db54 |
children | ac1ae0c21bb0 |
rev | line source |
---|---|
597
e1ee6661329a
adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp@bigasterisk.com
parents:
586
diff
changeset
|
1 """ |
e1ee6661329a
adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp@bigasterisk.com
parents:
586
diff
changeset
|
2 We get output statements that are like light9's deviceAttrs (:dev1 :color "#ff0000"), |
e1ee6661329a
adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp@bigasterisk.com
parents:
586
diff
changeset
|
3 convert those to outputAttrs (:dev1 :red 255; :green 0; :blue 0) and post them to mqtt. |
e1ee6661329a
adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp@bigasterisk.com
parents:
586
diff
changeset
|
4 |
e1ee6661329a
adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp@bigasterisk.com
parents:
586
diff
changeset
|
5 This is like light9/bin/collector. |
e1ee6661329a
adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp@bigasterisk.com
parents:
586
diff
changeset
|
6 """ |
581
30022797642e
mqtt_graph_bridge to new build rules and to py3
drewp@bigasterisk.com
parents:
460
diff
changeset
|
7 import json |
777 | 8 |
9 import cyclone.web | |
10 from cycloneerr import PrettyErrorHandler | |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
11 from docopt import docopt |
762 | 12 from greplin import scales |
13 from greplin.scales.cyclonehandler import StatsHandler | |
777 | 14 from mqtt_client import MqttClient |
15 from rdflib import Namespace | |
581
30022797642e
mqtt_graph_bridge to new build rules and to py3
drewp@bigasterisk.com
parents:
460
diff
changeset
|
16 from standardservice.logsetup import log, verboseLogging |
777 | 17 from twisted.internet import reactor |
18 | |
694
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
19 import rdf_over_http |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
20 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
21 ROOM = Namespace('http://projects.bigasterisk.com/room/') |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
22 |
777 | 23 STATS = scales.collection( |
24 '/root', | |
25 scales.PmfStat('putRequests'), | |
26 scales.PmfStat('statement'), | |
27 scales.PmfStat('mqttPublish'), | |
762 | 28 ) |
29 | |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
30 devs = { |
460
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
31 ROOM['kitchenLight']: { |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
32 'root': 'h801_skylight', |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
33 'hasWhite': True, |
460
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
34 }, |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
35 ROOM['kitchenCounterLight']: { |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
36 'root': 'h801_counter', |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
37 'hasWhite': True, |
460
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
38 }, |
1685 | 39 ROOM['livingLampShelf']: { 'root': 'sonoff_0/switch/sonoff_basic_relay/command', 'values': 'binary', }, |
40 ROOM['livingLampMantleEntry']: { 'root': 'sonoff_1/switch/sonoff_basic_relay/command', 'values': 'binary', }, | |
41 ROOM['livingLampMantleChair']: { 'root': 'sonoff_2/switch/sonoff_basic_relay/command', 'values': 'binary', }, | |
42 ROOM['livingLampToyShelf']: { 'root': 'sonoff_3/switch/sonoff_basic_relay/command', 'values': 'binary', }, | |
43 ROOM['livingLampPiano']: { 'root': 'sonoff_4/switch/sonoff_basic_relay/command', 'values': 'binary', }, | |
761 | 44 ROOM['theater']: { |
45 'root': 'theater_blaster/ir_out', | |
46 'values': 'theaterOutputs', | |
47 }, | |
776 | 48 ROOM['bedHeadboard']: { |
49 'root': 'bed/light/headboard/command', | |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
50 'hasWhite': True, |
776 | 51 }, |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
52 # https://github.com/Koenkk/zigbee2mqtt.io/blob/new_api/docs/information/mqtt_topics_and_message_structure.md#general |
1685 | 53 ROOM['frontRoom1']: { 'root': 'zigbee-dash/frontRoom1/set', 'hasBrightness': True, 'defaults': { 'transition': 0, } }, |
54 ROOM['frontRoom2']: { 'root': 'zigbee-dash/frontRoom2/set', 'hasBrightness': True, 'defaults': { 'transition': 0, } }, | |
55 ROOM['asherCeiling']: { 'root': 'zigbee-frontbed/0xf0d1b80000022c86/set', 'hasBrightness': True, 'defaults': { 'transition': 0, } }, | |
56 ROOM['asherBedBar']: { 'root': 'bed_bar_asher/light/rgb/command', 'hasBrightness': True, 'defaults': {}}, | |
57 ROOM['stairTop']: { 'root': 'zigbee-dash/stairTop/set', 'hasBrightness': True, 'defaults': { 'transition': 0, } }, | |
58 ROOM['noname1']: { 'root': 'zigbee-bang/0xf0d1b8000001ffc6/set', 'hasBrightness': True, 'defaults': { 'transition': 0, } }, | |
59 ROOM['noname2']: { 'root': 'zigbee-bang/0xf0d1b80000023583/set', 'hasBrightness': True, 'defaults': { 'transition': 0, } }, | |
60 ROOM['noname3']: { 'root': 'zigbee-bang/0xf0d1b80000023708/set', 'hasBrightness': True, 'defaults': { 'transition': 0, } }, | |
61 ROOM['noname4']: { 'root': 'zigbee-bang/0xf0d1b80000022adc/set', 'hasBrightness': True, 'defaults': { 'transition': 0, } }, | |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
62 } |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
63 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
64 |
761 | 65 class OutputPage(PrettyErrorHandler, cyclone.web.RequestHandler): |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
66 |
762 | 67 @STATS.putRequests.time() |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
68 def put(self): |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
69 for stmt in rdf_over_http.rdfStatementsFromRequest(self.request.arguments, self.request.body, self.request.headers): |
694
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
70 self._onStatement(stmt) |
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
71 |
762 | 72 @STATS.statement.time() |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
73 def _onStatement(self, stmt): |
694
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
74 log.info(f'incoming statement: {stmt}') |
392
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
75 ignored = True |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
76 for dev, attrs in devs.items(): |
696
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
77 if stmt[0] == ROOM['frontWindow']: |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
78 ignored = ignored and self._publishFrontScreenText(stmt) |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
79 if stmt[0:2] == (dev, ROOM['brightness']): |
696
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
80 log.info(f'brightness request: {stmt}') |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
81 brightness = stmt[2].toPython() |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
82 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
83 if attrs.get('values', '') == 'binary': |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
84 self._publishOnOff(attrs, brightness) |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
85 else: |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
86 self._publishRgbw(attrs, brightness) |
392
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
87 ignored = False |
761 | 88 if stmt[0:2] == (dev, ROOM['inputSelector']): |
765
82bea37aeb92
fix theater input selector string assembling
drewp@bigasterisk.com
parents:
764
diff
changeset
|
89 choice = stmt[2].toPython().decode('utf8') |
82bea37aeb92
fix theater input selector string assembling
drewp@bigasterisk.com
parents:
764
diff
changeset
|
90 self._publish(topic=attrs['root'], message=f'input_{choice}') |
761 | 91 ignored = False |
92 if stmt[0:2] == (dev, ROOM['volumeChange']): | |
93 delta = int(stmt[2].toPython()) | |
94 which = 'up' if delta > 0 else 'down' | |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
95 self._publish(topic=f'theater_blaster/ir_out/volume_{which}', message=json.dumps({'timed': abs(delta)})) |
761 | 96 ignored = False |
776 | 97 if stmt[0:2] == (dev, ROOM['color']): |
98 h = stmt[2].toPython() | |
789 | 99 msg = {} |
100 if h.endswith(b'K'): # accept "0.7*2200K" (brightness 0.7) | |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
101 # see https://www.zigbee2mqtt.io/information/mqtt_topics_and_message_structure.html#zigbee2mqttfriendly_nameset |
789 | 102 bright, kelvin = map(float, h[:-1].split(b'*')) |
1685 | 103 msg['state'] = 'ON' |
789 | 104 msg["color_temp"] = round(1000000 / kelvin, 2) |
105 msg['brightness'] = int(bright * 255) # 1..20 look about the same | |
106 else: | |
107 r, g, b = int(h[1:3], 16), int(h[3:5], 16), int(h[5:7], 16) | |
108 msg = { | |
109 'state': 'ON' if r or g or b else 'OFF', | |
110 'color': { | |
111 'r': r, | |
112 'g': g, | |
113 'b': b | |
114 }, | |
1685 | 115 'brightness': max(r, g, b), |
789 | 116 } |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
117 |
789 | 118 if attrs.get('hasWhite', False): |
119 msg['white_value'] = max(r, g, b) | |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
120 msg.update(attrs.get('defaults', {})) |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
121 self._publish(topic=attrs['root'], message=json.dumps(msg)) |
777 | 122 ignored = False |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
123 |
392
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
124 if ignored: |
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
125 log.warn("ignoring %s", stmt) |
696
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
126 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
127 def _publishOnOff(self, attrs, brightness): |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
128 msg = 'OFF' |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
129 if brightness > 0: |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
130 msg = 'ON' |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
131 self._publish(topic=attrs['root'], message=msg) |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
132 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
133 def _publishRgbw(self, attrs, brightness): |
777 | 134 for chan, scale in [('w1', 1), ('r', 1), ('g', .8), ('b', .8)]: |
135 self._publish(topic=f"{attrs['root']}/light/kit_{chan}/command", | |
136 messageJson={ | |
137 'state': 'ON', | |
138 'brightness': int(brightness * 255) | |
139 }) | |
696
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
140 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
141 def _publishFrontScreenText(self, stmt): |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
142 ignored = True |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
143 for line in ['line1', 'line2', 'line3', 'line4']: |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
144 if stmt[1] == ROOM[line]: |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
145 ignored = False |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
146 self.settings.mqtt.publish(b'frontwindow/%s' % line.encode('ascii'), stmt[2].toPython()) |
696
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
147 return ignored |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
148 |
762 | 149 @STATS.mqttPublish.time() |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
150 def _publish(self, topic: str, messageJson: object = None, message: str = None): |
765
82bea37aeb92
fix theater input selector string assembling
drewp@bigasterisk.com
parents:
764
diff
changeset
|
151 log.debug(f'mqtt.publish {topic} {message} {messageJson}') |
696
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
152 if messageJson is not None: |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
153 message = json.dumps(messageJson) |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
154 self.settings.mqtt.publish(topic.encode('ascii'), message.encode('ascii')) |
696
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
155 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
156 |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
157 if __name__ == '__main__': |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
158 arg = docopt(""" |
739 | 159 Usage: rdf_to_mqtt.py [options] |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
160 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
161 -v Verbose |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
162 """) |
581
30022797642e
mqtt_graph_bridge to new build rules and to py3
drewp@bigasterisk.com
parents:
460
diff
changeset
|
163 verboseLogging(arg['-v']) |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
164 |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
165 mqtt = MqttClient(clientId='rdf_to_mqtt', brokerHost='mosquitto-ext.default.svc.cluster.local', brokerPort=1883) |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
166 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
167 port = 10008 |
777 | 168 reactor.listenTCP(port, |
169 cyclone.web.Application([ | |
170 (r"/()", cyclone.web.StaticFileHandler, { | |
171 "path": ".", | |
172 "default_filename": "index.html" | |
173 }), | |
174 (r'/output', OutputPage), | |
175 (r'/stats/(.*)', StatsHandler, { | |
176 'serverName': 'rdf_to_mqtt' | |
177 }), | |
178 ], | |
179 mqtt=mqtt, | |
180 debug=arg['-v']), | |
181 interface='::') | |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
182 log.warn('serving on %s', port) |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
183 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
184 reactor.run() |