Mercurial > code > home > repos > light-bridge
annotate light.py @ 23:7d9a056e29fe
esp rgbw output; cleanup
author | drewp@bigasterisk.com |
---|---|
date | Mon, 29 Jan 2024 23:41:27 -0800 |
parents | 178e020289c1 |
children | cee43f550577 |
rev | line source |
---|---|
2 | 1 import asyncio |
2 import logging | |
13
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
3 from dataclasses import dataclass |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
4 from typing import Callable |
2 | 5 |
5
7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents:
4
diff
changeset
|
6 from color import Color |
21 | 7 from color_convert import DeviceColor, brightnessConv, ikeaWhiteConv, oneWhiteConv, relayConv, twoWhitesConv, zbConv |
14
e3dbd04dab96
add mqtt; talk to first light (no throttling)
drewp@bigasterisk.com
parents:
13
diff
changeset
|
8 from mqtt_io import MqttIo |
23 | 9 from protocols import ShellyGen1WebTransport, SonoffRelayTransport, TasmotaWebTransport, Transport, ZigbeeTransport, espColorMessage, zbBrightnessMessage, zbRelayMessage, zbWhiteSpectrumMessage |
5
7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents:
4
diff
changeset
|
10 |
20 | 11 log = logging.getLogger('lite') |
2 | 12 |
13 | |
14 @dataclass | |
15 class Light: | |
16 name: str | |
17 | 17 transport: Transport |
18 convertColor: Callable[[Color], DeviceColor] | |
9 | 19 |
20 requestingColor: Color = Color.fromHex('#000000') | |
21 requestingDeviceColor: DeviceColor = DeviceColor() | |
22 | |
23 emittingColor: Color = Color.fromHex('#000000') | |
24 online: bool | None = None | |
25 latencyMs: float | None = None | |
26 | |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
27 notifyChanged: Callable | None = None |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
28 |
9 | 29 def __post_init__(self): |
17 | 30 self.requestingDeviceColor = self.convertColor(self.requestingColor) |
2 | 31 |
5
7eeda7f4f9cd
spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents:
4
diff
changeset
|
32 def to_dict(self): |
9 | 33 d = { |
34 'name': self.name, | |
17 | 35 'address': self.transport.linked(), |
9 | 36 'requestingColor': self.requestingColor.hex(), |
37 'requestingDeviceColor': self.requestingDeviceColor.summary(), | |
38 'emittingColor': self.emittingColor.hex(), | |
39 'online': self.online, | |
40 'latencyMs': self.latencyMs, | |
2 | 41 } |
42 | |
9 | 43 return {'light': d} |
44 | |
45 async def setColor(self, c: Color): | |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
46 if c == self.requestingColor: |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
47 return |
9 | 48 self.requestingColor = c |
20 | 49 dc = self.convertColor(self.requestingColor) |
50 | |
51 log.info(f'setColor from {self.requestingColor} to {c} = {dc.summary()=}') | |
52 if dc != self.requestingDeviceColor: | |
53 self.requestingDeviceColor = dc | |
14
e3dbd04dab96
add mqtt; talk to first light (no throttling)
drewp@bigasterisk.com
parents:
13
diff
changeset
|
54 |
20 | 55 if self.notifyChanged: |
56 self.notifyChanged() | |
14
e3dbd04dab96
add mqtt; talk to first light (no throttling)
drewp@bigasterisk.com
parents:
13
diff
changeset
|
57 |
20 | 58 # waits for the relevant round-trip |
59 log.info(f'transport send {self.requestingDeviceColor.summary()}') | |
60 await self.transport.send(self.requestingDeviceColor) | |
14
e3dbd04dab96
add mqtt; talk to first light (no throttling)
drewp@bigasterisk.com
parents:
13
diff
changeset
|
61 |
e3dbd04dab96
add mqtt; talk to first light (no throttling)
drewp@bigasterisk.com
parents:
13
diff
changeset
|
62 self.emittingColor = self.requestingColor |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
63 if self.notifyChanged: |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
64 self.notifyChanged() |
9 | 65 |
2 | 66 |
14
e3dbd04dab96
add mqtt; talk to first light (no throttling)
drewp@bigasterisk.com
parents:
13
diff
changeset
|
67 def makeZbBar(mqtt: MqttIo, name: str, ieee: str) -> Light: |
17 | 68 return Light(name=name, convertColor=zbConv, transport=ZigbeeTransport(mqtt, name, ieee)) |
13
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
69 |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
70 |
20 | 71 def makeTasmota(name: str, hostname: str) -> Light: |
72 return Light(name=name, convertColor=twoWhitesConv, transport=TasmotaWebTransport(hostname)) | |
73 | |
74 | |
75 def makeShellyRGW2(name: str, hostname: str) -> Light: | |
76 return Light(name=name, convertColor=oneWhiteConv, transport=ShellyGen1WebTransport(hostname)) | |
77 | |
78 | |
79 def makeSonoffRelay(mqtt: MqttIo, name: str, topic: str) -> Light: | |
80 return Light(name=name, convertColor=relayConv, transport=SonoffRelayTransport(mqtt, topic)) | |
81 | |
82 | |
21 | 83 def makeZbIkeaWhiteTemp(mqtt: MqttIo, name: str, ieee: str) -> Light: |
84 return Light(name=name, convertColor=ikeaWhiteConv, transport=ZigbeeTransport(mqtt, name, ieee, msg=zbWhiteSpectrumMessage)) | |
85 | |
86 | |
87 def makeZbBrightness(mqtt: MqttIo, name: str, ieee: str) -> Light: | |
88 return Light(name=name, convertColor=brightnessConv, transport=ZigbeeTransport(mqtt, name, ieee, msg=zbBrightnessMessage)) | |
89 | |
90 | |
91 def makeZbRelay(mqtt: MqttIo, name: str, ieee: str) -> Light: | |
92 return Light(name=name, convertColor=relayConv, transport=ZigbeeTransport(mqtt, name, ieee, msg=zbRelayMessage)) | |
93 | |
94 | |
95 def makeEspBrightness(mqtt: MqttIo, name: str, topicPrefix: str) -> Light: | |
96 return Light(name=name, | |
97 convertColor=brightnessConv, | |
98 transport=ZigbeeTransport(mqtt, name, '', topic=lambda *arg: topicPrefix + '/command', msg=zbBrightnessMessage)) | |
99 | |
100 | |
23 | 101 def makeEspRgbw(mqtt: MqttIo, name: str, topicPrefix: str) -> Light: |
102 return Light(name=name, | |
103 convertColor=oneWhiteConv, | |
104 transport=ZigbeeTransport(mqtt, name, '', topic=lambda *arg: topicPrefix + '/command', msg=espColorMessage)) | |
105 | |
106 | |
2 | 107 class Lights: |
108 _d: dict[str, Light] = {} | |
109 | |
14
e3dbd04dab96
add mqtt; talk to first light (no throttling)
drewp@bigasterisk.com
parents:
13
diff
changeset
|
110 def __init__(self, mqtt: MqttIo): |
22 | 111 # todo: combine mqtt, aiohttp session, and pigpiod client into some |
112 # Transports object | |
14
e3dbd04dab96
add mqtt; talk to first light (no throttling)
drewp@bigasterisk.com
parents:
13
diff
changeset
|
113 self.mqtt = mqtt |
17 | 114 |
14
e3dbd04dab96
add mqtt; talk to first light (no throttling)
drewp@bigasterisk.com
parents:
13
diff
changeset
|
115 self.add(makeZbBar(mqtt, 'do-bar', '0xa4c13844948d2da4')) |
20 | 116 self.add(makeTasmota('do-lamp', 'tasmota-9E2AB7-2743')) |
21 | 117 self.add(makeTasmota('li-high-shelf', 'light-li-ceil')) |
118 self.add(makeTasmota('tr-door', 'light-tr-door')) | |
20 | 119 self.add(makeShellyRGW2('ki-ceiling', 'shellyrgbw2-e868e7f34c35')) |
120 self.add(makeShellyRGW2('ki-counter', 'shellyrgbw2-e868e7f34cb2')) | |
121 | |
21 | 122 self.add(makeZbIkeaWhiteTemp(mqtt, 'br-floor', '0x000b57fffedabd20')) |
123 self.add(makeZbIkeaWhiteTemp(mqtt, 'br-wall', '0x14b457fffe2dab6e')) | |
124 self.add(makeZbIkeaWhiteTemp(mqtt, 'en', '0x000b57fffe988959')) | |
125 self.add(makeZbIkeaWhiteTemp(mqtt, 'py', '0x000b57fffeaf42cd')) | |
126 self.add(makeZbIkeaWhiteTemp(mqtt, 'rr-lamp', '0x000b57fffe32e5a5')) | |
20 | 127 |
21 | 128 self.add(makeZbBrightness(mqtt, 'go-high', '0x847127fffebb3efa')) |
129 | |
130 self.add(makeZbRelay(mqtt, 'ws-hanging', '0xd0cf5efffe720b46')) | |
20 | 131 |
132 self.add(makeSonoffRelay(mqtt, 'li-lamp0', 'sonoff_0')) | |
133 self.add(makeSonoffRelay(mqtt, 'li-lamp1', 'sonoff_1')) | |
134 self.add(makeSonoffRelay(mqtt, 'li-lamp2', 'sonoff_2')) | |
135 self.add(makeSonoffRelay(mqtt, 'li-lamp3', 'sonoff_3')) | |
136 self.add(makeSonoffRelay(mqtt, 'li-lamp4', 'sonoff_4')) | |
6 | 137 |
21 | 138 self.add(makeEspBrightness(mqtt, 'ws-high0', 'workshop/light/high0')) |
139 self.add(makeEspBrightness(mqtt, 'ws-high1', 'workshop/light/high1')) | |
140 self.add(makeEspBrightness(mqtt, 'ws-high2', 'workshop/light/high2')) | |
141 self.add(makeEspBrightness(mqtt, 'ws-high3', 'workshop/light/high3')) | |
142 self.add(makeEspBrightness(mqtt, 'ws-kid', 'workshop/light/kid')) | |
143 self.add(makeEspBrightness(mqtt, 'ws-sewing', 'workshop/light/sewing')) | |
144 | |
23 | 145 self.add(makeEspRgbw(mqtt, 'br-headboard', 'bed/light/headboard')) |
146 | |
21 | 147 # ft-ceil |
148 # li-toys | |
149 # sh-top | |
150 # light-sh | |
151 # ga-hanging | |
152 | |
153 # wled: | |
154 # string-tr | |
155 # string-hr | |
156 # light-tr-ball | |
157 # wled-ft-hanging | |
158 | |
2 | 159 def add(self, d: Light): |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
160 d.notifyChanged = self.notifyChanged |
2 | 161 self._d[d.name] = d |
162 | |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
163 self.notifyChanged() |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
164 |
2 | 165 def byName(self, name: str) -> Light: |
166 return self._d[name] | |
167 | |
12 | 168 def to_dict(self): |
169 return {'lights': [d.to_dict() for d in sorted(self._d.values(), key=lambda r: r.name)]} | |
170 | |
171 # the following is bad. Get a better events lib. | |
172 _changeSleepTask: asyncio.Task | None = None | |
173 | |
2 | 174 async def changes(self): # yields None on any data change |
175 while True: | |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
176 log.info('Lights has a change') |
2 | 177 yield None |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
178 self._changeSleepTask = asyncio.create_task(self._sleep()) |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
179 try: |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
180 await self._changeSleepTask |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
181 except asyncio.CancelledError: |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
182 pass |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
183 self._changeSleepTask = None |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
184 |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
185 async def _sleep(self): |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
186 await asyncio.sleep(100) |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
187 |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
188 def notifyChanged(self): |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
189 log.info('Lights.notifyChanged()') |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
190 if self._changeSleepTask is not None: |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
191 self._changeSleepTask.cancel() |