Mercurial > code > home > repos > light-bridge
annotate light.py @ 28:fb2e91f230f4
new wled mode and more lights
author | drewp@bigasterisk.com |
---|---|
date | Mon, 02 Sep 2024 20:00:53 -0700 |
parents | 33b3eb24506e |
children | 35affd4d37d4 |
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 |
28 | 9 from protocols import ShellyGen1WebTransport, SonoffRelayTransport, TasmotaWebTransport, Transport, WledControl, WledTransport, 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 | |
26
33b3eb24506e
wled (single bulb) support. more lights
drewp@bigasterisk.com
parents:
25
diff
changeset
|
107 def makeWledSingleBulb(name: str, hostname: str) -> Light: |
28 | 108 return Light(name=name, convertColor=twoWhitesConv, transport=WledTransport(hostname, WledControl.wholeStringColor)) |
26
33b3eb24506e
wled (single bulb) support. more lights
drewp@bigasterisk.com
parents:
25
diff
changeset
|
109 |
28 | 110 def makeWledStringBrightness(name: str, hostname: str) -> Light: |
111 return Light(name=name, convertColor=brightnessConv, transport=WledTransport(hostname, WledControl.brightness)) | |
26
33b3eb24506e
wled (single bulb) support. more lights
drewp@bigasterisk.com
parents:
25
diff
changeset
|
112 |
2 | 113 class Lights: |
114 _d: dict[str, Light] = {} | |
115 | |
14
e3dbd04dab96
add mqtt; talk to first light (no throttling)
drewp@bigasterisk.com
parents:
13
diff
changeset
|
116 def __init__(self, mqtt: MqttIo): |
22 | 117 # todo: combine mqtt, aiohttp session, and pigpiod client into some |
118 # Transports object | |
14
e3dbd04dab96
add mqtt; talk to first light (no throttling)
drewp@bigasterisk.com
parents:
13
diff
changeset
|
119 self.mqtt = mqtt |
17 | 120 |
14
e3dbd04dab96
add mqtt; talk to first light (no throttling)
drewp@bigasterisk.com
parents:
13
diff
changeset
|
121 self.add(makeZbBar(mqtt, 'do-bar', '0xa4c13844948d2da4')) |
20 | 122 self.add(makeTasmota('do-lamp', 'tasmota-9E2AB7-2743')) |
21 | 123 self.add(makeTasmota('li-high-shelf', 'light-li-ceil')) |
124 self.add(makeTasmota('tr-door', 'light-tr-door')) | |
28 | 125 |
26
33b3eb24506e
wled (single bulb) support. more lights
drewp@bigasterisk.com
parents:
25
diff
changeset
|
126 self.add(makeWledSingleBulb('ft-hanging', 'light-ft-ceil')) |
28 | 127 self.add(makeWledSingleBulb('tr-ball', 'light-tr-ball')) |
128 self.add(makeWledStringBrightness('hr-string', 'light-hr-string')) | |
129 self.add(makeWledStringBrightness('tr-string', 'light-tr-string')) | |
26
33b3eb24506e
wled (single bulb) support. more lights
drewp@bigasterisk.com
parents:
25
diff
changeset
|
130 |
20 | 131 self.add(makeShellyRGW2('ki-ceiling', 'shellyrgbw2-e868e7f34c35')) |
132 self.add(makeShellyRGW2('ki-counter', 'shellyrgbw2-e868e7f34cb2')) | |
133 | |
21 | 134 self.add(makeZbIkeaWhiteTemp(mqtt, 'br-floor', '0x000b57fffedabd20')) |
135 self.add(makeZbIkeaWhiteTemp(mqtt, 'br-wall', '0x14b457fffe2dab6e')) | |
136 self.add(makeZbIkeaWhiteTemp(mqtt, 'en', '0x000b57fffe988959')) | |
137 self.add(makeZbIkeaWhiteTemp(mqtt, 'py', '0x000b57fffeaf42cd')) | |
138 self.add(makeZbIkeaWhiteTemp(mqtt, 'rr-lamp', '0x000b57fffe32e5a5')) | |
26
33b3eb24506e
wled (single bulb) support. more lights
drewp@bigasterisk.com
parents:
25
diff
changeset
|
139 self.add(makeZbIkeaWhiteTemp(mqtt, 'ft-ceiling', '0xd0cf5efffe28abcf')) |
28 | 140 self.add(makeZbIkeaWhiteTemp(mqtt, 'di-ceiling', '0x000b57fffe8c0ad4')) |
20 | 141 |
21 | 142 self.add(makeZbBrightness(mqtt, 'go-high', '0x847127fffebb3efa')) |
143 | |
144 self.add(makeZbRelay(mqtt, 'ws-hanging', '0xd0cf5efffe720b46')) | |
20 | 145 |
146 self.add(makeSonoffRelay(mqtt, 'li-lamp0', 'sonoff_0')) | |
147 self.add(makeSonoffRelay(mqtt, 'li-lamp1', 'sonoff_1')) | |
148 self.add(makeSonoffRelay(mqtt, 'li-lamp2', 'sonoff_2')) | |
149 self.add(makeSonoffRelay(mqtt, 'li-lamp3', 'sonoff_3')) | |
150 self.add(makeSonoffRelay(mqtt, 'li-lamp4', 'sonoff_4')) | |
6 | 151 |
21 | 152 self.add(makeEspBrightness(mqtt, 'ws-high0', 'workshop/light/high0')) |
153 self.add(makeEspBrightness(mqtt, 'ws-high1', 'workshop/light/high1')) | |
154 self.add(makeEspBrightness(mqtt, 'ws-high2', 'workshop/light/high2')) | |
155 self.add(makeEspBrightness(mqtt, 'ws-high3', 'workshop/light/high3')) | |
156 self.add(makeEspBrightness(mqtt, 'ws-kid', 'workshop/light/kid')) | |
157 self.add(makeEspBrightness(mqtt, 'ws-sewing', 'workshop/light/sewing')) | |
158 | |
23 | 159 self.add(makeEspRgbw(mqtt, 'br-headboard', 'bed/light/headboard')) |
160 | |
21 | 161 # ft-ceil |
162 # li-toys | |
163 # sh-top | |
164 # light-sh | |
165 # ga-hanging | |
166 | |
167 # wled: | |
168 # string-tr | |
169 # string-hr | |
170 # light-tr-ball | |
171 # wled-ft-hanging | |
172 | |
2 | 173 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
|
174 d.notifyChanged = self.notifyChanged |
2 | 175 self._d[d.name] = d |
176 | |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
177 self.notifyChanged() |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
178 |
25 | 179 def allNames(self) -> list[str]: |
180 return list(self._d.keys()) | |
181 | |
2 | 182 def byName(self, name: str) -> Light: |
183 return self._d[name] | |
184 | |
12 | 185 def to_dict(self): |
186 return {'lights': [d.to_dict() for d in sorted(self._d.values(), key=lambda r: r.name)]} | |
187 | |
188 # the following is bad. Get a better events lib. | |
189 _changeSleepTask: asyncio.Task | None = None | |
190 | |
2 | 191 async def changes(self): # yields None on any data change |
192 while True: | |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
193 log.info('Lights has a change') |
2 | 194 yield None |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
195 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
|
196 try: |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
197 await self._changeSleepTask |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
198 except asyncio.CancelledError: |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
199 pass |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
200 self._changeSleepTask = None |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
201 |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
202 async def _sleep(self): |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
203 await asyncio.sleep(100) |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
204 |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
205 def notifyChanged(self): |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
206 log.info('Lights.notifyChanged()') |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
207 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
|
208 self._changeSleepTask.cancel() |