annotate light.py @ 29:35affd4d37d4 default tip

add 1st ikea color light
author drewp@bigasterisk.com
date Sat, 14 Dec 2024 22:36:29 -0800
parents fb2e91f230f4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
1 import asyncio
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
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
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
5
5
7eeda7f4f9cd spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents: 4
diff changeset
6 from color import Color
21
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
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
29
35affd4d37d4 add 1st ikea color light
drewp@bigasterisk.com
parents: 28
diff changeset
9 from protocols import ShellyGen1WebTransport, SonoffRelayTransport, TasmotaWebTransport, Transport, WledControl, WledTransport, ZigbeeTransport, espColorMessage, zbBrightnessMessage, zbColorMessage, zbRelayMessage, zbWhiteSpectrumMessage
5
7eeda7f4f9cd spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents: 4
diff changeset
10
20
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
11 log = logging.getLogger('lite')
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
12
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
13
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
14 @dataclass
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
15 class Light:
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
16 name: str
17
1d15dc4d673f cleanup color convert code path
drewp@bigasterisk.com
parents: 15
diff changeset
17 transport: Transport
1d15dc4d673f cleanup color convert code path
drewp@bigasterisk.com
parents: 15
diff changeset
18 convertColor: Callable[[Color], DeviceColor]
9
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
19
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
20 requestingColor: Color = Color.fromHex('#000000')
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
21 requestingDeviceColor: DeviceColor = DeviceColor()
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
22
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
23 emittingColor: Color = Color.fromHex('#000000')
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
24 online: bool | None = None
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
25 latencyMs: float | None = None
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
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
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
29 def __post_init__(self):
17
1d15dc4d673f cleanup color convert code path
drewp@bigasterisk.com
parents: 15
diff changeset
30 self.requestingDeviceColor = self.convertColor(self.requestingColor)
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
31
5
7eeda7f4f9cd spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents: 4
diff changeset
32 def to_dict(self):
9
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
33 d = {
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
34 'name': self.name,
17
1d15dc4d673f cleanup color convert code path
drewp@bigasterisk.com
parents: 15
diff changeset
35 'address': self.transport.linked(),
9
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
36 'requestingColor': self.requestingColor.hex(),
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
37 'requestingDeviceColor': self.requestingDeviceColor.summary(),
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
38 'emittingColor': self.emittingColor.hex(),
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
39 'online': self.online,
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
40 'latencyMs': self.latencyMs,
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
41 }
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
42
9
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
43 return {'light': d}
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
44
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
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
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
48 self.requestingColor = c
20
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
49 dc = self.convertColor(self.requestingColor)
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
50
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
51 log.info(f'setColor from {self.requestingColor} to {c} = {dc.summary()=}')
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
52 if dc != self.requestingDeviceColor:
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
53 self.requestingDeviceColor = dc
14
e3dbd04dab96 add mqtt; talk to first light (no throttling)
drewp@bigasterisk.com
parents: 13
diff changeset
54
20
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
55 if self.notifyChanged:
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
56 self.notifyChanged()
14
e3dbd04dab96 add mqtt; talk to first light (no throttling)
drewp@bigasterisk.com
parents: 13
diff changeset
57
20
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
58 # waits for the relevant round-trip
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
59 log.info(f'transport send {self.requestingDeviceColor.summary()}')
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
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
9f427d8073c3 redo data model; add ui colors
drewp@bigasterisk.com
parents: 6
diff changeset
65
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
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
1d15dc4d673f cleanup color convert code path
drewp@bigasterisk.com
parents: 15
diff changeset
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
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
71 def makeTasmota(name: str, hostname: str) -> Light:
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
72 return Light(name=name, convertColor=twoWhitesConv, transport=TasmotaWebTransport(hostname))
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
73
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
74
29
35affd4d37d4 add 1st ikea color light
drewp@bigasterisk.com
parents: 28
diff changeset
75 def makeShellyRGBWW(name: str, hostname: str) -> Light:
20
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
76 return Light(name=name, convertColor=oneWhiteConv, transport=ShellyGen1WebTransport(hostname))
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
77
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
78
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
79 def makeSonoffRelay(mqtt: MqttIo, name: str, topic: str) -> Light:
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
80 return Light(name=name, convertColor=relayConv, transport=SonoffRelayTransport(mqtt, topic))
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
81
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
82
21
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
83 def makeZbIkeaWhiteTemp(mqtt: MqttIo, name: str, ieee: str) -> Light:
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
84 return Light(name=name, convertColor=ikeaWhiteConv, transport=ZigbeeTransport(mqtt, name, ieee, msg=zbWhiteSpectrumMessage))
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
85
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
86
29
35affd4d37d4 add 1st ikea color light
drewp@bigasterisk.com
parents: 28
diff changeset
87 def makeZbIkeaRGBWW(mqtt: MqttIo, name: str, ieee: str) -> Light:
35affd4d37d4 add 1st ikea color light
drewp@bigasterisk.com
parents: 28
diff changeset
88 return Light(name=name, convertColor=zbConv, transport=ZigbeeTransport(mqtt, name, ieee, msg=zbColorMessage))
35affd4d37d4 add 1st ikea color light
drewp@bigasterisk.com
parents: 28
diff changeset
89
35affd4d37d4 add 1st ikea color light
drewp@bigasterisk.com
parents: 28
diff changeset
90
21
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
91 def makeZbBrightness(mqtt: MqttIo, name: str, ieee: str) -> Light:
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
92 return Light(name=name, convertColor=brightnessConv, transport=ZigbeeTransport(mqtt, name, ieee, msg=zbBrightnessMessage))
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
93
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
94
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
95 def makeZbRelay(mqtt: MqttIo, name: str, ieee: str) -> Light:
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
96 return Light(name=name, convertColor=relayConv, transport=ZigbeeTransport(mqtt, name, ieee, msg=zbRelayMessage))
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
97
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
98
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
99 def makeEspBrightness(mqtt: MqttIo, name: str, topicPrefix: str) -> Light:
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
100 return Light(name=name,
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
101 convertColor=brightnessConv,
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
102 transport=ZigbeeTransport(mqtt, name, '', topic=lambda *arg: topicPrefix + '/command', msg=zbBrightnessMessage))
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
103
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
104
23
7d9a056e29fe esp rgbw output; cleanup
drewp@bigasterisk.com
parents: 22
diff changeset
105 def makeEspRgbw(mqtt: MqttIo, name: str, topicPrefix: str) -> Light:
7d9a056e29fe esp rgbw output; cleanup
drewp@bigasterisk.com
parents: 22
diff changeset
106 return Light(name=name,
7d9a056e29fe esp rgbw output; cleanup
drewp@bigasterisk.com
parents: 22
diff changeset
107 convertColor=oneWhiteConv,
7d9a056e29fe esp rgbw output; cleanup
drewp@bigasterisk.com
parents: 22
diff changeset
108 transport=ZigbeeTransport(mqtt, name, '', topic=lambda *arg: topicPrefix + '/command', msg=espColorMessage))
7d9a056e29fe esp rgbw output; cleanup
drewp@bigasterisk.com
parents: 22
diff changeset
109
7d9a056e29fe esp rgbw output; cleanup
drewp@bigasterisk.com
parents: 22
diff changeset
110
26
33b3eb24506e wled (single bulb) support. more lights
drewp@bigasterisk.com
parents: 25
diff changeset
111 def makeWledSingleBulb(name: str, hostname: str) -> Light:
28
fb2e91f230f4 new wled mode and more lights
drewp@bigasterisk.com
parents: 26
diff changeset
112 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
113
28
fb2e91f230f4 new wled mode and more lights
drewp@bigasterisk.com
parents: 26
diff changeset
114 def makeWledStringBrightness(name: str, hostname: str) -> Light:
fb2e91f230f4 new wled mode and more lights
drewp@bigasterisk.com
parents: 26
diff changeset
115 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
116
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
117 class Lights:
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
118 _d: dict[str, Light] = {}
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
119
14
e3dbd04dab96 add mqtt; talk to first light (no throttling)
drewp@bigasterisk.com
parents: 13
diff changeset
120 def __init__(self, mqtt: MqttIo):
22
178e020289c1 'full' button; other clean up
drewp@bigasterisk.com
parents: 21
diff changeset
121 # todo: combine mqtt, aiohttp session, and pigpiod client into some
178e020289c1 'full' button; other clean up
drewp@bigasterisk.com
parents: 21
diff changeset
122 # Transports object
14
e3dbd04dab96 add mqtt; talk to first light (no throttling)
drewp@bigasterisk.com
parents: 13
diff changeset
123 self.mqtt = mqtt
17
1d15dc4d673f cleanup color convert code path
drewp@bigasterisk.com
parents: 15
diff changeset
124
14
e3dbd04dab96 add mqtt; talk to first light (no throttling)
drewp@bigasterisk.com
parents: 13
diff changeset
125 self.add(makeZbBar(mqtt, 'do-bar', '0xa4c13844948d2da4'))
20
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
126 self.add(makeTasmota('do-lamp', 'tasmota-9E2AB7-2743'))
29
35affd4d37d4 add 1st ikea color light
drewp@bigasterisk.com
parents: 28
diff changeset
127 self.add(makeTasmota('li-high-shelf', 'li-high-shelf')) # bulb labeled 'L3'. athom. broken wifi.
21
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
128 self.add(makeTasmota('tr-door', 'light-tr-door'))
28
fb2e91f230f4 new wled mode and more lights
drewp@bigasterisk.com
parents: 26
diff changeset
129
26
33b3eb24506e wled (single bulb) support. more lights
drewp@bigasterisk.com
parents: 25
diff changeset
130 self.add(makeWledSingleBulb('ft-hanging', 'light-ft-ceil'))
28
fb2e91f230f4 new wled mode and more lights
drewp@bigasterisk.com
parents: 26
diff changeset
131 self.add(makeWledSingleBulb('tr-ball', 'light-tr-ball'))
fb2e91f230f4 new wled mode and more lights
drewp@bigasterisk.com
parents: 26
diff changeset
132 self.add(makeWledStringBrightness('hr-string', 'light-hr-string'))
fb2e91f230f4 new wled mode and more lights
drewp@bigasterisk.com
parents: 26
diff changeset
133 self.add(makeWledStringBrightness('tr-string', 'light-tr-string'))
26
33b3eb24506e wled (single bulb) support. more lights
drewp@bigasterisk.com
parents: 25
diff changeset
134
29
35affd4d37d4 add 1st ikea color light
drewp@bigasterisk.com
parents: 28
diff changeset
135 self.add(makeShellyRGBWW('ki-ceiling', 'shellyrgbw2-e868e7f34c35'))
35affd4d37d4 add 1st ikea color light
drewp@bigasterisk.com
parents: 28
diff changeset
136 self.add(makeShellyRGBWW('ki-counter', 'shellyrgbw2-e868e7f34cb2'))
20
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
137
21
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
138 self.add(makeZbIkeaWhiteTemp(mqtt, 'br-floor', '0x000b57fffedabd20'))
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
139 self.add(makeZbIkeaWhiteTemp(mqtt, 'br-wall', '0x14b457fffe2dab6e'))
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
140 self.add(makeZbIkeaWhiteTemp(mqtt, 'en', '0x000b57fffe988959'))
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
141 self.add(makeZbIkeaWhiteTemp(mqtt, 'py', '0x000b57fffeaf42cd'))
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
142 self.add(makeZbIkeaWhiteTemp(mqtt, 'rr-lamp', '0x000b57fffe32e5a5'))
26
33b3eb24506e wled (single bulb) support. more lights
drewp@bigasterisk.com
parents: 25
diff changeset
143 self.add(makeZbIkeaWhiteTemp(mqtt, 'ft-ceiling', '0xd0cf5efffe28abcf'))
29
35affd4d37d4 add 1st ikea color light
drewp@bigasterisk.com
parents: 28
diff changeset
144 self.add(makeZbIkeaWhiteTemp(mqtt, 'di-ceiling', '0x000b57fffe8c0ad4'))
35affd4d37d4 add 1st ikea color light
drewp@bigasterisk.com
parents: 28
diff changeset
145 self.add(makeZbIkeaRGBWW(mqtt, 'li-high-shelf4', '0xd0cf5efffe158e49'))
20
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
146
21
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
147 self.add(makeZbBrightness(mqtt, 'go-high', '0x847127fffebb3efa'))
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
148
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
149 self.add(makeZbRelay(mqtt, 'ws-hanging', '0xd0cf5efffe720b46'))
20
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
150
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
151 self.add(makeSonoffRelay(mqtt, 'li-lamp0', 'sonoff_0'))
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
152 self.add(makeSonoffRelay(mqtt, 'li-lamp1', 'sonoff_1'))
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
153 self.add(makeSonoffRelay(mqtt, 'li-lamp2', 'sonoff_2'))
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
154 self.add(makeSonoffRelay(mqtt, 'li-lamp3', 'sonoff_3'))
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 17
diff changeset
155 self.add(makeSonoffRelay(mqtt, 'li-lamp4', 'sonoff_4'))
6
fc8ed0efcd72 move init to Lights
drewp@bigasterisk.com
parents: 5
diff changeset
156
21
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
157 self.add(makeEspBrightness(mqtt, 'ws-high0', 'workshop/light/high0'))
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
158 self.add(makeEspBrightness(mqtt, 'ws-high1', 'workshop/light/high1'))
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
159 self.add(makeEspBrightness(mqtt, 'ws-high2', 'workshop/light/high2'))
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
160 self.add(makeEspBrightness(mqtt, 'ws-high3', 'workshop/light/high3'))
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
161 self.add(makeEspBrightness(mqtt, 'ws-kid', 'workshop/light/kid'))
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
162 self.add(makeEspBrightness(mqtt, 'ws-sewing', 'workshop/light/sewing'))
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
163
23
7d9a056e29fe esp rgbw output; cleanup
drewp@bigasterisk.com
parents: 22
diff changeset
164 self.add(makeEspRgbw(mqtt, 'br-headboard', 'bed/light/headboard'))
7d9a056e29fe esp rgbw output; cleanup
drewp@bigasterisk.com
parents: 22
diff changeset
165
21
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
166 # ft-ceil
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
167 # li-toys
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
168 # sh-top
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
169 # light-sh
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
170 # ga-hanging
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
171
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
172 # wled:
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
173 # string-tr
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
174 # string-hr
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
175 # light-tr-ball
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
176 # wled-ft-hanging
b8201490c731 more light types
drewp@bigasterisk.com
parents: 20
diff changeset
177
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
178 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
179 d.notifyChanged = self.notifyChanged
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
180 self._d[d.name] = d
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
181
11
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
182 self.notifyChanged()
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
183
25
cee43f550577 add /lightNames
drewp@bigasterisk.com
parents: 23
diff changeset
184 def allNames(self) -> list[str]:
cee43f550577 add /lightNames
drewp@bigasterisk.com
parents: 23
diff changeset
185 return list(self._d.keys())
cee43f550577 add /lightNames
drewp@bigasterisk.com
parents: 23
diff changeset
186
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
187 def byName(self, name: str) -> Light:
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
188 return self._d[name]
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
189
12
7cc004eafb82 refactor (approx)
drewp@bigasterisk.com
parents: 11
diff changeset
190 def to_dict(self):
7cc004eafb82 refactor (approx)
drewp@bigasterisk.com
parents: 11
diff changeset
191 return {'lights': [d.to_dict() for d in sorted(self._d.values(), key=lambda r: r.name)]}
7cc004eafb82 refactor (approx)
drewp@bigasterisk.com
parents: 11
diff changeset
192
7cc004eafb82 refactor (approx)
drewp@bigasterisk.com
parents: 11
diff changeset
193 # the following is bad. Get a better events lib.
7cc004eafb82 refactor (approx)
drewp@bigasterisk.com
parents: 11
diff changeset
194 _changeSleepTask: asyncio.Task | None = None
7cc004eafb82 refactor (approx)
drewp@bigasterisk.com
parents: 11
diff changeset
195
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
196 async def changes(self): # yields None on any data change
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
197 while True:
11
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
198 log.info('Lights has a change')
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents:
diff changeset
199 yield None
11
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
200 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
201 try:
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
202 await self._changeSleepTask
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
203 except asyncio.CancelledError:
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
204 pass
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
205 self._changeSleepTask = None
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
206
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
207 async def _sleep(self):
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
208 await asyncio.sleep(100)
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
209
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
210 def notifyChanged(self):
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
211 log.info('Lights.notifyChanged()')
028ed39aa78f more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents: 9
diff changeset
212 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
213 self._changeSleepTask.cancel()