Debian Codegraph @

tests/test_sync.py

raw symbols SCIP
1 from __future__ import annotations
2
3 import datetime
4 import unittest
5 from unittest import mock
6 from unittest.mock import patch
7
8 import pytest
9
10 import flux_led
11 from flux_led.const import (
12 COLOR_MODE_CCT,
13 COLOR_MODE_DIM,
14 COLOR_MODE_RGB,
15 COLOR_MODE_RGBW,
16 COLOR_MODE_RGBWW,
17 STATE_BLUE,
18 STATE_GREEN,
19 STATE_RED,
20 STATE_WARM_WHITE,
21 TRANSITION_GRADUAL,
22 MultiColorEffects,
23 )
24 from flux_led.pattern import PresetPattern
25 from flux_led.protocol import (
26 PROTOCOL_LEDENET_8BYTE,
27 PROTOCOL_LEDENET_8BYTE_AUTO_ON,
28 PROTOCOL_LEDENET_8BYTE_DIMMABLE_EFFECTS,
29 PROTOCOL_LEDENET_9BYTE,
30 PROTOCOL_LEDENET_9BYTE_AUTO_ON,
31 PROTOCOL_LEDENET_9BYTE_DIMMABLE_EFFECTS,
32 PROTOCOL_LEDENET_25BYTE,
33 PROTOCOL_LEDENET_ADDRESSABLE_A1,
34 PROTOCOL_LEDENET_ADDRESSABLE_A2,
35 PROTOCOL_LEDENET_ADDRESSABLE_A3,
36 PROTOCOL_LEDENET_ORIGINAL,
37 PROTOCOL_LEDENET_ORIGINAL_CCT,
38 PROTOCOL_LEDENET_SOCKET,
39 )
40 from flux_led.utils import (
41 color_temp_to_white_levels,
42 rgbcw_brightness,
43 rgbcw_to_rgbwc,
44 rgbw_brightness,
45 rgbwc_to_rgbcw,
46 rgbww_brightness,
47 scaled_color_temp_to_white_levels,
48 utils,
49 white_levels_to_color_temp,
50 white_levels_to_scaled_color_temp,
51 )
52
53 LEDENET_STATE_QUERY = b"\x81\x8a\x8b\x96"
54
55
56 class TestLight(unittest.TestCase):
57 @patch("flux_led.WifiLedBulb._send_msg")
58 @patch("flux_led.WifiLedBulb._read_msg")
59 @patch("flux_led.WifiLedBulb.connect")
60 def test_connect(self, mock_connect, mock_read, mock_send):
61 """Test setup with minimum configuration."""
62 calls = 0
63
64 def read_data(expected):
65 nonlocal calls
66 calls += 1
67 if calls == 1:
68 self.assertEqual(expected, 2)
69 return bytearray(b"\x81E")
70 if calls == 2:
71 self.assertEqual(expected, 12)
72 return bytearray(b"#a!\x10g\xffh\x00\x04\x00\xf0\x3d")
73 raise Exception
74
75 mock_read.side_effect = read_data
76 light = flux_led.WifiLedBulb("192.168.1.166")
77 assert light.color_modes == {COLOR_MODE_RGB, COLOR_MODE_DIM}
78
79 self.assertEqual(mock_read.call_count, 2)
80 self.assertEqual(mock_send.call_count, 1)
81 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
82
83 self.assertEqual(
84 light.__str__(),
85 "ON [Color: (103, 255, 104) Brightness: 100% raw state: 129,69,35,97,33,16,103,255,104,0,4,0,240,61,]",
86 )
87 self.assertEqual(light.protocol, PROTOCOL_LEDENET_8BYTE)
88 self.assertEqual(light.model_num, 0x45)
89 self.assertEqual(light.model, "Unknown Model (0x45)")
90 self.assertEqual(light.is_on, True)
91 self.assertEqual(light.mode, "color")
92 self.assertEqual(light.warm_white, 0)
93 self.assertEqual(light.brightness, 255)
94 self.assertEqual(light.getRgb(), (103, 255, 104))
95 self.assertEqual(light.rgb, (103, 255, 104))
96 self.assertEqual(light.rgb_unscaled, (103, 255, 104))
97 self.assertEqual(light.rgbwcapable, False)
98 self.assertEqual(light.device_type, flux_led.DeviceType.Bulb)
99
100 @patch("flux_led.WifiLedBulb._send_msg")
101 @patch("flux_led.WifiLedBulb._read_msg")
102 @patch("flux_led.WifiLedBulb.connect")
103 def test_rgb(self, mock_connect, mock_read, mock_send):
104 calls = 0
105
106 def read_data(expected):
107 nonlocal calls
108 calls += 1
109 if calls == 1:
110 self.assertEqual(expected, 2)
111 return bytearray(b"\x81E")
112 if calls == 2:
113 self.assertEqual(expected, 12)
114 return bytearray(b"#a!\x10g\xffh\x00\x04\x00\xf0\x3d")
115 if calls == 3:
116 self.assertEqual(expected, 14)
117 return bytearray(b"\x81E#a!\x10\x01\x19P\x00\x04\x00\xf0\xd9")
118
119 mock_read.side_effect = read_data
120 light = flux_led.WifiLedBulb("192.168.1.164")
121 self.assertEqual(light.model_num, 0x45)
122 self.assertEqual(light.model, "Unknown Model (0x45)")
123 assert light.color_modes == {COLOR_MODE_RGB, COLOR_MODE_DIM}
124
125 self.assertEqual(mock_read.call_count, 2)
126 self.assertEqual(mock_send.call_count, 1)
127 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
128
129 light.setRgb(1, 25, 80)
130 self.assertEqual(mock_read.call_count, 2)
131 self.assertEqual(mock_send.call_count, 2)
132 self.assertEqual(
133 mock_send.call_args, mock.call(bytearray(b"1\x01\x19P\x00\xf0\x0f\x9a"))
134 )
135 self.assertEqual(light.getRgb(), (1, 25, 80))
136 self.assertEqual(light.rgb, (1, 25, 80))
137 self.assertEqual(light.rgb_unscaled, (3, 80, 255))
138
139 light._transition_complete_time = 0
140 light.update_state()
141 self.assertEqual(mock_read.call_count, 3)
142 self.assertEqual(mock_send.call_count, 3)
143 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
144
145 self.assertEqual(
146 light.__str__(),
147 "ON [Color: (1, 25, 80) Brightness: 31% raw state: 129,69,35,97,33,16,1,25,80,0,4,0,240,217,]",
148 )
149 self.assertEqual(light.protocol, PROTOCOL_LEDENET_8BYTE)
150 self.assertEqual(light.is_on, True)
151 self.assertEqual(light.mode, "color")
152 self.assertEqual(light.warm_white, 0)
153 self.assertEqual(light.brightness, 80)
154 self.assertEqual(light.getRgb(), (1, 25, 80))
155 self.assertEqual(light.device_type, flux_led.DeviceType.Bulb)
156 self.assertEqual(light.version_num, 4)
157
158 @patch("flux_led.WifiLedBulb._send_msg")
159 @patch("flux_led.WifiLedBulb._read_msg")
160 @patch("flux_led.WifiLedBulb.connect")
161 def test_off_on(self, mock_connect, mock_read, mock_send):
162 calls = 0
163
164 def read_data(expected):
165 nonlocal calls
166 calls += 1
167 if calls == 1:
168 self.assertEqual(expected, 2)
169 return bytearray(b"\x81E")
170 if calls == 2:
171 self.assertEqual(expected, 12)
172 return bytearray(b"#a!\x10\x00\x00\x00\xa6\x04\x00\x0f\x34")
173 if calls == 3: # turn off response
174 self.assertEqual(expected, 4)
175 return bytearray(b"\x0fq#\xa3")
176 if calls == 4:
177 self.assertEqual(expected, 14)
178 return bytearray(b"\x81E$a!\x10\x00\x00\x00\xa6\x04\x00\x0f4")
179
180 mock_read.side_effect = read_data
181 light = flux_led.WifiLedBulb("192.168.1.164")
182 assert light.color_modes == {COLOR_MODE_RGB, COLOR_MODE_DIM}
183
184 self.assertEqual(
185 light.__str__(),
186 "ON [Warm White: 65% raw state: 129,69,35,97,33,16,0,0,0,166,4,0,15,52,]",
187 )
188 self.assertEqual(light.protocol, PROTOCOL_LEDENET_8BYTE)
189 self.assertEqual(light.is_on, True)
190 self.assertEqual(light.mode, "ww")
191 self.assertEqual(light.warm_white, 0)
192 self.assertEqual(light.brightness, 166)
193 self.assertEqual(light.getRgb(), (255, 255, 255))
194 self.assertEqual(light.rgbwcapable, False)
195 self.assertEqual(mock_read.call_count, 2)
196 self.assertEqual(mock_send.call_count, 1)
197 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
198
199 light.turnOff()
200 self.assertEqual(mock_read.call_count, 3)
201 self.assertEqual(mock_send.call_count, 2)
202 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"q$\x0f\xa4")))
203
204 light._transition_complete_time = 0
205 light.update_state()
206 self.assertEqual(mock_read.call_count, 4)
207 self.assertEqual(mock_send.call_count, 3)
208 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"\x81\x8a\x8b\x96")))
209
210 self.assertEqual(
211 light.__str__(),
212 "OFF [Warm White: 65% raw state: 129,69,36,97,33,16,0,0,0,166,4,0,15,52,]",
213 )
214 self.assertEqual(light.protocol, PROTOCOL_LEDENET_8BYTE)
215 self.assertEqual(light.is_on, False)
216 self.assertEqual(light.mode, "ww")
217 self.assertEqual(light.warm_white, 0)
218 self.assertEqual(light.brightness, 166)
219 self.assertEqual(light.getRgb(), (255, 255, 255))
220 self.assertEqual(light.rgbwcapable, False)
221 self.assertEqual(light.device_type, flux_led.DeviceType.Bulb)
222
223 @patch("flux_led.WifiLedBulb._send_msg")
224 @patch("flux_led.WifiLedBulb._read_msg")
225 @patch("flux_led.WifiLedBulb.connect")
226 def test_ww(self, mock_connect, mock_read, mock_send):
227 calls = 0
228
229 def read_data(expected):
230 nonlocal calls
231 calls += 1
232 if calls == 1:
233 self.assertEqual(expected, 2)
234 return bytearray(b"\x81E")
235 if calls == 2:
236 self.assertEqual(expected, 12)
237 return bytearray(b"#a!\x10\xb6\x00\x98\x00\x04\x00\xf0\xbd")
238 if calls == 3:
239 self.assertEqual(expected, 14)
240 return bytearray(b"\x81E#a!\x10\x00\x00\x00\x19\x04\x00\x0f\xa7")
241
242 mock_read.side_effect = read_data
243 light = flux_led.WifiLedBulb("192.168.1.164")
244 assert light.color_modes == {COLOR_MODE_RGB, COLOR_MODE_DIM}
245
246 self.assertEqual(mock_read.call_count, 2)
247 self.assertEqual(mock_send.call_count, 1)
248 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
249
250 self.assertEqual(
251 light.__str__(),
252 "ON [Color: (182, 0, 152) Brightness: 71% raw state: 129,69,35,97,33,16,182,0,152,0,4,0,240,189,]",
253 )
254 self.assertEqual(light.protocol, PROTOCOL_LEDENET_8BYTE)
255 self.assertEqual(light.is_on, True)
256 self.assertEqual(light.mode, "color")
257 self.assertEqual(light.warm_white, 0)
258 self.assertEqual(light.brightness, 182)
259 self.assertEqual(light.getRgb(), (182, 0, 152))
260 self.assertEqual(light.rgbwcapable, False)
261 self.assertEqual(light.device_type, flux_led.DeviceType.Bulb)
262
263 light.setWarmWhite255(25)
264 self.assertEqual(mock_read.call_count, 2)
265 self.assertEqual(mock_send.call_count, 2)
266 self.assertEqual(
267 mock_send.call_args, mock.call(bytearray(b"1\x00\x00\x00\x19\x0f\x0fh"))
268 )
269
270 light._transition_complete_time = 0
271 light.update_state()
272 self.assertEqual(mock_read.call_count, 3)
273 self.assertEqual(mock_send.call_count, 3)
274 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"\x81\x8a\x8b\x96")))
275
276 self.assertEqual(
277 light.__str__(),
278 "ON [Warm White: 9% raw state: 129,69,35,97,33,16,0,0,0,25,4,0,15,167,]",
279 )
280 self.assertEqual(light.protocol, PROTOCOL_LEDENET_8BYTE)
281 self.assertEqual(light.is_on, True)
282 self.assertEqual(light.mode, "ww")
283 self.assertEqual(light.warm_white, 0)
284 self.assertEqual(light.brightness, 25)
285 self.assertEqual(light.getRgb(), (255, 255, 255))
286 self.assertEqual(light.rgbwcapable, False)
287 self.assertEqual(light.device_type, flux_led.DeviceType.Bulb)
288
289 @patch("flux_led.WifiLedBulb._send_msg")
290 @patch("flux_led.WifiLedBulb._read_msg")
291 @patch("flux_led.WifiLedBulb.connect")
292 def test_switch(self, mock_connect, mock_read, mock_send):
293 calls = 0
294
295 def read_data(expected):
296 nonlocal calls
297 calls += 1
298 if calls == 1:
299 self.assertEqual(expected, 2)
300 return bytearray(b"\x81\x97")
301 if calls == 2:
302 self.assertEqual(expected, 12)
303 return bytearray(b"$$\x00\x00\x00\x00\x00\x00\x02\x00\x00b")
304 if calls == 3: # turn on response
305 self.assertEqual(expected, 4)
306 return bytearray(b"\x0fq#\xa3")
307 if calls == 4:
308 self.assertEqual(expected, 14)
309 return bytearray(b"\x81\x97##\x00\x00\x00\x00\x00\x00\x02\x00\x00`")
310
311 mock_read.side_effect = read_data
312 switch = flux_led.WifiLedBulb("192.168.1.164")
313 assert switch.color_modes == set()
314
315 self.assertEqual(mock_read.call_count, 2)
316 self.assertEqual(mock_send.call_count, 1)
317 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"\x81\x8a\x8b\x96")))
318
319 self.assertEqual(
320 switch.__str__(),
321 "OFF [Switch raw state: 129,151,36,36,0,0,0,0,0,0,2,0,0,98,]",
322 )
323 self.assertEqual(switch.protocol, PROTOCOL_LEDENET_SOCKET)
324 self.assertEqual(switch.is_on, False)
325 self.assertEqual(switch.mode, "switch")
326 self.assertEqual(switch.device_type, flux_led.DeviceType.Switch)
327
328 switch.turnOn()
329 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"q#\x0f\xa3")))
330 self.assertEqual(mock_read.call_count, 3)
331 self.assertEqual(mock_send.call_count, 2)
332
333 switch._transition_complete_time = 0
334 switch.update_state()
335 self.assertEqual(mock_read.call_count, 4)
336 self.assertEqual(mock_send.call_count, 3)
337
338 self.assertEqual(
339 switch.__str__(),
340 "ON [Switch raw state: 129,151,35,35,0,0,0,0,0,0,2,0,0,96,]",
341 )
342 self.assertEqual(switch.protocol, PROTOCOL_LEDENET_SOCKET)
343 self.assertEqual(switch.is_on, True)
344 self.assertEqual(switch.device_type, flux_led.DeviceType.Switch)
345
346 @patch("flux_led.WifiLedBulb._send_msg")
347 @patch("flux_led.WifiLedBulb._read_msg")
348 @patch("flux_led.WifiLedBulb.connect")
349 def test_rgb_brightness(self, mock_connect, mock_read, mock_send):
350 calls = 0
351
352 def read_data(expected):
353 nonlocal calls
354 calls += 1
355 if calls == 1: # first part of state response
356 self.assertEqual(expected, 2)
357 return bytearray(b"\x81E")
358 if calls == 2: # second part of state response
359 self.assertEqual(expected, 12)
360 return bytearray(b"$a!\x10\xff[\xd4\x00\x04\x00\xf0\x9e")
361 if calls == 3: # turn on response
362 self.assertEqual(expected, 4)
363 return bytearray(b"\x0fq#\xa3")
364 if calls == 4:
365 self.assertEqual(expected, 14)
366 return bytearray(b"\x81E#a!\x10\x03M\xf7\x00\x04\x00\xf0\xb6")
367
368 mock_read.side_effect = read_data
369 light = flux_led.WifiLedBulb("192.168.1.164")
370 assert light.color_modes == {COLOR_MODE_RGB, COLOR_MODE_DIM}
371
372 self.assertEqual(mock_read.call_count, 2)
373 self.assertEqual(mock_send.call_count, 1)
374 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
375 self.assertEqual(
376 light.__str__(),
377 "OFF [Color: (255, 91, 212) Brightness: 100% raw state: 129,69,36,97,33,16,255,91,212,0,4,0,240,158,]",
378 )
379 self.assertEqual(light.protocol, PROTOCOL_LEDENET_8BYTE)
380 self.assertEqual(light.getWarmWhite255(), 255)
381 self.assertEqual(light.getCCT(), (255, 255))
382 self.assertEqual(light.is_on, False)
383 self.assertEqual(light.mode, "color")
384 self.assertEqual(light.warm_white, 0)
385 self.assertEqual(light.brightness, 255)
386 self.assertEqual(light.getRgb(), (255, 91, 212))
387 self.assertEqual(light.device_type, flux_led.DeviceType.Bulb)
388
389 light.turnOn()
390 self.assertEqual(mock_read.call_count, 3)
391 self.assertEqual(mock_send.call_count, 2)
392 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"q#\x0f\xa3")))
393 self.assertEqual(
394 light.__str__(),
395 "ON [Color: (255, 91, 212) Brightness: 100% raw state: 129,69,35,97,33,16,255,91,212,0,4,0,240,158,]",
396 )
397 self.assertEqual(light.protocol, PROTOCOL_LEDENET_8BYTE)
398 self.assertEqual(light.is_on, True)
399 self.assertEqual(light.mode, "color")
400 self.assertEqual(light.warm_white, 0)
401 self.assertEqual(light.brightness, 255)
402 self.assertEqual(light.getRgb(), (255, 91, 212))
403
404 light.setRgb(1, 25, 80, brightness=247)
405 self.assertEqual(mock_read.call_count, 3)
406 self.assertEqual(mock_send.call_count, 3)
407 self.assertEqual(
408 mock_send.call_args, mock.call(bytearray(b"1\x03M\xf7\x00\xf0\x0fw"))
409 )
410 self.assertEqual(
411 light.__str__(),
412 "ON [Color: (3, 77, 247) Brightness: 97% raw state: 129,69,35,97,33,16,3,77,247,0,4,0,240,158,]",
413 )
414 self.assertEqual(light.protocol, PROTOCOL_LEDENET_8BYTE)
415 self.assertEqual(light.is_on, True)
416 self.assertEqual(light.mode, "color")
417 self.assertEqual(light.warm_white, 0)
418 self.assertEqual(light.brightness, 247)
419 self.assertEqual(light.getRgb(), (3, 77, 247))
420
421 light._transition_complete_time = 0
422 light.update_state()
423 self.assertEqual(mock_read.call_count, 4)
424 self.assertEqual(mock_send.call_count, 4)
425 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
426 self.assertEqual(
427 light.__str__(),
428 "ON [Color: (3, 77, 247) Brightness: 97% raw state: 129,69,35,97,33,16,3,77,247,0,4,0,240,182,]",
429 )
430 self.assertEqual(light.protocol, PROTOCOL_LEDENET_8BYTE)
431 self.assertEqual(light.is_on, True)
432 self.assertEqual(light.mode, "color")
433 self.assertEqual(light.warm_white, 0)
434 self.assertEqual(light.brightness, 247)
435 self.assertEqual(light.getRgb(), (3, 77, 247))
436
437 @patch("flux_led.WifiLedBulb._send_msg")
438 @patch("flux_led.WifiLedBulb._read_msg")
439 @patch("flux_led.WifiLedBulb.connect")
440 def test_rgbww_controller_version_4(self, mock_connect, mock_read, mock_send):
441 calls = 0
442
443 def read_data(expected):
444 nonlocal calls
445 calls += 1
446 if calls == 1:
447 self.assertEqual(expected, 2)
448 return bytearray(b"\x81\x25")
449 if calls == 2:
450 self.assertEqual(expected, 12)
451 return bytearray(b"\x23\x61\x05\x10\xb6\x00\x98\x00\x04\x00\xf0\x81")
452 if calls == 3:
453 self.assertEqual(expected, 14)
454 return bytearray(
455 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
456 )
457 if calls == 4:
458 self.assertEqual(expected, 14)
459 return bytearray(
460 b"\x81\x25\x23\x38\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xb5"
461 )
462 if calls == 5:
463 self.assertEqual(expected, 12)
464 return bytearray(b"\x0f\x11\x14\x16\x01\x02\x106\x02\x07\x00\x9c")
465
466 mock_read.side_effect = read_data
467 light = flux_led.WifiLedBulb("192.168.1.164")
468 assert light.color_modes == {COLOR_MODE_RGBWW, COLOR_MODE_CCT}
469 self.assertEqual(light.protocol, PROTOCOL_LEDENET_9BYTE)
470 self.assertEqual(light.model_num, 0x25)
471 self.assertEqual(light.version_num, 4)
472 self.assertEqual(light.microphone, False)
473 self.assertEqual(light.model, "Controller RGB/WW/CW (0x25)")
474 self.assertEqual(
475 light.effect_list,
476 [
477 "blue_fade",
478 "blue_strobe",
479 "colorjump",
480 "colorloop",
481 "colorstrobe",
482 "cyan_fade",
483 "cyan_strobe",
484 "gb_cross_fade",
485 "green_fade",
486 "green_strobe",
487 "purple_fade",
488 "purple_strobe",
489 "rb_cross_fade",
490 "red_fade",
491 "red_strobe",
492 "rg_cross_fade",
493 "white_fade",
494 "white_strobe",
495 "yellow_fade",
496 "yellow_strobe",
497 "random",
498 ],
499 )
500 assert light.pixels_per_segment is None
501 assert light.segments is None
502 assert light.music_pixels_per_segment is None
503 assert light.music_segments is None
504 assert light.ic_types is None
505 assert light.ic_type is None
506 assert light.operating_mode == "RGBWW"
507 assert light.operating_modes == ["DIM", "CCT", "RGB", "RGBW", "RGBWW"]
508 assert light.wiring is None
509 assert light.wirings is None
510
511 self.assertEqual(mock_read.call_count, 2)
512 self.assertEqual(mock_send.call_count, 1)
513 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
514
515 self.assertEqual(light.protocol, PROTOCOL_LEDENET_9BYTE)
516 self.assertEqual(light.is_on, True)
517 self.assertEqual(light.mode, "color")
518 self.assertEqual(light.min_temp, 2700)
519 self.assertEqual(light.max_temp, 6500)
520
521 self.assertEqual(light.warm_white, 0)
522 self.assertEqual(light.brightness, 61) # RGBWW brightness
523 self.assertEqual(light.getRgb(), (182, 0, 152))
524 self.assertEqual(light.getRgbw(), (182, 0, 152, 0))
525 self.assertEqual(light.getRgbww(), (182, 0, 152, 0, 0))
526
527 self.assertEqual(light.rgbwcapable, True)
528 self.assertEqual(
529 light.__str__(),
530 "ON [Color: (182, 0, 152) White: 0 raw state: 129,37,35,97,5,16,182,0,152,0,4,0,240,129,]",
531 )
532
533 light.setWarmWhite255(25)
534 self.assertEqual(mock_read.call_count, 2)
535 self.assertEqual(mock_send.call_count, 2)
536 self.assertEqual(
537 mock_send.call_args,
538 mock.call(bytearray(b"1\x00\x00\x00\x19\x00\x00\x0fY")),
539 )
540
541 light._transition_complete_time = 0
542 light.update_state()
543 self.assertEqual(mock_read.call_count, 3)
544 self.assertEqual(mock_send.call_count, 3)
545 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
546
547 self.assertEqual(light.protocol, PROTOCOL_LEDENET_9BYTE)
548 self.assertEqual(light.is_on, True)
549 self.assertEqual(light.mode, "color")
550 self.assertEqual(light.warm_white, 25)
551 self.assertEqual(light.cold_white, 37)
552 self.assertEqual(light.brightness, 81) # RGBWW brighness
553 self.assertEqual(light.rgbw, (182, 0, 152, 25))
554 self.assertEqual(light.getRgbw(), (182, 0, 152, 25))
555 self.assertEqual(light.rgbww, (182, 0, 152, 25, 37))
556 self.assertEqual(light.getRgbww(), (182, 0, 152, 25, 37))
557 self.assertEqual(light.rgbcw, (182, 0, 152, 37, 25))
558 self.assertEqual(light.getRgbcw(), (182, 0, 152, 37, 25))
559 self.assertEqual(light.rgbwcapable, True)
560 self.assertEqual(light.dimmable_effects, False)
561 self.assertEqual(light.requires_turn_on, True)
562 self.assertEqual(
563 light.__str__(),
564 "ON [Color: (182, 0, 152) White: 25 raw state: 129,37,35,97,5,16,182,0,152,25,4,37,15,222,]",
565 )
566
567 # Home Assistant legacy names
568 light.set_effect("colorjump", 50, 100)
569 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"a8\x10\x0f\xb8")))
570
571 # Library names
572 light.set_effect("seven_color_jumping", 50, 60)
573 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"a8\x10\x0f\xb8")))
574
575 with pytest.raises(ValueError):
576 light.set_effect("unknown", 50)
577
578 light._transition_complete_time = 0
579 light.update_state()
580 self.assertEqual(mock_read.call_count, 4)
581 self.assertEqual(mock_send.call_count, 6)
582 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
583 self.assertEqual(light.mode, "preset")
584 self.assertEqual(light.effect, "colorjump")
585 self.assertEqual(light.brightness, 255)
586
587 self.assertEqual(light.preset_pattern_num, 0x38)
588 self.assertEqual(
589 light.__str__(),
590 "ON [Pattern: colorjump (Speed 50%) raw state: 129,37,35,56,5,16,182,0,152,25,4,37,15,181,]",
591 )
592
593 assert light.getClock() == datetime.datetime(2022, 1, 2, 16, 54, 2)
594 self.assertEqual(mock_read.call_count, 5)
595 self.assertEqual(mock_send.call_count, 7)
596
597 light.setClock()
598 self.assertEqual(mock_read.call_count, 5)
599 self.assertEqual(mock_send.call_count, 8)
600
601 light.setWarmWhite(50)
602 self.assertEqual(
603 mock_send.call_args,
604 mock.call(bytearray(b"1\x00\x00\x00\x7f%\x00\x0f\xe4")),
605 )
606 light.setWarmWhite255(utils.percentToByte(50))
607 self.assertEqual(
608 mock_send.call_args,
609 mock.call(bytearray(b"1\x00\x00\x00\x7f%\x00\x0f\xe4")),
610 )
611 light.setColdWhite(50)
612 self.assertEqual(
613 mock_send.call_args,
614 mock.call(bytearray(b"1\x00\x00\x00\x00\x7f\x00\x0f\xbf")),
615 )
616 light.setColdWhite255(utils.percentToByte(50))
617 self.assertEqual(
618 mock_send.call_args,
619 mock.call(bytearray(b"1\x00\x00\x00\x00\x7f\x00\x0f\xbf")),
620 )
621 light.setCustomPattern([[255, 0, 0]], 50, TRANSITION_GRADUAL)
622 self.assertEqual(
623 mock_send.call_args,
624 mock.call(
625 bytearray(
626 b"Q\xff\x00\x00\x00\x01\x02\x03\x00\x01\x02\x03\x00\x01\x02\x03\x00\x01\x02\x03\x00\x01\x02\x03\x00\x01\x02\x03\x00\x01\x02\x03\x00\x01\x02\x03\x00\x01\x02\x03\x00\x01\x02\x03\x00\x01\x02\x03\x00\x01\x02\x03\x00\x01\x02\x03\x00\x01\x02\x03\x00\x01\x02\x03\x00\x10:\xff\x0f\x02"
627 )
628 ),
629 )
630 light.close()
631
632 @patch("flux_led.WifiLedBulb._send_msg")
633 @patch("flux_led.WifiLedBulb._read_msg")
634 @patch("flux_led.WifiLedBulb.connect")
635 def test_rgbww_controller_version_2_after_factory_reset(
636 self, mock_connect, mock_read, mock_send
637 ):
638 calls = 0
639
640 def read_data(expected):
641 nonlocal calls
642 calls += 1
643 if calls == 1:
644 self.assertEqual(expected, 2)
645 return bytearray(b"\x81\x25")
646 if calls == 2:
647 self.assertEqual(expected, 12)
648 return bytearray(b"\x23\x61\x00\x03\x00\xff\x00\x00\x02\x00\x5a\x88")
649 if calls == 3:
650 self.assertEqual(expected, 14)
651 return bytearray(
652 b"\x81\x25\x23\x61\x00\x03\x00\xff\x00\x00\x02\x00\x5a\x88"
653 )
654
655 mock_read.side_effect = read_data
656 light = flux_led.WifiLedBulb("192.168.1.164")
657 assert light.color_modes == {COLOR_MODE_CCT, COLOR_MODE_RGBWW}
658 self.assertEqual(light.protocol, PROTOCOL_LEDENET_9BYTE)
659 self.assertEqual(light.model_num, 0x25)
660 self.assertEqual(light.version_num, 2)
661 self.assertEqual(light.mode, "color")
662 self.assertEqual(light.raw_state.mode, 0)
663 self.assertEqual(light.microphone, False)
664 self.assertEqual(light.model, "Controller RGB/WW/CW (0x25)")
665 self.assertEqual(light.operating_mode, COLOR_MODE_RGBWW)
666
667 @patch("flux_led.WifiLedBulb._send_msg")
668 @patch("flux_led.WifiLedBulb._read_msg")
669 @patch("flux_led.WifiLedBulb.connect")
670 def test_rgbww_controller_version_9(self, mock_connect, mock_read, mock_send):
671 calls = 0
672
673 def read_data(expected):
674 nonlocal calls
675 calls += 1
676 if calls == 1:
677 self.assertEqual(expected, 2)
678 return bytearray(b"\x81\x25")
679 if calls == 2:
680 self.assertEqual(expected, 12)
681 return bytearray(b"\x23\x61\x05\x10\xb6\x00\x98\x00\x09\x00\xf0\x86")
682 if calls == 3:
683 self.assertEqual(expected, 14)
684 return bytearray(
685 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x09\x25\x0f\xe3"
686 )
687 if calls == 4:
688 self.assertEqual(expected, 14)
689 return bytearray(
690 b"\x81\x25\x23\x38\x05\x10\xb6\x00\x98\x19\x09\x25\x0f\xba"
691 )
692 if calls == 5:
693 self.assertEqual(expected, 94)
694 return bytearray(
695 b"\x0f\x22\xf0\x16\x01\x04\x00\x2b\x00\x00\x61\x19\x47\xff\x00\x00\xf0\xf0\x16\x01\x04\x04\x2c\x00\x00\x61\x7f\xff\x00\x00\x00\xf0\xf0\x16\x01\x03\x16\x1f\x00\x00\x61\xff\x00\x00\x00\x00\xf0\xf0\x16\x01\x03\x17\x13\x00\x00\x61\x81\x81\x81\x00\x00\xf0\xf0\x16\x01\x03\x17\x28\x00\x00\x61\x00\xff\x00\x00\x00\xf0\xf0\x16\x01\x04\x07\x2c\x00\x00\x61\x21\x00\xff\x00\x00\xf0\x00\x00"
696 )
697 if calls == 5:
698 self.assertEqual(expected, 4)
699 return bytearray(
700 b"\x0f\x22\xf0\x16\x01\x04\x00\x2b\x00\x00\x61\x19\x47\xff\x00\x00\xf0\xf0\x16\x01\x04\x04\x2c\x00\x00\x61\x7f\xff\x00\x00\x00\xf0\xf0\x16\x01\x03\x16\x1f\x00\x00\x61\xff\x00\x00\x00\x00\xf0\xf0\x16\x01\x03\x17\x13\x00\x00\x61\x81\x81\x81\x00\x00\xf0\xf0\x16\x01\x03\x17\x28\x00\x00\x61\x00\xff\x00\x00\x00\xf0\xf0\x16\x01\x04\x07\x2c\x00\x00\x61\x21\x00\xff\x00\x00\xf0\x00\x00"
701 )
702
703 mock_read.side_effect = read_data
704 light = flux_led.WifiLedBulb("192.168.1.164")
705 assert light.color_modes == {COLOR_MODE_RGBWW, COLOR_MODE_CCT}
706 self.assertEqual(light.protocol, PROTOCOL_LEDENET_9BYTE)
707 self.assertEqual(light.model_num, 0x25)
708 self.assertEqual(light.version_num, 9)
709 self.assertEqual(light.microphone, False)
710 self.assertEqual(light.model, "Controller RGB/WW/CW (0x25)")
711 self.assertEqual(
712 light.effect_list,
713 [
714 "blue_fade",
715 "blue_strobe",
716 "colorjump",
717 "colorloop",
718 "colorstrobe",
719 "cyan_fade",
720 "cyan_strobe",
721 "gb_cross_fade",
722 "green_fade",
723 "green_strobe",
724 "purple_fade",
725 "purple_strobe",
726 "rb_cross_fade",
727 "red_fade",
728 "red_strobe",
729 "rg_cross_fade",
730 "white_fade",
731 "white_strobe",
732 "yellow_fade",
733 "yellow_strobe",
734 "random",
735 ],
736 )
737
738 self.assertEqual(mock_read.call_count, 2)
739 self.assertEqual(mock_send.call_count, 1)
740 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
741
742 self.assertEqual(light.protocol, PROTOCOL_LEDENET_9BYTE)
743 self.assertEqual(light.is_on, True)
744 self.assertEqual(light.mode, "color")
745 self.assertEqual(light.min_temp, 2700)
746 self.assertEqual(light.max_temp, 6500)
747
748 self.assertEqual(light.warm_white, 0)
749 self.assertEqual(light.brightness, 61) # RGBWW brightness
750 self.assertEqual(light.getRgb(), (182, 0, 152))
751 self.assertEqual(light.getRgbw(), (182, 0, 152, 0))
752 self.assertEqual(light.getRgbww(), (182, 0, 152, 0, 0))
753
754 self.assertEqual(light.rgbwcapable, True)
755 self.assertEqual(
756 light.__str__(),
757 "ON [Color: (182, 0, 152) White: 0 raw state: 129,37,35,97,5,16,182,0,152,0,9,0,240,134,]",
758 )
759
760 light.setWarmWhite255(25)
761 self.assertEqual(mock_read.call_count, 2)
762 self.assertEqual(mock_send.call_count, 2)
763 self.assertEqual(
764 mock_send.call_args,
765 mock.call(bytearray(b"1\x00\x00\x00\x19\x00\x00\x0fY")),
766 )
767
768 light._transition_complete_time = 0
769 light.update_state()
770 self.assertEqual(mock_read.call_count, 3)
771 self.assertEqual(mock_send.call_count, 3)
772 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
773
774 self.assertEqual(light.protocol, PROTOCOL_LEDENET_9BYTE)
775 self.assertEqual(light.is_on, True)
776 self.assertEqual(light.mode, "color")
777 self.assertEqual(light.warm_white, 25)
778 self.assertEqual(light.cold_white, 37)
779 self.assertEqual(light.brightness, 81) # RGBWW brighness
780 self.assertEqual(light.rgbw, (182, 0, 152, 25))
781 self.assertEqual(light.getRgbw(), (182, 0, 152, 25))
782 self.assertEqual(light.rgbww, (182, 0, 152, 25, 37))
783 self.assertEqual(light.getRgbww(), (182, 0, 152, 25, 37))
784 self.assertEqual(light.rgbcw, (182, 0, 152, 37, 25))
785 self.assertEqual(light.getRgbcw(), (182, 0, 152, 37, 25))
786 self.assertEqual(light.rgbwcapable, True)
787 self.assertEqual(light.dimmable_effects, False)
788 self.assertEqual(light.requires_turn_on, True)
789 self.assertEqual(
790 light.__str__(),
791 "ON [Color: (182, 0, 152) White: 25 raw state: 129,37,35,97,5,16,182,0,152,25,9,37,15,227,]",
792 )
793
794 # Home Assistant legacy names
795 light.set_effect("colorjump", 50, 100)
796 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"a8\x10\x0f\xb8")))
797
798 # Library names
799 light.set_effect("seven_color_jumping", 50, 60)
800 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"a8\x10\x0f\xb8")))
801
802 with pytest.raises(ValueError):
803 light.set_effect("unknown", 50)
804
805 light._transition_complete_time = 0
806 light.update_state()
807 self.assertEqual(mock_read.call_count, 4)
808 self.assertEqual(mock_send.call_count, 6)
809 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
810 self.assertEqual(light.mode, "preset")
811 self.assertEqual(light.effect, "colorjump")
812 self.assertEqual(light.brightness, 255)
813
814 self.assertEqual(light.preset_pattern_num, 0x38)
815 self.assertEqual(
816 light.__str__(),
817 "ON [Pattern: colorjump (Speed 50%) raw state: 129,37,35,56,5,16,182,0,152,25,9,37,15,186,]",
818 )
819 timers = light.getTimers()
820 assert len(timers) == 6
821 self.assertEqual(mock_read.call_count, 5)
822 self.assertEqual(mock_send.call_count, 7)
823
824 light.sendTimers(timers)
825 self.assertEqual(mock_read.call_count, 6)
826 self.assertEqual(mock_send.call_count, 8)
827
828 @patch("flux_led.WifiLedBulb._send_msg")
829 @patch("flux_led.WifiLedBulb._read_msg")
830 @patch("flux_led.WifiLedBulb.connect")
831 def test_rgbww_controller_version_10(self, mock_connect, mock_read, mock_send):
832 calls = 0
833
834 def read_data(expected):
835 nonlocal calls
836 calls += 1
837 if calls == 1:
838 self.assertEqual(expected, 2)
839 return bytearray(b"\x81\x35")
840 if calls == 2:
841 self.assertEqual(expected, 12)
842 return bytearray(b"\x23\x61\x30\x40\xb5\x00\x9c\x00\x0a\x00\xf0\xf5")
843 if calls == 3:
844 self.assertEqual(expected, 14)
845 return bytearray(
846 b"\x81\x35\x23\x61\x30\x40\xb5\x00\x9c\x00\x0a\x00\xf0\xf5"
847 )
848 if calls == 4:
849 self.assertEqual(expected, 14)
850 return bytearray(
851 b"\x81\x35\x23\x61\x30\x40\xb5\x00\x9c\x00\x0a\x00\xf0\xf5"
852 )
853 if calls == 5:
854 self.assertEqual(expected, 14)
855 return bytearray(
856 b"\x81\x35\x23\x61\x30\x40\xb5\x00\x9c\x00\x0a\x00\xf0\xf5"
857 )
858 if calls == 6:
859 self.assertEqual(expected, 94)
860 return bytearray(
861 b"\x0f\x22\xf0\x16\x01\x04\x00\x2b\x00\x00\x61\x19\x47\xff\x00\x00\xf0"
862 b"\xf0\x16\x01\x04\x04\x2c\x00\x00\x61\x7f\xff\x00\x00\x00\xf0\xf0\x16"
863 b"\x01\x03\x16\x1f\x00\x00\x61\xff\x00\x00\x00\x00\xf0\xf0\x16\x01\x03"
864 b"\x17\x13\x00\x00\x61\x81\x81\x81\x00\x00\xf0\xf0\x16\x01\x03\x17\x28"
865 b"\x00\x00\x61\x00\xff\x00\x00\x00\xf0\xf0\x16\x01\x04\x07\x2c\x00\x00"
866 b"\x61\x21\x00\xff\x00\x00\xf0\x00\x00"
867 )
868
869 mock_read.side_effect = read_data
870 light = flux_led.WifiLedBulb("192.168.1.164")
871
872 assert light.color_modes == {COLOR_MODE_RGB, COLOR_MODE_CCT}
873 self.assertEqual(light.protocol, PROTOCOL_LEDENET_25BYTE)
874 self.assertEqual(light.model_num, 0x35)
875 self.assertEqual(light.version_num, 10)
876 self.assertEqual(light.speed, 64)
877
878 light.setRgb(255, 100, 50)
879 self.assertEqual(mock_send.call_count, 2)
880 self.assertEqual(light.getRgb(), (255, 100, 50))
881 self.assertEqual(light.getRgbw(), (255, 100, 50, 0))
882 self.assertEqual(light.getRgbww(), (255, 100, 50, 0, 0))
883
884 light.setColdWhite255(200)
885 self.assertEqual(mock_send.call_count, 3)
886 self.assertEqual(light.getRgb(), (255, 255, 255))
887 self.assertEqual(light.getRgbw(), (255, 255, 255, 255))
888 self.assertEqual(light.getRgbww(), (255, 255, 255, 255, 255))
889
890 light.setWhiteTemperature(2700, 100)
891 self.assertEqual(mock_send.call_count, 4)
892
893 light.setWhiteTemperature(6500, 100)
894 self.assertEqual(mock_send.call_count, 5)
895
896 light.update_state()
897 self.assertEqual(mock_read.call_count, 3)
898 self.assertEqual(mock_send.call_count, 6)
899 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
900
901 @patch("flux_led.WifiLedBulb._send_msg")
902 @patch("flux_led.WifiLedBulb._read_msg")
903 @patch("flux_led.WifiLedBulb.connect")
904 def test_rgbcw_bulb_v4(self, mock_connect, mock_read, mock_send):
905 calls = 0
906
907 def read_data(expected):
908 nonlocal calls
909 calls += 1
910 if calls == 1:
911 self.assertEqual(expected, 2)
912 return bytearray(b"\x81\x35")
913 if calls == 2:
914 self.assertEqual(expected, 12)
915 return bytearray(b"\x23\x61\x05\x10\xb6\x00\x98\x00\x04\x00\xf0\x91")
916 if calls == 3:
917 self.assertEqual(expected, 14)
918 return bytearray(
919 b"\x81\x35\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xee"
920 )
921 if calls == 4:
922 self.assertEqual(expected, 14)
923 return bytearray(
924 b"\x81\x35\x23\x38\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xc5"
925 )
926
927 mock_read.side_effect = read_data
928 light = flux_led.WifiLedBulb("192.168.1.164")
929 assert light.color_modes == {COLOR_MODE_RGB, COLOR_MODE_CCT}
930 self.assertEqual(light.version_num, 0x04)
931 self.assertEqual(light.protocol, PROTOCOL_LEDENET_9BYTE)
932 self.assertEqual(light.model_num, 0x35)
933 self.assertEqual(light.microphone, False)
934 self.assertEqual(light.model, "Bulb RGBCW (0x35)")
935 self.assertEqual(
936 light.effect_list,
937 [
938 "blue_fade",
939 "blue_strobe",
940 "colorjump",
941 "colorloop",
942 "colorstrobe",
943 "cyan_fade",
944 "cyan_strobe",
945 "gb_cross_fade",
946 "green_fade",
947 "green_strobe",
948 "purple_fade",
949 "purple_strobe",
950 "rb_cross_fade",
951 "red_fade",
952 "red_strobe",
953 "rg_cross_fade",
954 "white_fade",
955 "white_strobe",
956 "yellow_fade",
957 "yellow_strobe",
958 "random",
959 ],
960 )
961 assert light.pixels_per_segment is None
962 assert light.segments is None
963 assert light.music_pixels_per_segment is None
964 assert light.music_segments is None
965 assert light.ic_types is None
966 assert light.ic_type is None
967 assert light.operating_mode is None
968 assert light.operating_modes is None
969 assert light.wiring is None
970 assert light.wirings is None
971
972 self.assertEqual(mock_read.call_count, 2)
973 self.assertEqual(mock_send.call_count, 1)
974 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
975
976 self.assertEqual(light.protocol, PROTOCOL_LEDENET_9BYTE)
977 self.assertEqual(light.is_on, True)
978 self.assertEqual(light.mode, "color")
979 self.assertEqual(light.min_temp, 2700)
980 self.assertEqual(light.max_temp, 6500)
981
982 self.assertEqual(light.warm_white, 0)
983 self.assertEqual(light.brightness, 182)
984 self.assertEqual(light.getRgb(), (182, 0, 152))
985 self.assertEqual(light.getRgbw(), (182, 0, 152, 0))
986 self.assertEqual(light.getRgbww(), (182, 0, 152, 0, 0))
987
988 self.assertEqual(light.rgbwcapable, False)
989 self.assertEqual(
990 light.__str__(),
991 (
992 "ON [Color: (182, 0, 152) Brightness: 71% raw state: "
993 "129,53,35,97,5,16,182,0,152,0,4,0,240,145,]"
994 ),
995 )
996
997 light.setWarmWhite255(25)
998 self.assertEqual(mock_read.call_count, 2)
999 self.assertEqual(mock_send.call_count, 2)
1000 self.assertEqual(
1001 mock_send.call_args,
1002 mock.call(bytearray(b"1\x00\x00\x00\x19\x19\x0f\x0f\x81")),
1003 )
1004
1005 light._transition_complete_time = 0
1006 light.update_state()
1007 self.assertEqual(mock_read.call_count, 3)
1008 self.assertEqual(mock_send.call_count, 3)
1009 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
1010
1011 self.assertEqual(light.protocol, PROTOCOL_LEDENET_9BYTE)
1012 self.assertEqual(light.is_on, True)
1013 self.assertEqual(light.mode, "ww")
1014 self.assertEqual(light.warm_white, 25)
1015 self.assertEqual(light.cold_white, 37)
1016 self.assertEqual(light.brightness, 62)
1017 self.assertEqual(light.rgbw, (182, 0, 152, 25))
1018 self.assertEqual(light.getRgbw(), (255, 255, 255, 255))
1019 self.assertEqual(light.rgbww, (182, 0, 152, 25, 37))
1020 self.assertEqual(light.getRgbww(), (255, 255, 255, 255, 255))
1021 self.assertEqual(light.rgbcw, (182, 0, 152, 37, 25))
1022 self.assertEqual(light.getRgbcw(), (255, 255, 255, 255, 255))
1023 self.assertEqual(light.rgbwcapable, False)
1024 self.assertEqual(light.dimmable_effects, False)
1025 self.assertEqual(light.requires_turn_on, True)
1026 self.assertEqual(
1027 light.__str__(),
1028 (
1029 "ON [CCT: 4968K Brightness: 24% raw state: "
1030 "129,53,35,97,5,16,182,0,152,25,4,37,15,238,]"
1031 ),
1032 )
1033
1034 # Home Assistant legacy names
1035 light.set_effect("colorjump", 50, 100)
1036 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"a8\x10\x0f\xb8")))
1037
1038 # Library names
1039 light.set_effect("seven_color_jumping", 50, 60)
1040 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"a8\x10\x0f\xb8")))
1041
1042 with pytest.raises(ValueError):
1043 light.set_effect("unknown", 50)
1044
1045 light._transition_complete_time = 0
1046 light.update_state()
1047 self.assertEqual(mock_read.call_count, 4)
1048 self.assertEqual(mock_send.call_count, 6)
1049 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
1050 self.assertEqual(light.mode, "preset")
1051 self.assertEqual(light.effect, "colorjump")
1052 self.assertEqual(light.brightness, 255)
1053
1054 self.assertEqual(light.preset_pattern_num, 0x38)
1055 self.assertEqual(
1056 light.__str__(),
1057 (
1058 "ON [Pattern: colorjump (Speed 50%) raw state: "
1059 "129,53,35,56,5,16,182,0,152,25,4,37,15,197,]"
1060 ),
1061 )
1062 light.setWhiteTemperature(2700, 255)
1063 self.assertEqual(mock_read.call_count, 4)
1064 self.assertEqual(mock_send.call_count, 7)
1065 self.assertEqual(
1066 mock_send.call_args, mock.call(bytearray(b"1\x00\x00\x00\xff\x00\x0f\x0fN"))
1067 )
1068
1069 @patch("flux_led.WifiLedBulb._send_msg")
1070 @patch("flux_led.WifiLedBulb._read_msg")
1071 @patch("flux_led.WifiLedBulb.connect")
1072 def test_rgbcw_floor_lamp_v7(self, mock_connect, mock_read, mock_send):
1073 calls = 0
1074
1075 def read_data(expected):
1076 nonlocal calls
1077 calls += 1
1078 if calls == 1:
1079 self.assertEqual(expected, 2)
1080 return bytearray(b"\x81\x0e")
1081 if calls == 2:
1082 self.assertEqual(expected, 12)
1083 return bytearray(b"\x23\x61\x07\x10\xb6\x00\x98\x00\x07\x00\xf0\x6f")
1084 raise ValueError
1085
1086 mock_read.side_effect = read_data
1087 light = flux_led.WifiLedBulb("192.168.1.164")
1088 assert light.color_modes == {COLOR_MODE_RGB, COLOR_MODE_CCT}
1089 self.assertEqual(light.version_num, 0x07)
1090 self.assertEqual(light.protocol, PROTOCOL_LEDENET_9BYTE_AUTO_ON)
1091 self.assertEqual(light.model_num, 0x0E)
1092 self.assertEqual(light.microphone, False)
1093 self.assertEqual(light.dimmable_effects, False)
1094 self.assertEqual(light.requires_turn_on, False)
1095 self.assertEqual(light.model, "Floor Lamp RGBCW (0x0E)")
1096 self.assertEqual(
1097 light.effect_list,
1098 [
1099 "blue_fade",
1100 "blue_strobe",
1101 "colorjump",
1102 "colorloop",
1103 "colorstrobe",
1104 "cyan_fade",
1105 "cyan_strobe",
1106 "gb_cross_fade",
1107 "green_fade",
1108 "green_strobe",
1109 "purple_fade",
1110 "purple_strobe",
1111 "rb_cross_fade",
1112 "red_fade",
1113 "red_strobe",
1114 "rg_cross_fade",
1115 "white_fade",
1116 "white_strobe",
1117 "yellow_fade",
1118 "yellow_strobe",
1119 "random",
1120 ],
1121 )
1122
1123 self.assertEqual(mock_read.call_count, 2)
1124 self.assertEqual(mock_send.call_count, 1)
1125 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
1126
1127 self.assertEqual(light.is_on, True)
1128 self.assertEqual(light.mode, "color")
1129 self.assertEqual(light.min_temp, 2700)
1130 self.assertEqual(light.max_temp, 6500)
1131
1132 @patch("flux_led.WifiLedBulb._send_msg")
1133 @patch("flux_led.WifiLedBulb._read_msg")
1134 @patch("flux_led.WifiLedBulb.connect")
1135 def test_rgbcw_floor_lamp_v9(self, mock_connect, mock_read, mock_send):
1136 calls = 0
1137
1138 def read_data(expected):
1139 nonlocal calls
1140 calls += 1
1141 if calls == 1:
1142 self.assertEqual(expected, 2)
1143 return bytearray(b"\x81\x0e")
1144 if calls == 2:
1145 self.assertEqual(expected, 12)
1146 return bytearray(b"\x23\x61\x07\x10\xb6\x00\x98\x00\x09\x00\xf0\x71")
1147 raise ValueError
1148
1149 mock_read.side_effect = read_data
1150 light = flux_led.WifiLedBulb("192.168.1.164")
1151 assert light.color_modes == {COLOR_MODE_RGB, COLOR_MODE_CCT}
1152 self.assertEqual(light.version_num, 0x09)
1153 self.assertEqual(light.protocol, PROTOCOL_LEDENET_9BYTE_DIMMABLE_EFFECTS)
1154 self.assertEqual(light.model_num, 0x0E)
1155 self.assertEqual(light.microphone, False)
1156 self.assertEqual(light.dimmable_effects, True)
1157 self.assertEqual(light.requires_turn_on, False)
1158 self.assertEqual(light.model, "Floor Lamp RGBCW (0x0E)")
1159 self.assertEqual(
1160 light.effect_list,
1161 [
1162 "blue_fade",
1163 "blue_strobe",
1164 "colorjump",
1165 "colorloop",
1166 "colorstrobe",
1167 "cyan_fade",
1168 "cyan_strobe",
1169 "cycle_rgb",
1170 "cycle_seven_colors",
1171 "gb_cross_fade",
1172 "green_fade",
1173 "green_strobe",
1174 "purple_fade",
1175 "purple_strobe",
1176 "rb_cross_fade",
1177 "red_fade",
1178 "red_strobe",
1179 "rg_cross_fade",
1180 "rgb_cross_fade",
1181 "white_fade",
1182 "white_strobe",
1183 "yellow_fade",
1184 "yellow_strobe",
1185 "random",
1186 ],
1187 )
1188
1189 self.assertEqual(mock_read.call_count, 2)
1190 self.assertEqual(mock_send.call_count, 1)
1191 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
1192
1193 self.assertEqual(light.is_on, True)
1194 self.assertEqual(light.mode, "color")
1195 self.assertEqual(light.min_temp, 2700)
1196 self.assertEqual(light.max_temp, 6500)
1197
1198 @patch("flux_led.WifiLedBulb._send_msg")
1199 @patch("flux_led.WifiLedBulb._read_msg")
1200 @patch("flux_led.WifiLedBulb.connect")
1201 def test_rgb_controller_33_v3(self, mock_connect, mock_read, mock_send):
1202 calls = 0
1203
1204 def read_data(expected):
1205 nonlocal calls
1206 calls += 1
1207 if calls == 1:
1208 self.assertEqual(expected, 2)
1209 return bytearray(b"\x81\x33")
1210 if calls == 2:
1211 self.assertEqual(expected, 12)
1212 return bytearray(b"\x23\x61\x07\x10\xb6\x00\x98\x00\x03\x00\xf0\x90")
1213 raise ValueError
1214
1215 mock_read.side_effect = read_data
1216 light = flux_led.WifiLedBulb("192.168.1.164")
1217 assert light.color_modes == {COLOR_MODE_RGB}
1218 self.assertEqual(light.version_num, 0x03)
1219 self.assertEqual(light.protocol, PROTOCOL_LEDENET_8BYTE)
1220 self.assertEqual(light.model_num, 0x33)
1221 self.assertEqual(light.microphone, False)
1222 self.assertEqual(light.dimmable_effects, False)
1223 self.assertEqual(light.requires_turn_on, True)
1224 self.assertEqual(light._protocol.power_push_updates, False)
1225 self.assertEqual(light._protocol.state_push_updates, False)
1226 self.assertEqual(light.model, "Controller RGB (0x33)")
1227 self.assertEqual(
1228 light.effect_list,
1229 [
1230 "blue_fade",
1231 "blue_strobe",
1232 "colorjump",
1233 "colorloop",
1234 "colorstrobe",
1235 "cyan_fade",
1236 "cyan_strobe",
1237 "gb_cross_fade",
1238 "green_fade",
1239 "green_strobe",
1240 "purple_fade",
1241 "purple_strobe",
1242 "rb_cross_fade",
1243 "red_fade",
1244 "red_strobe",
1245 "rg_cross_fade",
1246 "white_fade",
1247 "white_strobe",
1248 "yellow_fade",
1249 "yellow_strobe",
1250 "random",
1251 ],
1252 )
1253
1254 self.assertEqual(mock_read.call_count, 2)
1255 self.assertEqual(mock_send.call_count, 1)
1256 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
1257
1258 self.assertEqual(light.is_on, True)
1259 self.assertEqual(light.mode, "color")
1260 self.assertEqual(light.min_temp, 2700)
1261 self.assertEqual(light.max_temp, 6500)
1262
1263 @patch("flux_led.WifiLedBulb._send_msg")
1264 @patch("flux_led.WifiLedBulb._read_msg")
1265 @patch("flux_led.WifiLedBulb.connect")
1266 def test_rgb_controller_33_v7(self, mock_connect, mock_read, mock_send):
1267 calls = 0
1268
1269 def read_data(expected):
1270 nonlocal calls
1271 calls += 1
1272 if calls == 1:
1273 self.assertEqual(expected, 2)
1274 return bytearray(b"\x81\x33")
1275 if calls == 2:
1276 self.assertEqual(expected, 12)
1277 return bytearray(b"\x23\x61\x07\x10\xb6\x00\x98\x00\x07\x00\xf0\x94")
1278 raise ValueError
1279
1280 mock_read.side_effect = read_data
1281 light = flux_led.WifiLedBulb("192.168.1.164")
1282 assert light.color_modes == {COLOR_MODE_RGB}
1283 self.assertEqual(light.mode, "color")
1284 self.assertEqual(light.version_num, 0x07)
1285 self.assertEqual(light.protocol, PROTOCOL_LEDENET_8BYTE_AUTO_ON)
1286 self.assertEqual(light.model_num, 0x33)
1287 self.assertEqual(light.microphone, False)
1288 self.assertEqual(light.dimmable_effects, False)
1289 self.assertEqual(light.requires_turn_on, False)
1290 self.assertEqual(light._protocol.power_push_updates, False)
1291 self.assertEqual(light._protocol.state_push_updates, False)
1292 self.assertEqual(light.model, "Controller RGB (0x33)")
1293 self.assertEqual(
1294 light.effect_list,
1295 [
1296 "blue_fade",
1297 "blue_strobe",
1298 "colorjump",
1299 "colorloop",
1300 "colorstrobe",
1301 "cyan_fade",
1302 "cyan_strobe",
1303 "gb_cross_fade",
1304 "green_fade",
1305 "green_strobe",
1306 "purple_fade",
1307 "purple_strobe",
1308 "rb_cross_fade",
1309 "red_fade",
1310 "red_strobe",
1311 "rg_cross_fade",
1312 "white_fade",
1313 "white_strobe",
1314 "yellow_fade",
1315 "yellow_strobe",
1316 "random",
1317 ],
1318 )
1319 assert light.pixels_per_segment is None
1320 assert light.segments is None
1321 assert light.music_pixels_per_segment is None
1322 assert light.music_segments is None
1323 assert light.ic_types is None
1324 assert light.ic_type is None
1325 assert light.operating_mode is None
1326 assert light.operating_modes is None
1327 assert light.wiring is None
1328 assert light.wirings == ["RGB", "GRB", "BRG"]
1329 self.assertEqual(mock_read.call_count, 2)
1330 self.assertEqual(mock_send.call_count, 1)
1331 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
1332
1333 self.assertEqual(light.is_on, True)
1334 self.assertEqual(light.mode, "color")
1335 self.assertEqual(light.min_temp, 2700)
1336 self.assertEqual(light.max_temp, 6500)
1337
1338 @patch("flux_led.WifiLedBulb._send_msg")
1339 @patch("flux_led.WifiLedBulb._read_msg")
1340 @patch("flux_led.WifiLedBulb.connect")
1341 def test_rgb_controller_33_v9(self, mock_connect, mock_read, mock_send):
1342 calls = 0
1343
1344 def read_data(expected):
1345 nonlocal calls
1346 calls += 1
1347 if calls == 1:
1348 self.assertEqual(expected, 2)
1349 return bytearray(b"\x81\x33")
1350 if calls == 2:
1351 self.assertEqual(expected, 12)
1352 return bytearray(b"\x23\x61\x07\x10\xb6\x00\x98\x00\x09\x00\xf0\x96")
1353 raise ValueError
1354
1355 mock_read.side_effect = read_data
1356 light = flux_led.WifiLedBulb("192.168.1.164")
1357 assert light.color_modes == {COLOR_MODE_RGB}
1358 self.assertEqual(light.version_num, 0x09)
1359 self.assertEqual(light.protocol, PROTOCOL_LEDENET_8BYTE_DIMMABLE_EFFECTS)
1360 self.assertEqual(light.model_num, 0x33)
1361 self.assertEqual(light.microphone, False)
1362 self.assertEqual(light.dimmable_effects, True)
1363 self.assertEqual(light.requires_turn_on, False)
1364 self.assertEqual(light._protocol.power_push_updates, True)
1365 self.assertEqual(light._protocol.state_push_updates, True)
1366 self.assertEqual(light.model, "Controller RGB (0x33)")
1367 self.assertEqual(
1368 light.effect_list,
1369 [
1370 "blue_fade",
1371 "blue_strobe",
1372 "colorjump",
1373 "colorloop",
1374 "colorstrobe",
1375 "cyan_fade",
1376 "cyan_strobe",
1377 "cycle_rgb",
1378 "cycle_seven_colors",
1379 "gb_cross_fade",
1380 "green_fade",
1381 "green_strobe",
1382 "purple_fade",
1383 "purple_strobe",
1384 "rb_cross_fade",
1385 "red_fade",
1386 "red_strobe",
1387 "rg_cross_fade",
1388 "rgb_cross_fade",
1389 "white_fade",
1390 "white_strobe",
1391 "yellow_fade",
1392 "yellow_strobe",
1393 "random",
1394 ],
1395 )
1396 assert light.pixels_per_segment is None
1397 assert light.segments is None
1398 assert light.music_pixels_per_segment is None
1399 assert light.music_segments is None
1400 assert light.ic_types is None
1401 assert light.ic_type is None
1402 assert light.operating_mode is None
1403 assert light.operating_modes is None
1404 assert light.wiring is None
1405 assert light.wirings == ["RGB", "GRB", "BRG"]
1406 self.assertEqual(mock_read.call_count, 2)
1407 self.assertEqual(mock_send.call_count, 1)
1408 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
1409
1410 self.assertEqual(light.is_on, True)
1411 self.assertEqual(light.mode, "color")
1412 self.assertEqual(light.min_temp, 2700)
1413 self.assertEqual(light.max_temp, 6500)
1414
1415 light.set_effect("blue_fade", 50, 50)
1416 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"8(\x102\xa2")))
1417
1418 assert PresetPattern.valtostr(0x25) == "Seven Color Cross Fade"
1419 assert PresetPattern.str_to_val("Seven Color Cross Fade") == 0x25
1420 assert PresetPattern.str_to_val("colorloop") == 0x25
1421
1422 light.set_effect("colorloop", 50, 50)
1423 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"8%\x102\x9f")))
1424
1425 @patch("flux_led.WifiLedBulb._send_msg")
1426 @patch("flux_led.WifiLedBulb._read_msg")
1427 @patch("flux_led.WifiLedBulb.connect")
1428 def test_rgbcw_bulb_v9(self, mock_connect, mock_read, mock_send):
1429 calls = 0
1430
1431 def read_data(expected):
1432 nonlocal calls
1433 calls += 1
1434 if calls == 1:
1435 self.assertEqual(expected, 2)
1436 return bytearray(b"\x81\x35")
1437 if calls == 2:
1438 self.assertEqual(expected, 12)
1439 return bytearray(b"\x23\x61\x05\x10\xb6\x00\x98\x00\x09\x00\xf0\x96")
1440 if calls == 3:
1441 self.assertEqual(expected, 14)
1442 return bytearray(
1443 b"\x81\x35\x23\x61\x05\x10\xb6\x00\x98\x19\x09\x25\x0f\xf3"
1444 )
1445 if calls == 4:
1446 self.assertEqual(expected, 14)
1447 return bytearray(
1448 b"\x81\x35\x23\x38\x05\x10\xb6\x00\x98\x19\x09\x25\x0f\xca"
1449 )
1450
1451 mock_read.side_effect = read_data
1452 light = flux_led.WifiLedBulb("192.168.1.164")
1453 assert light.color_modes == {COLOR_MODE_RGB, COLOR_MODE_CCT}
1454 self.assertEqual(light.protocol, PROTOCOL_LEDENET_9BYTE_DIMMABLE_EFFECTS)
1455 self.assertEqual(light.model_num, 0x35)
1456 self.assertEqual(light.microphone, False)
1457 self.assertEqual(light.model, "Bulb RGBCW (0x35)")
1458 self.assertEqual(
1459 light.effect_list,
1460 [
1461 "blue_fade",
1462 "blue_strobe",
1463 "colorjump",
1464 "colorloop",
1465 "colorstrobe",
1466 "cyan_fade",
1467 "cyan_strobe",
1468 "cycle_rgb",
1469 "cycle_seven_colors",
1470 "gb_cross_fade",
1471 "green_fade",
1472 "green_strobe",
1473 "purple_fade",
1474 "purple_strobe",
1475 "rb_cross_fade",
1476 "red_fade",
1477 "red_strobe",
1478 "rg_cross_fade",
1479 "rgb_cross_fade",
1480 "white_fade",
1481 "white_strobe",
1482 "yellow_fade",
1483 "yellow_strobe",
1484 "random",
1485 ],
1486 )
1487
1488 self.assertEqual(mock_read.call_count, 2)
1489 self.assertEqual(mock_send.call_count, 1)
1490 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
1491
1492 self.assertEqual(light.protocol, PROTOCOL_LEDENET_9BYTE_DIMMABLE_EFFECTS)
1493 self.assertEqual(light.is_on, True)
1494 self.assertEqual(light.mode, "color")
1495 self.assertEqual(light.min_temp, 2700)
1496 self.assertEqual(light.max_temp, 6500)
1497
1498 self.assertEqual(light.warm_white, 0)
1499 self.assertEqual(light.brightness, 182)
1500 self.assertEqual(light.getRgb(), (182, 0, 152))
1501 self.assertEqual(light.getRgbw(), (182, 0, 152, 0))
1502 self.assertEqual(light.getRgbww(), (182, 0, 152, 0, 0))
1503
1504 self.assertEqual(light.rgbwcapable, False)
1505 self.assertEqual(
1506 light.__str__(),
1507 (
1508 "ON [Color: (182, 0, 152) Brightness: 71% raw state: "
1509 "129,53,35,97,5,16,182,0,152,0,9,0,240,150,]"
1510 ),
1511 )
1512
1513 light.setWarmWhite255(25)
1514 self.assertEqual(mock_read.call_count, 2)
1515 self.assertEqual(mock_send.call_count, 2)
1516 self.assertEqual(
1517 mock_send.call_args,
1518 mock.call(bytearray(b"1\x00\x00\x00\x19\x19\x0f\x0f\x81")),
1519 )
1520
1521 light._transition_complete_time = 0
1522 light.update_state()
1523 self.assertEqual(mock_read.call_count, 3)
1524 self.assertEqual(mock_send.call_count, 3)
1525 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
1526
1527 self.assertEqual(light.protocol, PROTOCOL_LEDENET_9BYTE_DIMMABLE_EFFECTS)
1528 self.assertEqual(light.is_on, True)
1529 self.assertEqual(light.mode, "ww")
1530 self.assertEqual(light.warm_white, 25)
1531 self.assertEqual(light.cold_white, 37)
1532 self.assertEqual(light.brightness, 62)
1533 self.assertEqual(light.rgbw, (182, 0, 152, 25))
1534 self.assertEqual(light.getRgbw(), (255, 255, 255, 255))
1535 self.assertEqual(light.rgbww, (182, 0, 152, 25, 37))
1536 self.assertEqual(light.getRgbww(), (255, 255, 255, 255, 255))
1537 self.assertEqual(light.rgbcw, (182, 0, 152, 37, 25))
1538 self.assertEqual(light.getRgbcw(), (255, 255, 255, 255, 255))
1539 self.assertEqual(light.rgbwcapable, False)
1540 self.assertEqual(light.dimmable_effects, True)
1541 self.assertEqual(light._protocol.power_push_updates, True)
1542 self.assertEqual(light._protocol.state_push_updates, True)
1543 self.assertEqual(light.requires_turn_on, False)
1544 self.assertEqual(
1545 light.__str__(),
1546 (
1547 "ON [CCT: 4968K Brightness: 24% raw state: "
1548 "129,53,35,97,5,16,182,0,152,25,9,37,15,243,]"
1549 ),
1550 )
1551
1552 # Home Assistant legacy names
1553 light.set_effect("colorjump", 50, 100)
1554 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"88\x10d\xe4")))
1555
1556 # Library names
1557 light.set_effect("seven_color_jumping", 50, 50)
1558 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"88\x102\xb2")))
1559
1560 light.set_effect("rgb_cross_fade", 50, 60)
1561 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"8$\x10<\xa8")))
1562
1563 with pytest.raises(ValueError):
1564 light.set_effect("unknown", 50)
1565
1566 with pytest.raises(ValueError):
1567 light.setPresetPattern(0x38, 50, 200)
1568
1569 with pytest.raises(ValueError):
1570 light.setPresetPattern(0x99, 50, 100)
1571
1572 light._transition_complete_time = 0
1573 light.update_state()
1574 self.assertEqual(mock_read.call_count, 4)
1575 self.assertEqual(mock_send.call_count, 7)
1576 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
1577 self.assertEqual(light.mode, "preset")
1578 self.assertEqual(light.effect, "colorjump")
1579 self.assertEqual(light.brightness, 153)
1580
1581 self.assertEqual(light.preset_pattern_num, 0x38)
1582 self.assertEqual(
1583 light.__str__(),
1584 (
1585 "ON [Pattern: colorjump (Speed 50%) raw state: "
1586 "129,53,35,56,5,16,182,0,152,25,9,37,15,202,]"
1587 ),
1588 )
1589
1590 @patch("flux_led.WifiLedBulb._send_msg")
1591 @patch("flux_led.WifiLedBulb._read_msg")
1592 @patch("flux_led.WifiLedBulb.connect")
1593 def test_original_ledenet(self, mock_connect, mock_read, mock_send):
1594 calls = 0
1595
1596 def read_data(expected):
1597 nonlocal calls
1598 calls += 1
1599 if calls == 1:
1600 self.assertEqual(expected, 2)
1601 return bytearray(b"")
1602 if calls == 2:
1603 self.assertEqual(expected, 2)
1604 return bytearray(b"f\x01")
1605 if calls == 3:
1606 self.assertEqual(expected, 9)
1607 return bytearray(b"#A!\x08\xff\x80*\x01\x99")
1608 if calls == 4:
1609 self.assertEqual(expected, 11)
1610 return bytearray(b"f\x01#A!\x08\x01\x19P\x01\x99")
1611 if calls == 5: # ready turn off response
1612 self.assertEqual(expected, 4)
1613 return bytearray(b"\x0fq#\xa3")
1614 if calls == 6:
1615 self.assertEqual(expected, 11)
1616 return bytearray(b"f\x01$A!\x08\x01\x19P\x01\x99")
1617 if calls == 7: # ready turn on response
1618 self.assertEqual(expected, 4)
1619 return bytearray(b"\x0fq#\xa3")
1620 if calls == 8:
1621 self.assertEqual(expected, 11)
1622 return bytearray(b"f\x01#A!\x08\x01\x19P\x01\x99")
1623
1624 mock_read.side_effect = read_data
1625 light = flux_led.WifiLedBulb("192.168.1.164")
1626 assert light.color_modes == {COLOR_MODE_RGB}
1627 self.assertEqual(light.model_num, 0x01)
1628 self.assertEqual(light.model, "Legacy Controller RGB (0x01)")
1629 self.assertEqual(light.dimmable_effects, False)
1630 self.assertEqual(light.requires_turn_on, True)
1631 self.assertEqual(light.white_active, True)
1632 self.assertEqual(light._protocol.power_push_updates, False)
1633 self.assertEqual(light._protocol.state_push_updates, False)
1634
1635 self.assertEqual(mock_read.call_count, 3)
1636 self.assertEqual(mock_send.call_count, 2)
1637 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"\xef\x01w")))
1638
1639 light.setRgb(1, 25, 80)
1640 self.assertEqual(mock_read.call_count, 3)
1641 self.assertEqual(mock_send.call_count, 3)
1642 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"V\x01\x19P\xaa")))
1643
1644 light._transition_complete_time = 0
1645 light.update_state()
1646 self.assertEqual(mock_read.call_count, 4)
1647 self.assertEqual(mock_send.call_count, 4)
1648 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"\xef\x01w")))
1649
1650 self.assertEqual(
1651 light.__str__(),
1652 "ON [Color: (1, 25, 80) Brightness: 31% raw state: 102,1,35,65,33,8,1,25,80,1,153,0,]",
1653 )
1654 self.assertEqual(light.protocol, PROTOCOL_LEDENET_ORIGINAL)
1655 self.assertEqual(light.is_on, True)
1656 self.assertEqual(light.mode, "color")
1657 self.assertEqual(light.warm_white, 0)
1658 self.assertEqual(light.brightness, 80)
1659 self.assertEqual(light.getRgb(), (1, 25, 80))
1660
1661 light.turnOff()
1662 self.assertEqual(mock_read.call_count, 5)
1663 self.assertEqual(mock_send.call_count, 5)
1664 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"\xcc$3")))
1665
1666 light._transition_complete_time = 0
1667 light.update_state()
1668 self.assertEqual(mock_read.call_count, 6)
1669 self.assertEqual(mock_send.call_count, 6)
1670 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"\xef\x01w")))
1671
1672 self.assertEqual(
1673 light.__str__(),
1674 "OFF [Color: (1, 25, 80) Brightness: 31% raw state: 102,1,36,65,33,8,1,25,80,1,153,0,]",
1675 )
1676 self.assertEqual(light.protocol, PROTOCOL_LEDENET_ORIGINAL)
1677 self.assertEqual(light.is_on, False)
1678 self.assertEqual(light.mode, "color")
1679 self.assertEqual(light.warm_white, 0)
1680 self.assertEqual(light.brightness, 80)
1681 self.assertEqual(light.getRgb(), (1, 25, 80))
1682
1683 light.turnOn()
1684 self.assertEqual(mock_read.call_count, 7)
1685 self.assertEqual(mock_send.call_count, 7)
1686 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"\xcc#3")))
1687
1688 light._transition_complete_time = 0
1689 light.update_state()
1690 self.assertEqual(mock_read.call_count, 8)
1691 self.assertEqual(mock_send.call_count, 8)
1692 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"\xef\x01w")))
1693
1694 self.assertEqual(
1695 light.__str__(),
1696 "ON [Color: (1, 25, 80) Brightness: 31% raw state: 102,1,35,65,33,8,1,25,80,1,153,0,]",
1697 )
1698 self.assertEqual(light.protocol, PROTOCOL_LEDENET_ORIGINAL)
1699 self.assertEqual(light.is_on, True)
1700 self.assertEqual(light.mode, "color")
1701 self.assertEqual(light.warm_white, 0)
1702 self.assertEqual(light.cool_white, 0)
1703 self.assertEqual(light.brightness, 80)
1704 self.assertEqual(light.getRgb(), (1, 25, 80))
1705 self.assertEqual(light.version_num, 0)
1706
1707 light.set_effect("colorjump", 50, 100)
1708 self.assertEqual(mock_read.call_count, 8)
1709 self.assertEqual(mock_send.call_count, 9)
1710 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"\xbb8\x10D")))
1711
1712 @patch("flux_led.WifiLedBulb._send_msg")
1713 @patch("flux_led.WifiLedBulb._read_msg")
1714 @patch("flux_led.WifiLedBulb.connect")
1715 def test_original_ledenet_cct(self, mock_connect, mock_read, mock_send):
1716 calls = 0
1717
1718 def read_data(expected):
1719 nonlocal calls
1720 calls += 1
1721 if calls == 1:
1722 self.assertEqual(expected, 2)
1723 return bytearray(b"")
1724 if calls == 2:
1725 self.assertEqual(expected, 2)
1726 return bytearray(b"f\x03")
1727 if calls == 3:
1728 self.assertEqual(expected, 9)
1729 return bytearray(b"#A!\x08\xff\x80*\x01\x99")
1730 if calls == 4:
1731 self.assertEqual(expected, 11)
1732 return bytearray(b"f\x03#A!\x08\x01\x19P\x01\x99")
1733 if calls == 5: # ready turn off response
1734 self.assertEqual(expected, 4)
1735 return bytearray(b"\x0fq#\xa3")
1736 if calls == 6:
1737 self.assertEqual(expected, 11)
1738 return bytearray(b"f\x03$A!\x08\x01\x19P\x01\x99")
1739 if calls == 7: # ready turn on response
1740 self.assertEqual(expected, 4)
1741 return bytearray(b"\x0fq#\xa3")
1742 if calls == 8:
1743 self.assertEqual(expected, 11)
1744 return bytearray(b"f\x03#A!\x08\x01\x19P\x01\x99")
1745
1746 mock_read.side_effect = read_data
1747 light = flux_led.WifiLedBulb("192.168.1.164")
1748 assert light.color_modes == {COLOR_MODE_CCT}
1749 assert light.effect is None
1750 assert light.effect_list == [
1751 "Cool Flash",
1752 "Cool Gradual",
1753 "Warm Flash",
1754 "Warm Gradual",
1755 "random",
1756 ]
1757 self.assertEqual(light.model_num, 0x03)
1758 self.assertEqual(light.model, "Legacy Controller CCT (0x03)")
1759 self.assertEqual(light.dimmable_effects, False)
1760 self.assertEqual(light.requires_turn_on, True)
1761 self.assertEqual(light.white_active, True)
1762 self.assertEqual(light._protocol.power_push_updates, False)
1763 self.assertEqual(light._protocol.state_push_updates, False)
1764
1765 self.assertEqual(mock_read.call_count, 3)
1766 self.assertEqual(mock_send.call_count, 2)
1767 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"\xef\x01w")))
1768
1769 light.setWhiteTemperature(2700, 255)
1770 self.assertEqual(mock_read.call_count, 3)
1771 self.assertEqual(mock_send.call_count, 3)
1772 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"V\xff\x00\xaa")))
1773
1774 light._transition_complete_time = 0
1775 light.update_state()
1776 self.assertEqual(mock_read.call_count, 4)
1777 self.assertEqual(mock_send.call_count, 4)
1778 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"\xef\x01w")))
1779
1780 self.assertEqual(
1781 light.__str__(),
1782 "ON [CCT: 6354K Brightness: 10% raw state: 102,3,35,65,33,8,1,0,80,1,153,25,]",
1783 )
1784 self.assertEqual(light.protocol, PROTOCOL_LEDENET_ORIGINAL_CCT)
1785 self.assertEqual(light.is_on, True)
1786 self.assertEqual(light.mode, "ww")
1787 self.assertEqual(light.warm_white, 0)
1788 self.assertEqual(light.brightness, 26)
1789
1790 light.turnOff()
1791 self.assertEqual(mock_read.call_count, 5)
1792 self.assertEqual(mock_send.call_count, 5)
1793 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"\xcc$3")))
1794
1795 light._transition_complete_time = 0
1796 light.update_state()
1797 self.assertEqual(mock_read.call_count, 6)
1798 self.assertEqual(mock_send.call_count, 6)
1799 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"\xef\x01w")))
1800
1801 self.assertEqual(
1802 light.__str__(),
1803 "OFF [CCT: 6354K Brightness: 10% raw state: 102,3,36,65,33,8,1,0,80,1,153,25,]",
1804 )
1805 self.assertEqual(light.protocol, PROTOCOL_LEDENET_ORIGINAL_CCT)
1806 self.assertEqual(light.is_on, False)
1807 self.assertEqual(light.mode, "ww")
1808 self.assertEqual(light.cool_white, 0)
1809 self.assertEqual(light.warm_white, 0)
1810 self.assertEqual(light.brightness, 26)
1811
1812 light.turnOn()
1813 self.assertEqual(mock_read.call_count, 7)
1814 self.assertEqual(mock_send.call_count, 7)
1815 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"\xcc#3")))
1816
1817 light._transition_complete_time = 0
1818 light.update_state()
1819 self.assertEqual(mock_read.call_count, 8)
1820 self.assertEqual(mock_send.call_count, 8)
1821 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"\xef\x01w")))
1822
1823 self.assertEqual(
1824 light.__str__(),
1825 "ON [CCT: 6354K Brightness: 10% raw state: 102,3,35,65,33,8,1,0,80,1,153,25,]",
1826 )
1827 self.assertEqual(light.protocol, PROTOCOL_LEDENET_ORIGINAL_CCT)
1828 self.assertEqual(light.is_on, True)
1829 self.assertEqual(light.mode, "ww")
1830 self.assertEqual(light.warm_white, 0)
1831 self.assertEqual(light.cool_white, 0)
1832 self.assertEqual(light.brightness, 26)
1833 self.assertEqual(light.version_num, 0)
1834
1835 light.set_effect("Warm Flash", 50, 100)
1836 self.assertEqual(mock_read.call_count, 8)
1837 self.assertEqual(mock_send.call_count, 9)
1838 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"\xbb<\x10D")))
1839
1840 light.set_effect("Cool Gradual", 50, 100)
1841 self.assertEqual(mock_read.call_count, 8)
1842 self.assertEqual(mock_send.call_count, 10)
1843 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"\xbbJ\x10D")))
1844
1845 @patch("flux_led.WifiLedBulb._send_msg")
1846 @patch("flux_led.WifiLedBulb._read_msg")
1847 @patch("flux_led.WifiLedBulb.connect")
1848 def test_state_transition(self, mock_connect, mock_read, mock_send):
1849 calls = 0
1850
1851 def read_data(expected):
1852 nonlocal calls
1853 calls += 1
1854 if calls == 1:
1855 self.assertEqual(expected, 2)
1856 return bytearray(b"\x81E")
1857 if calls == 2:
1858 self.assertEqual(expected, 12)
1859 return bytearray(b"#a!\x10g\xffh\x00\x04\x00\xf0=")
1860 if calls == 3:
1861 self.assertEqual(expected, 14)
1862 return bytearray(b"\x81E#a!\x10\x01\x19P\x00\x04\x00\xf0\xd9")
1863 if calls == 4:
1864 self.assertEqual(expected, 14)
1865 return bytearray(b"\x81E#a!\x10\x01\x19P\x00\x04\x00\xf0\xd9")
1866
1867 mock_read.side_effect = read_data
1868 light = flux_led.WifiLedBulb("192.168.1.164")
1869 assert light.color_modes == {COLOR_MODE_RGB, COLOR_MODE_DIM}
1870
1871 self.assertEqual(mock_read.call_count, 2)
1872 self.assertEqual(mock_send.call_count, 1)
1873 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
1874
1875 light.setRgb(50, 100, 50)
1876 self.assertEqual(mock_read.call_count, 2)
1877 self.assertEqual(mock_send.call_count, 2)
1878 self.assertEqual(
1879 mock_send.call_args, mock.call(bytearray(b"12d2\x00\xf0\x0f\xf8"))
1880 )
1881 self.assertEqual(light.getRgb(), (50, 100, 50))
1882
1883 # While a transition is in progress we do not update
1884 # internal state
1885 light.update_state()
1886 self.assertEqual(light.getRgb(), (50, 100, 50))
1887
1888 # Now that the transition has completed state should
1889 # be updated, we mock the bulb to replay with an
1890 # RGB state of (1, 25, 80)
1891 light._transition_complete_time = 0
1892 light.update_state()
1893 self.assertEqual(mock_read.call_count, 4)
1894 self.assertEqual(mock_send.call_count, 4)
1895 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
1896
1897 self.assertEqual(
1898 light.__str__(),
1899 "ON [Color: (1, 25, 80) Brightness: 31% raw state: 129,69,35,97,33,16,1,25,80,0,4,0,240,217,]",
1900 )
1901 self.assertEqual(light.protocol, PROTOCOL_LEDENET_8BYTE)
1902 self.assertEqual(light.is_on, True)
1903 self.assertEqual(light.mode, "color")
1904 self.assertEqual(light.warm_white, 0)
1905 self.assertEqual(light.brightness, 80)
1906 self.assertEqual(light.getRgb(), (1, 25, 80))
1907 self.assertEqual(light.device_type, flux_led.DeviceType.Bulb)
1908
1909 def test_rgbww_brightness(self):
1910 assert rgbww_brightness((128, 128, 128, 128, 128), 255) == (
1911 255,
1912 255,
1913 255,
1914 255,
1915 255,
1916 )
1917 assert rgbww_brightness((128, 128, 128, 128, 128), 128) == (
1918 128,
1919 128,
1920 128,
1921 128,
1922 128,
1923 )
1924 assert rgbww_brightness((255, 255, 255, 255, 255), 128) == (
1925 128,
1926 128,
1927 128,
1928 128,
1929 128,
1930 )
1931 assert rgbww_brightness((0, 255, 0, 0, 0), 255) == (0, 255, 0, 255, 255)
1932 assert rgbww_brightness((0, 255, 0, 0, 0), 128) == (0, 255, 0, 64, 64)
1933
1934 def test_rgbcw_brightness(self):
1935 assert rgbcw_brightness((128, 128, 128, 128, 128), 255) == (
1936 255,
1937 255,
1938 255,
1939 255,
1940 255,
1941 )
1942 assert rgbcw_brightness((128, 128, 128, 128, 128), 128) == (
1943 128,
1944 128,
1945 128,
1946 128,
1947 128,
1948 )
1949 assert rgbcw_brightness((255, 255, 255, 255, 255), 128) == (
1950 128,
1951 128,
1952 128,
1953 128,
1954 128,
1955 )
1956 assert rgbcw_brightness((0, 255, 0, 0, 0), 255) == (0, 255, 0, 255, 255)
1957 assert rgbcw_brightness((0, 255, 0, 0, 0), 128) == (0, 255, 0, 64, 64)
1958
1959 def test_rgbw_brightness(self):
1960 assert rgbw_brightness((128, 128, 128, 128), 255) == (255, 255, 255, 255)
1961 assert rgbw_brightness((128, 128, 128, 128), 128) == (128, 128, 128, 128)
1962 assert rgbw_brightness((255, 255, 255, 255), 128) == (128, 128, 128, 128)
1963 assert rgbw_brightness((0, 255, 0, 0), 255) == (0, 255, 0, 255)
1964 assert rgbw_brightness((0, 255, 0, 0), 128) == (0, 255, 0, 0)
1965
1966 def test_rgbwc_to_rgbcw_rgbcw_to_rgbwc_round_trip(self):
1967 rgbwc = (1, 2, 3, 4, 5)
1968 rgbcw = rgbwc_to_rgbcw(rgbwc)
1969 assert rgbcw == (1, 2, 3, 5, 4)
1970 assert rgbcw_to_rgbwc(rgbcw) == rgbwc
1971
1972 def test_color_object_to_tuple(self):
1973 assert utils.color_object_to_tuple("red") == (255, 0, 0)
1974 assert utils.color_object_to_tuple("green") == (0, 128, 0)
1975 assert utils.color_object_to_tuple("blue") == (0, 0, 255)
1976 green = (0, 255, 0)
1977 assert utils.color_object_to_tuple(green) == green
1978 assert utils.color_object_to_tuple(set()) is None
1979 assert utils.color_object_to_tuple("#ff00ff") == (255, 0, 255)
1980 assert utils.color_object_to_tuple("(255,0,255)") == (255, 0, 255)
1981
1982 def test_get_color_names_list(self):
1983 names = utils.get_color_names_list()
1984 assert len(names) > 120
1985 assert "springgreen" in names
1986 assert "yellow" in names
1987
1988 def test_color_tuple_to_string(self):
1989 assert utils.color_tuple_to_string((255, 0, 0)) == "red"
1990 assert utils.color_tuple_to_string((0, 128, 0)) == "green"
1991 assert utils.color_tuple_to_string((0, 0, 255)) == "blue"
1992 assert utils.color_tuple_to_string((3, 2, 1)) == "(3, 2, 1)"
1993
1994 def test_color_temp_to_white_levels(self):
1995 assert color_temp_to_white_levels(2700, 255) == (255, 0)
1996 assert color_temp_to_white_levels(4600, 255) == (128, 128)
1997 assert color_temp_to_white_levels(5000, 255) == (101, 154)
1998 assert color_temp_to_white_levels(6500, 255) == (0, 255)
1999 assert color_temp_to_white_levels(2700, 128) == (128, 0)
2000 assert color_temp_to_white_levels(4600, 128) == (64, 64)
2001 assert color_temp_to_white_levels(5000, 128) == (50, 77)
2002 assert color_temp_to_white_levels(6500, 128) == (0, 128)
2003 assert color_temp_to_white_levels(6500, 255) == (0, 255)
2004 with pytest.raises(ValueError):
2005 color_temp_to_white_levels(6500, -1)
2006
2007 def test_white_levels_to_color_temp(self):
2008 assert white_levels_to_color_temp(0, 255) == (6500, 255)
2009 assert white_levels_to_color_temp(255, 255) == (4600, 255)
2010 assert white_levels_to_color_temp(128, 128) == (4600, 255)
2011 assert white_levels_to_color_temp(255, 0) == (2700, 255)
2012 assert white_levels_to_color_temp(0, 128) == (6500, 128)
2013 assert white_levels_to_color_temp(64, 64) == (4600, 128)
2014 assert white_levels_to_color_temp(77, 50) == (4196, 127)
2015 assert white_levels_to_color_temp(128, 0) == (2700, 128)
2016 assert white_levels_to_color_temp(0, 0) == (2700, 0)
2017 with pytest.raises(ValueError):
2018 white_levels_to_color_temp(-1, 0)
2019 with pytest.raises(ValueError):
2020 white_levels_to_color_temp(0, 500)
2021
2022 def test_scaled_color_temp_to_white_levels(self):
2023 assert scaled_color_temp_to_white_levels(0, 100) == (255, 0)
2024 assert scaled_color_temp_to_white_levels(50, 100) == (128, 128)
2025 assert scaled_color_temp_to_white_levels(76, 100) == (61, 194)
2026 assert scaled_color_temp_to_white_levels(100, 100) == (0, 255)
2027 assert scaled_color_temp_to_white_levels(42, 50) == (74, 54)
2028 assert scaled_color_temp_to_white_levels(71, 50) == (37, 91)
2029 assert scaled_color_temp_to_white_levels(77, 50) == (29, 98)
2030 assert scaled_color_temp_to_white_levels(100, 50) == (0, 128)
2031 assert scaled_color_temp_to_white_levels(100, 100) == (0, 255)
2032 with pytest.raises(ValueError):
2033 scaled_color_temp_to_white_levels(100, -1)
2034 with pytest.raises(ValueError):
2035 scaled_color_temp_to_white_levels(-1, 100)
2036
2037 def test_white_levels_to_scaled_color_temp(self):
2038 assert white_levels_to_scaled_color_temp(0, 255) == (100, 100)
2039 assert white_levels_to_scaled_color_temp(255, 255) == (50, 100)
2040 assert white_levels_to_scaled_color_temp(128, 128) == (50, 100)
2041 assert white_levels_to_scaled_color_temp(255, 0) == (0, 100)
2042 assert white_levels_to_scaled_color_temp(0, 128) == (100, 50)
2043 assert white_levels_to_scaled_color_temp(64, 64) == (50, 50)
2044 assert white_levels_to_scaled_color_temp(77, 50) == (39, 50)
2045 assert white_levels_to_scaled_color_temp(128, 0) == (0, 50)
2046 assert white_levels_to_scaled_color_temp(0, 0) == (0, 0)
2047 with pytest.raises(ValueError):
2048 white_levels_to_scaled_color_temp(-1, 0)
2049 with pytest.raises(ValueError):
2050 white_levels_to_scaled_color_temp(0, 500)
2051
2052 @patch("flux_led.WifiLedBulb._send_msg")
2053 @patch("flux_led.WifiLedBulb._read_msg")
2054 @patch("flux_led.WifiLedBulb.connect")
2055 def test_unknown_model_detection_rgbw_cct(self, mock_connect, mock_read, mock_send):
2056 calls = 0
2057 model_not_in_db = 222
2058
2059 def read_data(expected):
2060 nonlocal calls
2061 calls += 1
2062 if calls == 1:
2063 self.assertEqual(expected, 2)
2064 return bytearray([129, model_not_in_db])
2065 if calls == 2:
2066 self.assertEqual(expected, 12)
2067 return bytearray(b"$$\x47\x00\x00\x00\x00\x00\x02\x00\x00\xf0")
2068 if calls == 3:
2069 self.assertEqual(expected, 14)
2070 return bytearray(
2071 b"\x81\xde\x23\x41\x47\x00\x00\x00\x00\x00\x02\xff\x00\x0b"
2072 )
2073
2074 mock_read.side_effect = read_data
2075 light = flux_led.WifiLedBulb("192.168.1.164")
2076 assert light.color_modes == {COLOR_MODE_RGB, COLOR_MODE_CCT}
2077 self.assertEqual(light.model_num, 0xDE)
2078 self.assertEqual(light.model, "Unknown Model (0xDE)")
2079 assert light.color_mode == COLOR_MODE_RGB
2080 light.update_state()
2081 assert light.color_mode == COLOR_MODE_CCT
2082 self.assertEqual(light.color_temp, 6500)
2083 self.assertEqual(light.isOn(), True)
2084 self.assertEqual(light.getCCT(), (0, 255))
2085 self.assertEqual(light.getWarmWhite255(), 255)
2086 self.assertEqual(light.getWhiteTemperature(), (6500, 255))
2087 self.assertEqual(
2088 light.__str__(),
2089 "ON [CCT: 6500K Brightness: 100% raw state: 129,222,35,65,71,0,0,0,0,0,2,255,0,11,]",
2090 )
2091
2092 @patch("flux_led.WifiLedBulb._send_msg")
2093 @patch("flux_led.WifiLedBulb._read_msg")
2094 @patch("flux_led.WifiLedBulb.connect")
2095 def test_unknown_model_detection_rgb_dim(self, mock_connect, mock_read, mock_send):
2096 calls = 0
2097 model_not_in_db = 222
2098
2099 def read_data(expected):
2100 nonlocal calls
2101 calls += 1
2102 if calls == 1:
2103 self.assertEqual(expected, 2)
2104 return bytearray([129, model_not_in_db])
2105 if calls == 2:
2106 self.assertEqual(expected, 12)
2107 return bytearray(b"$$\x46\x00\x00\x00\x00\x00\x02\x00\x00\xef")
2108
2109 mock_read.side_effect = read_data
2110 switch = flux_led.WifiLedBulb("192.168.1.164")
2111 assert switch.color_modes == {COLOR_MODE_RGB, COLOR_MODE_DIM}
2112
2113 @patch("flux_led.WifiLedBulb._send_msg")
2114 @patch("flux_led.WifiLedBulb._read_msg")
2115 @patch("flux_led.WifiLedBulb.connect")
2116 def test_unknown_model_detection_rgbww(self, mock_connect, mock_read, mock_send):
2117 calls = 0
2118 model_not_in_db = 222
2119
2120 def read_data(expected):
2121 nonlocal calls
2122 calls += 1
2123 if calls == 1:
2124 self.assertEqual(expected, 2)
2125 return bytearray([129, model_not_in_db])
2126 if calls == 2:
2127 self.assertEqual(expected, 12)
2128 return bytearray(b"$$\x45\x00\x00\x00\x00\x00\x02\x00\x00\xee")
2129
2130 mock_read.side_effect = read_data
2131 light = flux_led.WifiLedBulb("192.168.1.164")
2132 assert light.color_modes == {COLOR_MODE_RGBWW, COLOR_MODE_CCT}
2133
2134 @patch("flux_led.WifiLedBulb._send_msg")
2135 @patch("flux_led.WifiLedBulb._read_msg")
2136 @patch("flux_led.WifiLedBulb.connect")
2137 def test_unknown_model_detection_rgbw(self, mock_connect, mock_read, mock_send):
2138 calls = 0
2139 model_not_in_db = 222
2140
2141 def read_data(expected):
2142 nonlocal calls
2143 calls += 1
2144 if calls == 1:
2145 self.assertEqual(expected, 2)
2146 return bytearray([129, model_not_in_db])
2147 if calls == 2:
2148 self.assertEqual(expected, 12)
2149 return bytearray(b"$$\x44\x00\x00\x00\x00\x00\x02\x00\x00\xed")
2150
2151 mock_read.side_effect = read_data
2152 light = flux_led.WifiLedBulb("192.168.1.164")
2153 assert light.color_modes == {COLOR_MODE_RGBW, COLOR_MODE_CCT}
2154 self.assertEqual(light.color_mode, COLOR_MODE_RGBW)
2155
2156 light.setWhiteTemperature(light.max_temp, 255)
2157 self.assertEqual(mock_read.call_count, 2)
2158 self.assertEqual(mock_send.call_count, 2)
2159 self.assertEqual(
2160 mock_send.call_args,
2161 mock.call(bytearray(b"1\xff\xff\xff\x00\x00\x0f=")),
2162 )
2163 self.assertEqual(light.color_mode, COLOR_MODE_CCT)
2164
2165 light.setWhiteTemperature(light.min_temp, 255)
2166 self.assertEqual(mock_read.call_count, 2)
2167 self.assertEqual(mock_send.call_count, 3)
2168 self.assertEqual(
2169 mock_send.call_args,
2170 mock.call(bytearray(b"1\x00\x00\x00\xff\x00\x0f?")),
2171 )
2172 self.assertEqual(light.color_mode, COLOR_MODE_CCT)
2173
2174 light.setWhiteTemperature(
2175 light.max_temp - ((light.max_temp - light.min_temp) / 2), 255
2176 )
2177 self.assertEqual(mock_read.call_count, 2)
2178 self.assertEqual(mock_send.call_count, 4)
2179 self.assertEqual(
2180 mock_send.call_args,
2181 mock.call(bytearray(b"1\x80\x80\x80\x80\x00\x0f@")),
2182 )
2183 self.assertEqual(light.color_mode, COLOR_MODE_CCT)
2184
2185 @patch("flux_led.WifiLedBulb._send_msg")
2186 @patch("flux_led.WifiLedBulb._read_msg")
2187 @patch("flux_led.WifiLedBulb.connect")
2188 def test_single_channel_remapping(self, mock_connect, mock_read, mock_send):
2189 calls = 0
2190
2191 def read_data(expected):
2192 nonlocal calls
2193 calls += 1
2194 if calls == 1:
2195 self.assertEqual(expected, 2)
2196 return bytearray(b"\x81\x41")
2197 if calls == 2:
2198 self.assertEqual(expected, 12)
2199 return bytearray(b"#a\x41\x10\xff\x00\x00\x00\x04\x00\xf0\x8a")
2200 if calls == 3:
2201 self.assertEqual(expected, 14)
2202 return bytearray(b"\x81\x41#a\x41\x10\x64\x00\x00\x00\x04\x00\xf0\xef")
2203 raise ValueError("Too many calls")
2204
2205 mock_read.side_effect = read_data
2206 light = flux_led.WifiLedBulb("192.168.1.164")
2207 self.assertEqual(light.model_num, 0x41)
2208 self.assertEqual(light.model, "Controller Dimmable (0x41)")
2209 assert light.color_modes == {COLOR_MODE_DIM}
2210
2211 self.assertEqual(mock_read.call_count, 2)
2212 self.assertEqual(mock_send.call_count, 1)
2213 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
2214
2215 self.assertEqual(
2216 light.__str__(),
2217 "ON [Warm White: 100% raw state: 129,65,35,97,65,16,0,0,0,255,4,0,240,138,]",
2218 )
2219 self.assertEqual(light.protocol, PROTOCOL_LEDENET_8BYTE_AUTO_ON)
2220 self.assertEqual(light.is_on, True)
2221 self.assertEqual(light.mode, "ww")
2222 self.assertEqual(light.warm_white, 0)
2223 self.assertEqual(light.brightness, 255)
2224 self.assertEqual(light.rgbwcapable, False)
2225 self.assertEqual(light.device_type, flux_led.DeviceType.Bulb)
2226
2227 light.setRgbw(0, 0, 0, w=0x80)
2228 self.assertEqual(mock_read.call_count, 2)
2229 self.assertEqual(mock_send.call_count, 2)
2230 self.assertEqual(
2231 mock_send.call_args, mock.call(bytearray(b"1\x80\x00\x00\x00\x00\x0f\xc0"))
2232 )
2233 assert light.raw_state.warm_white == 0x80
2234 self.assertEqual(
2235 light.__str__(),
2236 "ON [Warm White: 50% raw state: 129,65,35,97,65,16,0,0,0,128,4,0,240,138,]",
2237 )
2238
2239 # Update state now assumes its externally set to 100
2240 light._transition_complete_time = 0
2241 light.update_state()
2242 self.assertEqual(mock_read.call_count, 3)
2243 assert light.raw_state.warm_white == 100
2244 self.assertEqual(light.getWarmWhite255(), 100)
2245 self.assertEqual(light.brightness, 100)
2246 self.assertEqual(
2247 light.__str__(),
2248 "ON [Warm White: 39% raw state: 129,65,35,97,65,16,0,0,0,100,4,0,240,239,]",
2249 )
2250
2251 light._set_power_state(light._protocol.off_byte)
2252 self.assertEqual(
2253 light.__str__(),
2254 "OFF [Warm White: 39% raw state: 129,65,36,97,65,16,0,0,0,100,4,0,240,239,]",
2255 )
2256 light._set_power_state(light._protocol.on_byte)
2257 self.assertEqual(
2258 light.__str__(),
2259 "ON [Warm White: 39% raw state: 129,65,35,97,65,16,0,0,0,100,4,0,240,239,]",
2260 )
2261 light._replace_raw_state(
2262 {STATE_RED: 255, STATE_GREEN: 0, STATE_BLUE: 0, STATE_WARM_WHITE: 0}
2263 )
2264 self.assertEqual(
2265 light.__str__(),
2266 "ON [Warm White: 100% raw state: 129,65,35,97,65,16,0,0,0,255,4,0,240,239,]",
2267 )
2268 # Verify we do not remap states that have not changed
2269 light._replace_raw_state({STATE_BLUE: 0})
2270 self.assertEqual(
2271 light.__str__(),
2272 "ON [Warm White: 100% raw state: 129,65,35,97,65,16,0,0,0,255,4,0,240,239,]",
2273 )
2274 # Verify we do not remap states that have not changed
2275 light._replace_raw_state({STATE_GREEN: 255, STATE_BLUE: 255})
2276 self.assertEqual(
2277 light.__str__(),
2278 "ON [Warm White: 100% raw state: 129,65,35,97,65,16,0,255,255,255,4,0,240,239,]",
2279 )
2280 self.assertEqual(light.dimmable_effects, False)
2281 self.assertEqual(light.requires_turn_on, False)
2282 self.assertEqual(light._protocol.power_push_updates, False)
2283 self.assertEqual(light._protocol.state_push_updates, False)
2284
2285 @patch("flux_led.WifiLedBulb._send_msg")
2286 @patch("flux_led.WifiLedBulb._read_msg")
2287 @patch("flux_led.WifiLedBulb.connect")
2288 def test_addressable_strip_effects_a2(self, mock_connect, mock_read, mock_send):
2289 calls = 0
2290
2291 def read_data(expected):
2292 nonlocal calls
2293 calls += 1
2294 if calls == 1:
2295 self.assertEqual(expected, 2)
2296 return bytearray(b"\x81\xa2")
2297 if calls == 2:
2298 self.assertEqual(expected, 12)
2299 return bytearray(b"#a\x41\x10\xff\x00\x00\x00\x04\x00\xf0\xeb")
2300 if calls == 3:
2301 self.assertEqual(expected, 14)
2302 return bytearray(
2303 b"\x81\xa2#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\xd4"
2304 )
2305 if calls == 4:
2306 self.assertEqual(expected, 14)
2307 return bytearray(
2308 b"\x81\xa2#\x24\x01\x10\x64\x00\x00\x00\x04\x00\xf0\xd3"
2309 )
2310 raise ValueError("Too many calls")
2311
2312 mock_read.side_effect = read_data
2313 light = flux_led.WifiLedBulb("192.168.1.164")
2314 self.assertEqual(light.speed_adjust_off, False)
2315 self.assertEqual(light.model_num, 0xA2)
2316 self.assertEqual(light.microphone, True)
2317 self.assertEqual(light.model, "Addressable v2 (0xA2)")
2318 assert len(light.effect_list) == 105
2319 assert light.color_modes == {COLOR_MODE_RGB}
2320
2321 self.assertEqual(mock_read.call_count, 2)
2322 self.assertEqual(mock_send.call_count, 1)
2323 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
2324
2325 self.assertEqual(
2326 light.__str__(),
2327 "ON [Color: (255, 0, 0) Brightness: 100% raw state: 129,162,35,97,65,16,255,0,0,0,4,0,240,235,]",
2328 )
2329 self.assertEqual(light.protocol, PROTOCOL_LEDENET_ADDRESSABLE_A2)
2330 self.assertEqual(light.is_on, True)
2331 self.assertEqual(light.mode, "color")
2332 self.assertEqual(light.warm_white, 0)
2333 self.assertEqual(light.brightness, 255)
2334 self.assertEqual(light.rgbwcapable, False)
2335 self.assertEqual(light.device_type, flux_led.DeviceType.Bulb)
2336 self.assertEqual(light.dimmable_effects, True)
2337 self.assertEqual(light.requires_turn_on, False)
2338 self.assertEqual(light._protocol.power_push_updates, False)
2339 self.assertEqual(light._protocol.state_push_updates, False)
2340
2341 light.setRgbw(0, 255, 0)
2342 self.assertEqual(mock_read.call_count, 2)
2343 self.assertEqual(mock_send.call_count, 2)
2344 self.assertEqual(
2345 mock_send.call_args,
2346 mock.call(bytearray(b"A\x01\x00\xff\x00\x00\x00\x00`\xff\x00\x00\xa0")),
2347 )
2348
2349 light.set_effect("RBM 1", 50)
2350 self.assertEqual(mock_read.call_count, 2)
2351 self.assertEqual(mock_send.call_count, 3)
2352 self.assertEqual(
2353 mock_send.call_args,
2354 mock.call(bytearray(b"B\x012d\xd9")),
2355 )
2356 light._transition_complete_time = 0
2357 light.update_state()
2358 self.assertEqual(
2359 light.__str__(),
2360 "ON [Pattern: RBM 1 (Speed 16%) raw state: 129,162,35,37,1,16,100,0,0,0,4,0,240,212,]",
2361 )
2362 assert light.effect == "RBM 1"
2363 assert light.brightness == 255
2364 assert light.getSpeed() == 16
2365 light.update_state()
2366 self.assertEqual(
2367 light.__str__(),
2368 "ON [Pattern: Multi Color Static (Speed 16%) raw state: 129,162,35,36,1,16,100,0,0,0,4,0,240,211,]",
2369 )
2370 assert light.effect == "Multi Color Static"
2371 assert light.brightness == 255
2372 assert light.getSpeed() == 16
2373
2374 with pytest.raises(ValueError):
2375 light.setPresetPattern(1, 50, 200)
2376
2377 with pytest.raises(ValueError):
2378 light.setPresetPattern(105, 50, 100)
2379
2380 @patch("flux_led.WifiLedBulb._send_msg")
2381 @patch("flux_led.WifiLedBulb._read_msg")
2382 @patch("flux_led.WifiLedBulb.connect")
2383 def test_addressable_strip_effects_a3(self, mock_connect, mock_read, mock_send):
2384 calls = 0
2385
2386 def read_data(expected):
2387 nonlocal calls
2388 calls += 1
2389 if calls == 1:
2390 self.assertEqual(expected, 2)
2391 return bytearray(b"\x81\xa3")
2392 if calls == 2:
2393 self.assertEqual(expected, 12)
2394 return bytearray(b"#a\x41\x10\xff\x00\x00\x00\x04\x00\xf0\xec")
2395 if calls == 3:
2396 self.assertEqual(expected, 14)
2397 return bytearray(
2398 b"\x81\xa3#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\xd5"
2399 )
2400 raise ValueError("Too many calls")
2401
2402 mock_read.side_effect = read_data
2403 light = flux_led.WifiLedBulb("192.168.1.164")
2404 self.assertEqual(light.speed_adjust_off, True)
2405 self.assertEqual(light.model_num, 0xA3)
2406 self.assertEqual(light.microphone, True)
2407 self.assertEqual(light.model, "Addressable v3 (0xA3)")
2408 assert len(light.effect_list) == 105
2409 assert light.color_modes == {COLOR_MODE_RGB}
2410
2411 self.assertEqual(mock_read.call_count, 2)
2412 self.assertEqual(mock_send.call_count, 1)
2413 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
2414
2415 self.assertEqual(
2416 light.__str__(),
2417 "ON [Color: (255, 0, 0) Brightness: 100% raw state: 129,163,35,97,65,16,255,0,0,0,4,0,240,236,]",
2418 )
2419 self.assertEqual(light.protocol, PROTOCOL_LEDENET_ADDRESSABLE_A3)
2420 self.assertEqual(light.is_on, True)
2421 self.assertEqual(light.mode, "color")
2422 self.assertEqual(light.warm_white, 0)
2423 self.assertEqual(light.brightness, 255)
2424 self.assertEqual(light.rgbwcapable, False)
2425 self.assertEqual(light.device_type, flux_led.DeviceType.Bulb)
2426 self.assertEqual(light.dimmable_effects, True)
2427 self.assertEqual(light.requires_turn_on, False)
2428 self.assertEqual(light._protocol.power_push_updates, True)
2429 self.assertEqual(light._protocol.state_push_updates, True)
2430
2431 light.setRgbw(0, 255, 0)
2432 self.assertEqual(mock_read.call_count, 2)
2433 self.assertEqual(mock_send.call_count, 2)
2434 self.assertEqual(
2435 mock_send.call_args,
2436 mock.call(
2437 bytearray(
2438 b"\xb0\xb1\xb2\xb3\x00\x01\x01\x00\x00\rA\x01\x00\xff\x00\x00\x00\x00`\xff\x00\x00\xa0\x15"
2439 )
2440 ),
2441 )
2442
2443 light.set_effect("RBM 1", 50)
2444 self.assertEqual(mock_read.call_count, 2)
2445 self.assertEqual(mock_send.call_count, 3)
2446 self.assertEqual(
2447 mock_send.call_args,
2448 mock.call(
2449 bytearray(b"\xb0\xb1\xb2\xb3\x00\x01\x01\x01\x00\x05B\x012d\xd9\x80")
2450 ),
2451 )
2452 light._transition_complete_time = 0
2453 light.update_state()
2454 self.assertEqual(
2455 light.__str__(),
2456 "ON [Pattern: RBM 1 (Speed 16%) raw state: 129,163,35,37,1,16,100,0,0,0,4,0,240,213,]",
2457 )
2458 assert light.effect == "RBM 1"
2459 assert light.brightness == 255
2460 assert light.getSpeed() == 16
2461
2462 data = light._protocol.construct_zone_change(
2463 2, [(255, 255, 255), (0, 255, 0)], 100, MultiColorEffects.STATIC
2464 )
2465 assert data == (
2466 b"\xb0\xb1\xb2\xb3\x00\x01\x01\x03\x00\x0fY\x00\x0f\xff\xff\xff\x00\xff\x00\x00\x1e\x01d\x00\xe7\xa8"
2467 )
2468 data = light._protocol.construct_zone_change(
2469 4, [(255, 255, 255), (0, 255, 0)], 100, MultiColorEffects.STATIC
2470 )
2471 assert data == (
2472 b"\xb0\xb1\xb2\xb3\x00\x01\x01\x04\x00\x15Y\x00\x15\xff\xff\xff\xff\xff\xff\x00\xff\x00\x00\xff\x00\x00\x1e\x01d\x00\xe9\xb3"
2473 )
2474
2475 @patch("flux_led.WifiLedBulb._send_msg")
2476 @patch("flux_led.WifiLedBulb._read_msg")
2477 @patch("flux_led.WifiLedBulb.connect")
2478 def test_original_addressable_strip_effects(
2479 self, mock_connect, mock_read, mock_send
2480 ):
2481 calls = 0
2482
2483 def read_data(expected):
2484 nonlocal calls
2485 calls += 1
2486 if calls == 1:
2487 self.assertEqual(expected, 2)
2488 return bytearray(b"\x81\xa1")
2489 if calls == 2:
2490 self.assertEqual(expected, 12)
2491 return bytearray(b"#a\x41\x10\xff\x00\x00\x00\x04\x00\xf0\xea")
2492 if calls == 3:
2493 self.assertEqual(expected, 14)
2494 return bytearray(
2495 b"\x81\xa1#\x00\xa1\x01\x64\x00\x00\x00\x04\x00\xf0\x3f"
2496 )
2497 if calls == 4:
2498 self.assertEqual(expected, 14)
2499 return bytearray(
2500 b"\x81\xa1\x23\x00\x61\x64\x07\x00\x21\x03\x03\x01\x2c\x65"
2501 )
2502 raise ValueError("Too many calls")
2503
2504 mock_read.side_effect = read_data
2505 light = flux_led.WifiLedBulb("192.168.1.164")
2506 self.assertEqual(light.speed_adjust_off, False)
2507 self.assertEqual(light.dimmable_effects, False)
2508 self.assertEqual(light._protocol.power_push_updates, True)
2509 self.assertEqual(light._protocol.state_push_updates, False)
2510 self.assertEqual(light.requires_turn_on, False)
2511 self.assertEqual(light.model_num, 0xA1)
2512
2513 self.assertEqual(light.model, "Addressable v1 (0xA1)")
2514 assert len(light.effect_list) == 301
2515 assert light.color_modes == {COLOR_MODE_RGB}
2516
2517 self.assertEqual(mock_read.call_count, 2)
2518 self.assertEqual(mock_send.call_count, 1)
2519 self.assertEqual(mock_send.call_args, mock.call(bytearray(LEDENET_STATE_QUERY)))
2520
2521 self.assertEqual(
2522 light.__str__(),
2523 "ON [Color: (255, 0, 0) Brightness: 100% raw state: 129,161,35,97,65,16,255,0,0,0,4,0,240,234,]",
2524 )
2525 self.assertEqual(light.protocol, PROTOCOL_LEDENET_ADDRESSABLE_A1)
2526 self.assertEqual(light.is_on, True)
2527 self.assertEqual(light.mode, "color")
2528 self.assertEqual(light.warm_white, 0)
2529 self.assertEqual(light.brightness, 255)
2530 self.assertEqual(light.rgbwcapable, False)
2531 self.assertEqual(light.device_type, flux_led.DeviceType.Bulb)
2532
2533 light.setRgbw(0, 255, 0)
2534 self.assertEqual(mock_read.call_count, 2)
2535 self.assertEqual(mock_send.call_count, 2)
2536 self.assertEqual(
2537 mock_send.call_args,
2538 mock.call(bytearray(b"1\x00\xff\x00\x00\x00\xf0\x0f/")),
2539 )
2540
2541 light.set_effect(
2542 "Overlay circularly, 7 colors with black background from start to end", 50
2543 )
2544 self.assertEqual(mock_read.call_count, 2)
2545 self.assertEqual(mock_send.call_count, 3)
2546 self.assertEqual(mock_send.call_args, mock.call(bytearray(b"a\x00\xa12\x0fC")))
2547 assert light.brightness == 255
2548
2549 light._transition_complete_time = 0
2550 light.update_state()
2551 self.assertEqual(
2552 light.__str__(),
2553 "ON [Pattern: Overlay circularly, 7 colors with black background from start to end (Speed 1%) raw state: 129,161,35,0,161,1,100,0,0,0,4,0,240,63,]",
2554 )
2555 assert (
2556 light.effect
2557 == "Overlay circularly, 7 colors with black background from start to end"
2558 )
2559 assert light.getSpeed() == 1
2560 light.set_effect("random", 50)
2561 self.assertEqual(mock_send.call_count, 5)
2562
2563 light.set_levels(128, 0, 0)
2564 self.assertEqual(mock_read.call_count, 3)
2565 self.assertEqual(mock_send.call_count, 6)
2566 self.assertEqual(
2567 mock_send.call_args,
2568 mock.call(bytearray(b"1\x80\x00\x00\x00\x00\xf0\x0f\xb0")),
2569 )
2570 light.update_state()
2571 assert light.effect is None
2572 assert light.brightness == 128
2573
2574 with pytest.raises(ValueError):
2575 light.setPresetPattern(1, 50, 200)
2576
2577 with pytest.raises(ValueError):
2578 light.setPresetPattern(305, 50, 100)