Debian Codegraph @

tests/test_light.py

raw symbols SCIP
1 """BleBox light entities tests."""
2
3 import json
4
5 from .conftest import CommonEntity, DefaultBoxTest, future_date, jmerge
6
7 from blebox_uniapi.box_types import get_latest_api_level
8 from blebox_uniapi.light import Light
9
10 # TODO: remove
11 import colorsys
12
13 import pytest
14
15 # TODO: remove
16 ATTR_BRIGHTNESS = "brightness"
17 ATTR_HS_COLOR = "ATTR_HS_COLOR"
18 ATTR_WHITE_VALUE = "ATTR_WHITE_VALUE"
19 ATTR_RGB_COLOR = "rgb_color"
20 ATTR_RGBW_COLOR = "rgbw_color"
21 ATTR_EFFECT = "effect"
22 ATTR_COLOR_TEMP = "color_temp"
23 ATTR_RGBWW_COLOR = "rgbww_color"
24 SUPPORT_BRIGHTNESS = 1
25 SUPPORT_COLOR = 2
26 SUPPORT_WHITE_VALUE = 4
27 COLOR_MODE_BRIGHTNESS = "brightness"
28 COLOR_MODE_COLOR_TEMP = "color_temp"
29 COLOR_MODE_ONOFF = "onoff"
30 COLOR_MODE_RGB = "rgb"
31 COLOR_MODE_RGBW = "rgbw"
32 COLOR_MODE_RGBWW = "rgbww"
33
34 COLOR_MODE_MAP = {
35 1: COLOR_MODE_RGBW,
36 2: COLOR_MODE_RGB,
37 3: COLOR_MODE_BRIGHTNESS,
38 4: COLOR_MODE_RGBW,
39 # RGB and Brightness 2 and 3 implementation difference, if W hex is not null only this or RGB + Brightness separated with mask
40 5: COLOR_MODE_COLOR_TEMP,
41 6: COLOR_MODE_COLOR_TEMP, # two instances
42 7: COLOR_MODE_RGBWW,
43 }
44
45 # NOTE: copied from Home Assistant color util module
46
47
48 def rgb_hex_to_rgb_list(hex_string: str):
49 """Return an RGB color value list from a hex color string."""
50 return [
51 int(hex_string[i : i + len(hex_string) // 3], 16)
52 for i in range(0, len(hex_string), len(hex_string) // 3)
53 ]
54
55
56 def color_hsv_to_RGB(iH: float, iS: float, iV: float):
57 """Convert an hsv color into its rgb representation.
58
59 Hue is scaled 0-360
60 Sat is scaled 0-100
61 Val is scaled 0-100
62 """
63 fRGB = colorsys.hsv_to_rgb(iH / 360, iS / 100, iV / 100)
64 return (int(fRGB[0] * 255), int(fRGB[1] * 255), int(fRGB[2] * 255))
65
66
67 def color_hs_to_RGB(iH: float, iS: float):
68 """Convert an hsv color into its rgb representation."""
69 return color_hsv_to_RGB(iH, iS, 100)
70
71
72 def color_RGB_to_hs(iR: float, iG: float, iB: float):
73 """Convert an rgb color to its hs representation."""
74 return color_RGB_to_hsv(iR, iG, iB)[:2]
75
76
77 def color_RGB_to_hsv(iR: float, iG: float, iB: float):
78 """Convert an rgb color to its hsv representation.
79
80 Hue is scaled 0-360
81 Sat is scaled 0-100
82 Val is scaled 0-100
83 """
84 fHSV = colorsys.rgb_to_hsv(iR / 255.0, iG / 255.0, iB / 255.0)
85 return round(fHSV[0] * 360, 3), round(fHSV[1] * 100, 3), round(fHSV[2] * 100, 3)
86
87
88 def color_rgb_to_hex(r: int, g: int, b: int) -> str:
89 """Return a RGB color from a hex color string."""
90 return "{0:02x}{1:02x}{2:02x}".format(round(r), round(g), round(b))
91
92
93 class BleBoxLightEntity(CommonEntity):
94 """Representation of BleBox lights."""
95
96 @property
97 def is_on(self) -> bool:
98 """Return if light is on."""
99 return self._feature.is_on
100
101 @property
102 def brightness(self):
103 """Return the brightness."""
104 return self._feature.brightness
105
106 @property
107 def color_temp(self):
108 """Return color temperature."""
109 return self._feature.color_temp
110
111 @property
112 def color_mode(self):
113 """Return the color mode. Set values to _attr_ibutes if needed."""
114 color_mode_tmp = COLOR_MODE_MAP.get(self._feature.color_mode, COLOR_MODE_ONOFF)
115 if color_mode_tmp == COLOR_MODE_COLOR_TEMP:
116 self._attr_min_mireds = 1
117 self._attr_max_mireds = 255
118
119 return color_mode_tmp
120
121 @property
122 def effect_list(self):
123 """Return the list of supported effects."""
124 return self._feature.effect_list
125
126 @property
127 def effect(self):
128 """Return the current effect."""
129 return self._feature.effect
130
131 @property
132 def rgb_color(self):
133 """Return value for rgb."""
134 if (rgb_hex := self._feature.rgb_hex) is None:
135 return None
136 return tuple(
137 self._feature.normalise_elements_of_rgb(
138 self._feature.rgb_hex_to_rgb_list(rgb_hex)[0:3]
139 )
140 )
141
142 @property
143 def rgbw_color(self):
144 """Return the hue and saturation."""
145 if (rgbw_hex := self._feature.rgbw_hex) is None:
146 return None
147 return tuple(self._feature.rgb_hex_to_rgb_list(rgbw_hex)[0:4])
148
149 @property
150 def rgbww_color(self):
151 """Return value for rgbww."""
152 if (rgbww_hex := self._feature.rgbww_hex) is None:
153 return None
154 return tuple(self._feature.rgb_hex_to_rgb_list(rgbww_hex))
155
156 async def async_turn_on(self, **kwargs):
157 """Turn the light on."""
158 rgbw = kwargs.get(ATTR_RGBW_COLOR)
159 brightness = kwargs.get(ATTR_BRIGHTNESS)
160 effect = kwargs.get(ATTR_EFFECT)
161 color_temp = kwargs.get(ATTR_COLOR_TEMP)
162 rgbww = kwargs.get(ATTR_RGBWW_COLOR)
163 feature = self._feature
164 value = feature.sensible_on_value
165 rgb = kwargs.get(ATTR_RGB_COLOR)
166
167 if rgbw is not None:
168 value = list(rgbw)
169 if color_temp is not None:
170 value = feature.return_color_temp_with_brightness(
171 int(color_temp), self.brightness
172 )
173
174 if rgbww is not None:
175 value = list(rgbww)
176
177 if rgb is not None:
178 if self.color_mode == COLOR_MODE_RGB and brightness is None:
179 brightness = self.brightness
180 value = list(rgb)
181
182 if brightness is not None:
183 if self.color_mode == ATTR_COLOR_TEMP:
184 value = feature.return_color_temp_with_brightness(
185 self.color_temp, brightness
186 )
187 else:
188 value = feature.apply_brightness(value, brightness)
189
190 if effect is not None:
191 effect_value = self.effect_list.index(effect)
192 await self._feature.async_api_command("effect", effect_value)
193 else:
194 await self._feature.async_on(value)
195
196 async def async_turn_off(self, **kwargs):
197 """Turn the light off."""
198 await self._feature.async_off()
199
200
201 class TestDimmer(DefaultBoxTest):
202 """Tests for BleBox dimmerBox."""
203
204 DEVCLASS = "lights"
205 ENTITY_CLASS = BleBoxLightEntity
206
207 DEV_INFO_PATH = "api/dimmer/state"
208 DEVICE_INFO = json.loads(
209 """
210 {
211 "device": {
212 "deviceName": "My dimmer",
213 "type": "dimmerBox",
214 "fv": "0.247",
215 "hv": "0.2",
216 "id": "1afe34e750b8",
217 "apiLevel": "20170829"
218 },
219 "network": {
220 "ip": "192.168.1.239",
221 "ssid": "myWiFiNetwork",
222 "station_status": 5,
223 "apSSID": "dimmerBox-ap",
224 "apPasswd": ""
225 },
226 "dimmer": {
227 "loadType": 7,
228 "currentBrightness": 65,
229 "desiredBrightness": 65,
230 "temperature": 39,
231 "overloaded": false,
232 "overheated": false
233 }
234 }
235 """
236 )
237
238 def patch_version(apiLevel):
239 """Generate a patch for a JSON state fixture."""
240 return f"""
241 {{ "device": {{ "apiLevel": {apiLevel} }} }}
242 """
243
244 DEVICE_INFO_FUTURE = jmerge(DEVICE_INFO, patch_version(future_date()))
245 DEVICE_INFO_LATEST = jmerge(
246 DEVICE_INFO, patch_version(get_latest_api_level("dimmerBox"))
247 )
248 DEVICE_INFO_UNSUPPORTED = jmerge(
249 DEVICE_INFO, patch_version(20140828)
250 ) # fake version, default_api_level implemented
251
252 DEVICE_INFO_UNSPECIFIED_API = json.loads(
253 """
254 {
255 "device": {
256 "deviceName": "My dimmer",
257 "type": "dimmerBox",
258 "fv": "0.247",
259 "hv": "0.2",
260 "id": "1afe34e750b8"
261 },
262 "network": {
263 "ip": "192.168.1.239",
264 "ssid": "myWiFiNetwork",
265 "station_status": 5,
266 "apSSID": "dimmerBox-ap",
267 "apPasswd": ""
268 },
269 "dimmer": {
270 "loadType": 7,
271 "currentBrightness": 65,
272 "desiredBrightness": 65,
273 "temperature": 39,
274 "overloaded": false,
275 "overheated": false
276 }
277 }
278 """
279 )
280
281 STATE_DEFAULT = json.loads(
282 """
283 {
284 "dimmer": {
285 "loadType": 7,
286 "currentBrightness": 11,
287 "desiredBrightness": 53,
288 "temperature": 29,
289 "overloaded": false,
290 "overheated": false
291 }
292 }
293 """
294 )
295
296 def patch_state(current, desired):
297 """Generate a patch for a JSON state fixture."""
298 return f"""
299 {{
300 "dimmer": {{
301 "currentBrightness": {current},
302 "desiredBrightness": {desired}
303 }}
304 }}
305 """
306
307 STATE_OFF = jmerge(STATE_DEFAULT, patch_state(0, 0))
308 STATE_ON_DEFAULT = jmerge(STATE_DEFAULT, patch_state(238, 255))
309 STATE_ON_BRIGHT = jmerge(STATE_DEFAULT, patch_state(201, 202))
310
311 async def test_init(self, aioclient_mock):
312 """Test cover default state."""
313 await self.allow_get_info(aioclient_mock)
314 entity = (await self.async_entities(aioclient_mock))[0]
315
316 assert entity.name == "My dimmer (dimmerBox#brightness)"
317 assert entity.unique_id == "BleBox-dimmerBox-1afe34e750b8-brightness"
318
319 # assert entity.supported_features & SUPPORT_BRIGHTNESS
320 assert entity.brightness is None
321
322 assert entity.is_on is None
323
324 async def test_device_info(self, aioclient_mock):
325 await self.allow_get_info(aioclient_mock, self.DEVICE_INFO)
326 entity = (await self.async_entities(aioclient_mock))[0]
327 assert entity.device_info["name"] == "My dimmer"
328 assert entity.device_info["mac"] == "1afe34e750b8"
329 assert entity.device_info["manufacturer"] == "BleBox"
330 assert entity.device_info["model"] == "dimmerBox"
331 assert entity.device_info["sw_version"] == "0.247"
332
333 async def test_update(self, aioclient_mock):
334 """Test light updating."""
335
336 entity = await self.updated(aioclient_mock, self.STATE_DEFAULT)
337
338 assert entity.brightness == 53
339 assert entity.is_on is True
340
341 async def allow_set_brightness(self, code, aioclient_mock, value, response):
342 """Set up mock for HTTP POST simulating brightness change."""
343 await self.allow_post(
344 code,
345 aioclient_mock,
346 "/api/dimmer/set",
347 '{"dimmer":{"desiredBrightness": ' + str(value) + "}}",
348 response,
349 )
350
351 async def test_on(self, aioclient_mock):
352 """Test light on."""
353 entity = await self.updated(aioclient_mock, self.STATE_OFF)
354 assert entity.is_on is False
355
356 async def turn_on():
357 await entity.async_turn_on()
358
359 await self.allow_set_brightness(
360 turn_on, aioclient_mock, 255, self.STATE_ON_DEFAULT
361 )
362
363 assert entity.is_on is True
364 # TODO: is max brightness a good default?
365 assert entity.brightness == 255
366
367 async def test_on_with_brightness(self, aioclient_mock):
368 """Test light on with a brightness value."""
369 entity = await self.updated(aioclient_mock, self.STATE_OFF)
370 assert entity.is_on is False
371
372 async def turn_on():
373 await entity.async_turn_on(brightness=202)
374
375 await self.allow_set_brightness(
376 turn_on, aioclient_mock, 202, self.STATE_ON_BRIGHT
377 )
378
379 assert entity.is_on is True
380 assert entity.brightness == 202 # as if desired brightness not reached yet
381
382 async def test_off(self, aioclient_mock):
383 """Test light off."""
384 entity = await self.updated(aioclient_mock, self.STATE_ON_DEFAULT)
385 assert entity.is_on is True
386
387 async def turn_off():
388 await entity.async_turn_off()
389
390 await self.allow_set_brightness(turn_off, aioclient_mock, 0, self.STATE_OFF)
391
392 assert entity.is_on is False
393 assert entity.brightness == 0
394
395 async def test_unspecified_version(self, aioclient_mock):
396 """
397 As default_api_level implemented for this class of devices, test method shall be omitted.
398 """
399 pass
400
401
402 class TestWLightBoxS(DefaultBoxTest):
403 """Tests for BleBox wLightBoxS."""
404
405 DEVCLASS = "lights"
406 ENTITY_CLASS = BleBoxLightEntity
407
408 DEVICE_EXTENDED_INFO_PATH = "/api/rgbw/extended/state"
409 DEV_INFO_PATH = "api/rgbw/state"
410 DEVICE_INFO = json.loads(
411 """
412 {
413 "device": {
414 "deviceName": "My wLightBoxS",
415 "type": "wLightBox",
416 "product":"wLightBoxS",
417 "fv": "0.924",
418 "hv": "0.1",
419 "universe": 0,
420 "id": "1afe34e750b8",
421 "apiLevel": 20200229,
422 "ip": "192.168.9.13",
423 "availableFv": null
424 }
425 }
426 """
427 )
428
429 DEVICE_INFO2 = {
430 "device": {
431 "deviceName": "My wLightBoxS",
432 "type": "wLightBox",
433 "product": "wLightBoxS",
434 "hv": "s_0.1",
435 "fv": "0.1022",
436 "universe": 0,
437 "apiLevel": "20200229",
438 "id": "ce50e32d2707",
439 "ip": "192.168.1.25",
440 "availableFv": None,
441 }
442 }
443
444 DEVICE_EXTENDED_INFO = {
445 "rgbw": {
446 "desiredColor": "f5",
447 "currentColor": "f5", # 245 in decimal
448 "lastOnColor": "f5",
449 "durationsMs": {"colorFade": 1000, "effectFade": 1000, "effectStep": 1000},
450 "effectID": 0,
451 "colorMode": 3,
452 "favColors": {"0": "ff", "1": "00", "2": "c0", "3": "40", "4": "00"},
453 "effectsNames": {"0": "NONE", "1": "FADE", "2": "Stroboskop", "3": "BELL"},
454 }
455 }
456
457 def patch_version(apiLevel):
458 """Generate a patch for a JSON state fixture."""
459 return f"""
460 {{ "device": {{ "apiLevel": {apiLevel} }} }}
461 """
462
463 DEVICE_INFO_FUTURE = jmerge(DEVICE_INFO, patch_version(future_date()))
464 DEVICE_INFO_LATEST = jmerge(
465 DEVICE_INFO, patch_version(get_latest_api_level("wLightBoxS"))
466 )
467 DEVICE_INFO_UNSUPPORTED = jmerge(DEVICE_INFO, patch_version(20140717))
468
469 DEVICE_INFO_UNSPECIFIED_API = json.loads(
470 """
471 {
472 "device": {
473 "deviceName": "My wLightBoxS",
474 "type": "wLightBoxS",
475 "fv": "0.247",
476 "hv": "0.2",
477 "id": "1afe34e750b8"
478 },
479 "network": {
480 "ip": "192.168.1.239",
481 "ssid": "myWiFiNetwork",
482 "station_status": 5,
483 "apSSID": "wLightBoxS-ap",
484 "apPasswd": ""
485 },
486 "rgbw": {
487 "desiredColor": "e3",
488 "currentColor": "df",
489 "fadeSpeed": 255,
490 "effectID": 0
491 }
492 }
493 """
494 )
495
496 STATE_ON = json.loads(
497 """
498 {
499 "rgbw": {
500 "desiredColor": "ab",
501 "currentColor": "cd",
502 "fadeSpeed": 255,
503 "effectID": 0,
504 "colorMode": 3
505 }
506 }
507 """
508 )
509
510 STATE_FULL_ON = json.loads(
511 """
512 {
513 "rgbw": {
514 "desiredColor": "ff",
515 "currentColor": "ce",
516 "fadeSpeed": 255,
517 "effectID": 0
518 }
519 }
520 """
521 )
522
523 STATE_OFF = json.loads(
524 """
525 {
526 "rgbw": {
527 "desiredColor": "00",
528 "currentColor": "00",
529 "fadeSpeed": 255,
530 "effectID": 0
531 }
532 }
533 """
534 )
535
536 STATE_DEFAULT = STATE_ON
537
538 async def test_init(self, aioclient_mock):
539 """Test cover default state."""
540 await self.allow_get_info(aioclient_mock)
541 entity = (await self.async_entities(aioclient_mock))[0]
542
543 assert entity.name == "My wLightBoxS (wLightBoxS#brightness_mono1)"
544 assert entity.unique_id == "BleBox-wLightBoxS-1afe34e750b8-brightness_mono1"
545 assert entity.brightness == 0xF5
546 assert entity.is_on
547 assert entity._feature.index == 0
548
549 async def test_device_info(self, aioclient_mock):
550 await self.allow_get_info(aioclient_mock, self.DEVICE_INFO)
551 entity = (await self.async_entities(aioclient_mock))[0]
552 assert entity.device_info["name"] == "My wLightBoxS"
553 assert entity.device_info["mac"] == "1afe34e750b8"
554 assert entity.device_info["manufacturer"] == "BleBox"
555 assert entity.device_info["model"] == "wLightBoxS"
556 assert entity.device_info["sw_version"] == "0.924"
557
558 async def test_device_info2(self, aioclient_mock):
559 await self.allow_get_info(aioclient_mock, self.DEVICE_INFO2)
560 entity = (await self.async_entities(aioclient_mock))[0]
561 assert entity.device_info["name"] == "My wLightBoxS"
562 assert entity.device_info["mac"] == "ce50e32d2707"
563 assert entity.device_info["manufacturer"] == "BleBox"
564 assert entity.device_info["model"] == "wLightBoxS"
565 assert entity.device_info["sw_version"] == "0.1022"
566
567 async def test_update(self, aioclient_mock):
568 """Test light updating."""
569
570 entity = await self.updated(aioclient_mock, self.STATE_DEFAULT)
571 assert entity.brightness == 0xAB
572 assert entity.is_on is True
573
574 async def allow_set_brightness(self, code, aioclient_mock, value, response):
575 """Set up mock for HTTP POST simulating color change."""
576 raw = "{:02x}".format(value)
577 await self.allow_post(
578 code,
579 aioclient_mock,
580 "/api/rgbw/set",
581 json.dumps(
582 {"rgbw": {"desiredColor": raw + "------"}}
583 ), # simulating mask for color mod 3
584 response,
585 )
586
587 async def test_on(self, aioclient_mock):
588 """Test light on."""
589 entity = await self.updated(aioclient_mock, self.STATE_OFF)
590 assert entity.is_on is False
591
592 async def turn_on():
593 await entity.async_turn_on()
594
595 await self.allow_set_brightness(
596 turn_on, aioclient_mock, 0xFF, self.STATE_FULL_ON
597 )
598
599 assert entity.is_on is True
600 assert entity.brightness == 0xFF
601
602 async def test_on_with_bad_value_type(self, aioclient_mock):
603 """Test light on with off value."""
604 entity = await self.updated(aioclient_mock, self.STATE_OFF)
605 assert entity.is_on is False
606
607 with pytest.raises(
608 ValueError,
609 match=r"adjust_brightness called with bad parameter \(00 is <class 'str'> instead of int\)",
610 ):
611 await entity.async_turn_on(brightness="00")
612
613 async def test_on_with_bad_value_exceeding_max(self, aioclient_mock):
614 """Test light on with off value."""
615 entity = await self.updated(aioclient_mock, self.STATE_OFF)
616 assert entity.is_on is False
617
618 with pytest.raises(
619 ValueError,
620 match=r"adjust_brightness called with bad parameter \(1234 is greater than 255\)",
621 ):
622 await entity.async_turn_on(brightness=1234)
623
624 async def test_off(self, aioclient_mock):
625 """Test light off."""
626 entity = await self.updated(aioclient_mock, self.STATE_ON)
627 assert entity.is_on is True
628
629 async def turn_off():
630 await entity.async_turn_off()
631
632 await self.allow_set_brightness(turn_off, aioclient_mock, 0, self.STATE_OFF)
633
634 assert entity.is_on is False
635 assert entity.brightness == 0
636
637 async def test_unspecified_version(self, aioclient_mock):
638 """
639 As default_api_level implemented for this class of devices, test method shall be omitted.
640 """
641 pass
642
643
644 class TestWLightBox(DefaultBoxTest):
645 """Tests for BleBox wLightBox."""
646
647 DEVCLASS = "lights"
648 ENTITY_CLASS = BleBoxLightEntity
649
650 DEVICE_EXTENDED_INFO_PATH = "/api/rgbw/extended/state"
651 # TODO: rename everywhere (STATE_PATH)
652 DEV_INFO_PATH = "api/rgbw/state"
653
654 DEVICE_INFO = json.loads(
655 """
656 {
657 "device": {
658 "deviceName": "My light 1",
659 "type": "wLightBox",
660 "fv": "0.993",
661 "hv": "4.3",
662 "id": "1afe34e750b8",
663 "apiLevel": 20190808
664 }
665 }
666 """
667 )
668 DEVICE_EXTENDED_INFO = {
669 "rgbw": {
670 "colorMode": 4,
671 "effectID": 0,
672 "desiredColor": "fa00203A",
673 "currentColor": "ff00302F",
674 "lastOnColor": "f1e2d3e4",
675 "durationsMs": {"colorFade": 1000, "effectFade": 1500, "effectStep": 2000},
676 "favColors": {"0": "ff", "1": "00", "2": "c0", "3": "40", "4": "00"},
677 "effectsNames": {"0": "NONE", "1": "FADE", "2": "Stroboskop", "3": "BELL"},
678 }
679 }
680 DEVICE_EXTENDED_INFO_COLORMODE_1 = {
681 "rgbw": {
682 "colorMode": 1,
683 "effectID": 0,
684 "desiredColor": "fa",
685 "currentColor": "ff",
686 "lastOnColor": "ff",
687 "durationsMs": {"colorFade": 1000, "effectFade": 1500, "effectStep": 2000},
688 "favColors": {"0": "ff", "1": "00", "2": "c0", "3": "40", "4": "00"},
689 "effectsNames": {"0": "NONE", "1": "FADE", "2": "Stroboskop", "3": "BELL"},
690 }
691 }
692 DEVICE_EXTENDED_INFO_COLORMODE_2 = {
693 "rgbw": {
694 "colorMode": 2,
695 "effectID": 0,
696 "desiredColor": "fa00203A",
697 "currentColor": "ff00302F",
698 "lastOnColor": "f1e2d3e4",
699 "durationsMs": {"colorFade": 1000, "effectFade": 1500, "effectStep": 2000},
700 "favColors": {"0": "ff", "1": "00", "2": "c0", "3": "40", "4": "00"},
701 "effectsNames": {"0": "NONE", "1": "FADE", "2": "Stroboskop", "3": "BELL"},
702 }
703 }
704 DEVICE_EXTENDED_INFO_COLORMODE_3 = {
705 "rgbw": {
706 "colorMode": 3,
707 "effectID": 0,
708 "desiredColor": "fa00203A",
709 "currentColor": "ff00302F",
710 "lastOnColor": "f1e2d3e4",
711 "durationsMs": {"colorFade": 1000, "effectFade": 1500, "effectStep": 2000},
712 "favColors": {"0": "ff", "1": "00", "2": "c0", "3": "40", "4": "00"},
713 "effectsNames": {"0": "NONE", "1": "FADE", "2": "Stroboskop", "3": "BELL"},
714 }
715 }
716 DEVICE_EXTENDED_INFO_COLORMODE_4 = {
717 "rgbw": {
718 "colorMode": 4,
719 "effectID": 0,
720 "desiredColor": "fa00203A",
721 "currentColor": "ff00302F",
722 "lastOnColor": "f1e2d3e4",
723 "durationsMs": {"colorFade": 1000, "effectFade": 1500, "effectStep": 2000},
724 "favColors": {"0": "ff", "1": "00", "2": "c0", "3": "40", "4": "00"},
725 "effectsNames": {"0": "NONE", "1": "FADE", "2": "Stroboskop", "3": "BELL"},
726 }
727 }
728 DEVICE_EXTENDED_INFO_COLORMODE_5 = {
729 "rgbw": {
730 "colorMode": 5,
731 "effectID": 0,
732 "desiredColor": "fa00203A",
733 "currentColor": "ff00302F",
734 "lastOnColor": "f1e2d3e4",
735 "durationsMs": {"colorFade": 1000, "effectFade": 1500, "effectStep": 2000},
736 "favColors": {"0": "ff", "1": "00", "2": "c0", "3": "40", "4": "00"},
737 "effectsNames": {"0": "NONE", "1": "FADE", "2": "Stroboskop", "3": "BELL"},
738 }
739 }
740
741 DEVICE_EXTENDED_INFO_COLORMODE_6 = {
742 "rgbw": {
743 "colorMode": 6,
744 "effectID": 0,
745 "desiredColor": "fa00203A",
746 "currentColor": "ff00302F",
747 "lastOnColor": "f1e2d3e4",
748 "durationsMs": {"colorFade": 1000, "effectFade": 1500, "effectStep": 2000},
749 "favColors": {"0": "ff", "1": "00", "2": "c0", "3": "40", "4": "00"},
750 "effectsNames": {"0": "NONE", "1": "FADE", "2": "Stroboskop", "3": "BELL"},
751 }
752 }
753 DEVICE_EXTENDED_INFO_COLORMODE_7 = {
754 "rgbw": {
755 "colorMode": 7,
756 "effectID": 0,
757 "desiredColor": "fcfffcff00",
758 "currentColor": "fcfffcff00",
759 "lastOnColor": "fcfffcff00",
760 "durationsMs": {"colorFade": 1000, "effectFade": 1000, "effectStep": 1000},
761 "favColors": {"0": "ff", "1": "00", "2": "c0", "3": "40", "4": "00"},
762 "effectsNames": {"0": "NONE", "1": "FADE", "2": "Stroboskop", "3": "BELL"},
763 }
764 }
765
766 def patch_version(apiLevel):
767 """Generate a patch for a JSON state fixture."""
768 return f"""
769 {{ "device": {{ "apiLevel": {apiLevel} }} }}
770 """
771
772 DEVICE_INFO_FUTURE = jmerge(DEVICE_INFO, patch_version(future_date()))
773 DEVICE_INFO_LATEST = jmerge(
774 DEVICE_INFO, patch_version(get_latest_api_level("wLightBox"))
775 )
776 DEVICE_INFO_UNSUPPORTED = jmerge(DEVICE_INFO, patch_version(20140717))
777
778 DEVICE_INFO_UNSPECIFIED_API = json.loads(
779 """
780 {
781 "device": {
782 "deviceName": "My light 1",
783 "type": "wLightBox",
784 "fv": "0.247",
785 "hv": "0.2",
786 "id": "1afe34e750b8"
787 },
788 "network": {
789 "ip": "192.168.1.237",
790 "ssid": "myWiFiNetwork",
791 "station_status": 5,
792 "apSSID": "wLightBox-ap",
793 "apPasswd": ""
794 },
795 "rgbw": {
796 "desiredColor": "abcdefd9",
797 "currentColor": "abcdefd9",
798 "fadeSpeed": 248,
799 "effectSpeed": 2,
800 "effectID": 3,
801 "colorMode": 3
802 }
803 }
804 """
805 )
806
807 STATE_DEFAULT = json.loads(
808 """
809 {
810 "rgbw": {
811 "colorMode": 4,
812 "effectID": 0,
813 "desiredColor": "fa00203A",
814 "currentColor": "ff00302F",
815 "lastOnColor": "f1e2d3e4",
816 "durationsMs": {
817 "colorFade": 1000,
818 "effectFade": 1500,
819 "effectStep": 2000
820 }
821 }
822 }
823 """
824 )
825
826 def patch_state(current, desired=None, last=None):
827 """Generate a patch for a JSON state fixture."""
828
829 if desired is None:
830 desired = current
831
832 return f"""
833 {{
834 "rgbw": {{
835 "currentColor": "{current}",
836 "desiredColor": "{desired}"
837 { "" if last is None else f',"lastOnColor": "{last}"' }
838 }}
839 }}
840 """
841
842 STATE_OFF = jmerge(STATE_DEFAULT, patch_state("00000000"))
843 STATE_OFF_NOLAST_WHITE = jmerge(
844 STATE_DEFAULT, patch_state("0a0b0c0d", "00000000", "dacefb00")
845 )
846 STATE_ON = STATE_DEFAULT
847 STATE_ON_AFTER_WHITE = jmerge(
848 STATE_DEFAULT, patch_state("01020304", "f1e2d3c7", "f1e2d3c7")
849 )
850 STATE_ON_AFTER_RESET_WHITE = jmerge(
851 STATE_DEFAULT, patch_state("01020304", "f1e2d300", "f1e2d300")
852 )
853
854 STATE_ON_ONLY_SOME_COLOR = jmerge(STATE_DEFAULT, patch_state("ffa1b200"))
855 STATE_ON_LAST = jmerge(STATE_DEFAULT, patch_state("01020304", "f1e2d3e4"))
856 STATE_AFTER_SOME_COLOR_SET = jmerge(STATE_DEFAULT, patch_state("ffa1b2e4"))
857
858 async def test_init(self, aioclient_mock):
859 """Test cover default state."""
860 await self.allow_get_info(aioclient_mock)
861 entity = (await self.async_entities(aioclient_mock))[0]
862
863 assert entity.name == "My light 1 (wLightBox#color_RGBorW)"
864 assert entity.unique_id == "BleBox-wLightBox-1afe34e750b8-color_RGBorW"
865 # In current state of master branch white_value is not property of BleBoxLightEntity, fake test... dissapointing
866 assert entity._feature.supports_white
867 assert entity._feature.white_value == 0x3A
868
869 assert entity._feature.supports_color
870 assert entity.brightness == 0xFA
871 assert entity.is_on is True
872
873 async def test_device_info(self, aioclient_mock):
874 await self.allow_get_info(aioclient_mock, self.DEVICE_INFO)
875 entity = (await self.async_entities(aioclient_mock))[0]
876 assert entity.device_info["name"] == "My light 1"
877 assert entity.device_info["mac"] == "1afe34e750b8"
878 assert entity.device_info["manufacturer"] == "BleBox"
879 assert entity.device_info["model"] == "wLightBox"
880 assert entity.device_info["sw_version"] == "0.993"
881
882 async def test_update(self, aioclient_mock):
883 """Test light updating."""
884
885 entity = await self.updated(aioclient_mock, self.STATE_DEFAULT)
886 assert entity.brightness == 250
887 # assert entity.hs_color == (352.32, 100.0)
888 # assert entity.white_value == 0x3A
889 assert entity.is_on is True # state already available
890
891 async def allow_set_color(self, code, aioclient_mock, value, response):
892 """Set up mock for HTTP POST simulating color change."""
893 await self.allow_post(
894 code,
895 aioclient_mock,
896 "/api/rgbw/set",
897 '{"rgbw":{"desiredColor": "' + str(value) + '"}}',
898 response,
899 )
900
901 async def test_on_to_last_color(self, aioclient_mock):
902 """Test light on."""
903 entity = await self.updated(aioclient_mock, self.STATE_OFF)
904 assert entity.is_on is False
905
906 async def action():
907 await entity.async_turn_on()
908
909 await self.allow_set_color(
910 action, aioclient_mock, "f1e2d3e4", self.STATE_ON_LAST
911 )
912
913 assert entity.is_on is True
914 # assert entity.white_value == 0xE4
915 assert entity.rgbw_color == tuple(
916 [int(i, 16) for i in ["f1", "e2", "d3", "e4"]]
917 )
918
919 async def test_off(self, aioclient_mock):
920 """Test light off."""
921 entity = await self.updated(aioclient_mock, self.STATE_ON)
922 assert entity.is_on is True
923
924 async def action():
925 await entity.async_turn_off()
926
927 await self.allow_set_color(action, aioclient_mock, "00000000", self.STATE_OFF)
928
929 assert entity.is_on is False
930 # assert entity.white_value == 0x00
931
932 async def test_colormode_5_brightness(self, aioclient_mock):
933 self.DEVICE_EXTENDED_INFO = self.DEVICE_EXTENDED_INFO_COLORMODE_5
934 await self.allow_get_info(aioclient_mock)
935 self.STATE_DEFAULT["colorMode"] = 5
936 entity = await self.updated(aioclient_mock, self.STATE_DEFAULT)
937
938 assert "_cct" in entity.name
939 assert entity.brightness == 250
940
941 async def test_colormode_6_brightness(self, aioclient_mock):
942 self.DEVICE_EXTENDED_INFO = self.DEVICE_EXTENDED_INFO_COLORMODE_6
943 await self.allow_get_info(aioclient_mock)
944 self.STATE_DEFAULT["colorMode"] = 6
945 entity = await self.updated(aioclient_mock, self.STATE_DEFAULT, 1)
946
947 assert "_cct2" in entity.name
948 assert entity.brightness
949 assert entity._feature.index == 1
950
951 async def test_effect_list_return_list(self, aioclient_mock):
952 self.DEVICE_EXTENDED_INFO = self.DEVICE_EXTENDED_INFO_COLORMODE_5
953 await self.allow_get_info(aioclient_mock)
954 self.STATE_DEFAULT["colorMode"] = 5
955 entity = await self.updated(aioclient_mock, self.STATE_DEFAULT)
956
957 assert entity.effect_list
958
959 async def test_color_temp_for_colomode_6(self, aioclient_mock):
960 self.DEVICE_EXTENDED_INFO = self.DEVICE_EXTENDED_INFO_COLORMODE_6
961 await self.allow_get_info(aioclient_mock)
962 self.STATE_DEFAULT["colorMode"] = 6
963 entity = await self.updated(aioclient_mock, self.STATE_DEFAULT)
964
965 assert "_cct1" in entity.name
966 assert entity.color_temp
967 assert entity._feature.index == 0
968
969 async def test_mono_4ch_indices(self, aioclient_mock):
970 """Test that 4-channel MONO mode produces entities with correct indices."""
971 self.DEVICE_EXTENDED_INFO = self.DEVICE_EXTENDED_INFO_COLORMODE_3
972 await self.allow_get_info(aioclient_mock)
973 self.STATE_DEFAULT["rgbw"]["colorMode"] = 3
974 entities = await self.async_entities(aioclient_mock)
975
976 assert len(entities) == 4
977 for i, entity in enumerate(entities):
978 assert entity._feature.index == i
979
980 async def test_color_temp_for_colomode_rgbww(self, aioclient_mock):
981 self.DEVICE_EXTENDED_INFO = self.DEVICE_EXTENDED_INFO_COLORMODE_7
982 await self.allow_get_info(aioclient_mock)
983 self.STATE_DEFAULT["colorMode"] = 7
984 entity = await self.updated(aioclient_mock, self.STATE_DEFAULT)
985
986 assert "_RGBCCT" in entity.name
987 assert entity.color_temp
988 assert entity.brightness
989
990 async def test_normalise_element_colormode_rgb(self, aioclient_mock):
991 # testing sensible on value which is used only while async_turn_on executed
992
993 self.DEVICE_EXTENDED_INFO = self.DEVICE_EXTENDED_INFO_COLORMODE_2
994 self.DEVICE_EXTENDED_INFO = jmerge(
995 self.DEVICE_EXTENDED_INFO, self.patch_state("fafafa", "fafafa")
996 )
997 self.STATE_DEFAULT["rgbw"]["colorMode"] = 2
998
999 self.STATE_DEFAULT = jmerge(
1000 self.STATE_DEFAULT, self.patch_state("fafafa", "fafafa")
1001 )
1002
1003 await self.allow_get_info(aioclient_mock)
1004 entity = await self.updated(aioclient_mock, self.STATE_DEFAULT)
1005
1006 async def turn_on():
1007 await entity.async_turn_on(rgb_color=(255, 0, 140))
1008
1009 self.STATE_ON = jmerge(self.STATE_ON, self.patch_state("fafafa", "fafafa"))
1010 await self.allow_set_color(turn_on, aioclient_mock, "fa0089", self.STATE_ON)
1011
1012 assert max(entity.rgbw_color) == 250
1013
1014 async def test_normalise_when_max_is_zero_rgb(self, aioclient_mock):
1015 # testing sensible on value which is used only while async_turn_on executed
1016
1017 self.DEVICE_EXTENDED_INFO = self.DEVICE_EXTENDED_INFO_COLORMODE_2
1018 self.DEVICE_EXTENDED_INFO = jmerge(
1019 self.DEVICE_EXTENDED_INFO, self.patch_state("030303", "030303")
1020 )
1021 self.STATE_DEFAULT["rgbw"]["colorMode"] = 2
1022
1023 self.STATE_DEFAULT = jmerge(
1024 self.STATE_DEFAULT, self.patch_state("030303", "030303")
1025 )
1026
1027 await self.allow_get_info(aioclient_mock)
1028 entity = await self.updated(aioclient_mock, self.STATE_DEFAULT)
1029
1030 async def turn_on():
1031 await entity.async_turn_on(rgb_color=(255, 10, 255))
1032
1033 self.STATE_ON = jmerge(self.STATE_ON, self.patch_state("000000", "000000"))
1034 await self.allow_set_color(turn_on, aioclient_mock, "030003", self.STATE_ON)
1035
1036 assert max(entity.rgb_color) == 255
1037
1038 async def test_sensible_on_value_for_color_mode_1(self, aioclient_mock):
1039 self.DEVICE_EXTENDED_INFO = self.DEVICE_EXTENDED_INFO_COLORMODE_1
1040 self.DEVICE_EXTENDED_INFO = jmerge(
1041 self.DEVICE_EXTENDED_INFO, self.patch_state("00000000", "00000000")
1042 )
1043 self.STATE_DEFAULT["rgbw"]["colorMode"] = 1
1044
1045 self.STATE_DEFAULT = jmerge(
1046 self.STATE_DEFAULT, self.patch_state("00000000", "00000000")
1047 )
1048
1049 await self.allow_get_info(aioclient_mock)
1050
1051 entity = await self.updated(aioclient_mock, self.STATE_DEFAULT)
1052
1053 async def turn_on():
1054 await entity.async_turn_on()
1055
1056 self.STATE_ON = jmerge(self.STATE_ON, self.patch_state("ffffffff", "ffffffff"))
1057 await self.allow_set_color(turn_on, aioclient_mock, "ffffffff", self.STATE_ON)
1058
1059 assert entity.rgbw_color == (255, 255, 255, 255)
1060
1061 async def test_sensible_on_value_for_color_mode_5(self, aioclient_mock):
1062 self.DEVICE_EXTENDED_INFO = self.DEVICE_EXTENDED_INFO_COLORMODE_5
1063 self.DEVICE_EXTENDED_INFO = jmerge(
1064 self.DEVICE_EXTENDED_INFO, self.patch_state("00000000", "00000000")
1065 )
1066 self.STATE_DEFAULT["rgbw"]["colorMode"] = 5
1067
1068 self.STATE_DEFAULT = jmerge(
1069 self.STATE_DEFAULT, self.patch_state("00000000", "00000000")
1070 )
1071
1072 await self.allow_get_info(aioclient_mock)
1073 self.STATE_DEFAULT["colorMode"] = 5
1074 entity = await self.updated(aioclient_mock, self.STATE_DEFAULT)
1075
1076 async def turn_on():
1077 await entity.async_turn_on()
1078
1079 self.STATE_ON = jmerge(self.STATE_ON, self.patch_state("ffffffff", "ffffffff"))
1080 await self.allow_set_color(turn_on, aioclient_mock, "ffff----", self.STATE_ON)
1081
1082 assert entity.color_temp == 128
1083
1084 async def test_turn_on_color_temp_full_warm_for_color_mode_5(self, aioclient_mock):
1085 self.DEVICE_EXTENDED_INFO = self.DEVICE_EXTENDED_INFO_COLORMODE_5
1086 self.STATE_DEFAULT["rgbw"]["colorMode"] = 5
1087
1088 await self.allow_get_info(aioclient_mock)
1089 self.STATE_DEFAULT["colorMode"] = 5
1090 entity = await self.updated(aioclient_mock, self.STATE_DEFAULT)
1091
1092 async def turn_on():
1093 await entity.async_turn_on(color_temp=1)
1094
1095 self.STATE_ON = jmerge(self.STATE_ON, self.patch_state("02ffffff", "02ffffff"))
1096
1097 await self.allow_set_color(turn_on, aioclient_mock, "02fa----", self.STATE_ON)
1098
1099 assert entity.color_temp == 1
1100
1101 async def test_turn_on_color_temp_full_cold_for_color_mode_5(self, aioclient_mock):
1102 self.DEVICE_EXTENDED_INFO = self.DEVICE_EXTENDED_INFO_COLORMODE_5
1103 self.STATE_DEFAULT["rgbw"]["colorMode"] = 5
1104
1105 await self.allow_get_info(aioclient_mock)
1106 self.STATE_DEFAULT["colorMode"] = 5
1107 entity = await self.updated(aioclient_mock, self.STATE_DEFAULT)
1108
1109 async def turn_on():
1110 await entity.async_turn_on(color_temp=255)
1111
1112 self.STATE_ON = jmerge(self.STATE_ON, self.patch_state("fa00ffff", "fa02ffff"))
1113
1114 await self.allow_set_color(turn_on, aioclient_mock, "fa00----", self.STATE_ON)
1115
1116 assert entity.color_temp == 255
1117
1118 async def test_sensible_on_value_for_color_mode_6(self, aioclient_mock):
1119 self.DEVICE_EXTENDED_INFO = self.DEVICE_EXTENDED_INFO_COLORMODE_6
1120 self.STATE_DEFAULT["rgbw"]["colorMode"] = 6
1121
1122 await self.allow_get_info(aioclient_mock)
1123 self.STATE_DEFAULT["colorMode"] = 6
1124 entity = await self.updated(aioclient_mock, self.STATE_DEFAULT)
1125
1126 async def turn_on():
1127 await entity.async_turn_on(color_temp=255)
1128
1129 self.STATE_ON = jmerge(self.STATE_ON, self.patch_state("fa00ffff", "fa02ffff"))
1130
1131 await self.allow_set_color(turn_on, aioclient_mock, "fa00----", self.STATE_ON)
1132
1133 assert entity.color_temp == 255
1134
1135 async def test_sensible_on_value_for_color_mode_7(self, aioclient_mock):
1136 self.DEVICE_EXTENDED_INFO = self.DEVICE_EXTENDED_INFO_COLORMODE_7
1137 self.STATE_DEFAULT["rgbw"]["colorMode"] = 7
1138 self.STATE_DEFAULT = jmerge(
1139 self.STATE_DEFAULT, self.patch_state("fcfffcff00", "fcfffcff00")
1140 )
1141 await self.allow_get_info(aioclient_mock)
1142 self.STATE_DEFAULT["colorMode"] = 7
1143 entity = await self.updated(aioclient_mock, self.STATE_DEFAULT)
1144
1145 async def turn_on():
1146 await entity.async_turn_on(rgbww_color=(0, 0, 0, 120, 214))
1147
1148 self.STATE_ON = jmerge(
1149 self.STATE_ON, self.patch_state("000000d678", "000000d678")
1150 )
1151
1152 await self.allow_set_color(turn_on, aioclient_mock, "000000d678", self.STATE_ON)
1153
1154 assert entity.rgbww_color == (0, 0, 0, 120, 214)
1155
1156 async def test_unspecified_version(self, aioclient_mock):
1157 """
1158 As default_api_level implemented for this class of devices, test method shall be omitted.
1159 """
1160 pass
1161
1162
1163 def test_unit_light_evaluate_brightness_from_rgb():
1164 tested_ob = Light.evaluate_brightness_from_rgb(iterable=(140, 230))
1165 assert tested_ob == 230
1166
1167
1168 def test_unit_light_evaluate_off_value_mono_2ch():
1169 """Test evaluate_off_value for MONO mode with 2 channels (e.g., desired_color="0000")."""
1170 from blebox_uniapi.box import Box
1171 from unittest.mock import MagicMock
1172
1173 box = MagicMock(spec=Box)
1174 box.type = "wLightBox"
1175
1176 def mask(x):
1177 return f"{x}------"
1178
1179 light = Light(
1180 product=box,
1181 alias="test_mono1",
1182 methods={},
1183 extended_state={"rgbw": {"colorMode": 3}},
1184 mask=mask,
1185 desired_color="0000",
1186 color_mode=3,
1187 effect_list=None,
1188 current_effect=None,
1189 )
1190
1191 assert light._off_value == "00"
1192
1193
1194 def test_unit_light_evaluate_off_value_mono_4ch():
1195 """Test evaluate_off_value for MONO mode with 4 channels (e.g., desired_color="00000000")."""
1196 from blebox_uniapi.box import Box
1197 from unittest.mock import MagicMock
1198
1199 box = MagicMock(spec=Box)
1200 box.type = "wLightBox"
1201
1202 def mask(x):
1203 return f"------{x}"
1204
1205 light = Light(
1206 product=box,
1207 alias="test_mono4",
1208 methods={},
1209 extended_state={"rgbw": {"colorMode": 3}},
1210 mask=mask,
1211 desired_color="00000000",
1212 color_mode=3,
1213 effect_list=None,
1214 current_effect=None,
1215 )
1216
1217 assert light._off_value == "00"
1218
1219
1220 def test_unit_light_evaluate_off_value_ct():
1221 """Test evaluate_off_value for CT (color temperature) mode."""
1222 from blebox_uniapi.box import Box
1223 from unittest.mock import MagicMock
1224
1225 box = MagicMock(spec=Box)
1226 box.type = "wLightBox"
1227
1228 def mask(x):
1229 return f"{x}----"
1230
1231 light = Light(
1232 product=box,
1233 alias="test_cct",
1234 methods={},
1235 extended_state={"rgbw": {"colorMode": 5}},
1236 mask=mask,
1237 desired_color="ffff",
1238 color_mode=5,
1239 effect_list=None,
1240 current_effect=None,
1241 )
1242
1243 assert light._off_value == "0000"
1244
1245
1246 def test_unit_light_get_placeholder_for_color_mode():
1247 """Test _get_placeholder_for_color_mode returns correct placeholder for each color mode."""
1248 from blebox_uniapi.box import Box
1249 from blebox_uniapi.light import BleboxColorMode
1250 from unittest.mock import MagicMock
1251
1252 box = MagicMock(spec=Box)
1253 box.type = "wLightBox"
1254
1255 def mask(x):
1256 return x
1257
1258 test_cases = [
1259 (BleboxColorMode.MONO, "xx"),
1260 (BleboxColorMode.CT, "xxxx"),
1261 (BleboxColorMode.CTx2, "xxxx"),
1262 (BleboxColorMode.RGB, "xxxxxx"),
1263 (BleboxColorMode.RGBW, "xxxxxxxx"),
1264 (BleboxColorMode.RGBorW, "xxxxxxxx"),
1265 ]
1266
1267 for color_mode, expected_placeholder in test_cases:
1268 light = Light(
1269 product=box,
1270 alias=f"test_{color_mode}",
1271 methods={},
1272 extended_state={"rgbw": {"colorMode": color_mode}},
1273 mask=mask,
1274 desired_color="ff000000",
1275 color_mode=color_mode,
1276 effect_list=None,
1277 current_effect=None,
1278 )
1279 assert light._get_placeholder_for_color_mode() == expected_placeholder
1280
1281
1282 def test_unit_light_set_last_on_value_with_last_color():
1283 """Test _set_last_on_value calls expect_rgbw when last_color exists and raw is off."""
1284 from blebox_uniapi.box import Box
1285 from unittest.mock import MagicMock
1286
1287 box = MagicMock(spec=Box)
1288 box.type = "wLightBox"
1289 box.expect_rgbw = MagicMock(return_value="ff00ff00")
1290 box.last_data = {"rgbw": {"lastOnColor": "ff00ff00"}}
1291
1292 def mask(x):
1293 return x
1294
1295 light = Light(
1296 product=box,
1297 alias="test_last_color",
1298 methods={"last_color": "rgbw.lastOnColor"},
1299 extended_state={"rgbw": {"colorMode": 4, "lastOnColor": "ff00ff00"}},
1300 mask=mask,
1301 desired_color="00000000",
1302 color_mode=4,
1303 effect_list=None,
1304 current_effect=None,
1305 )
1306
1307 light._set_last_on_value("test_alias", box, light._off_value)
1308 box.expect_rgbw.assert_called_once_with("test_alias", "ff00ff00")
1309 assert light._last_on_state == "ff00ff00"
1310
1311
1312 def test_unit_light_set_last_on_value_without_last_color():
1313 """Test _set_last_on_value uses default_on_value when last_color is None."""
1314 from blebox_uniapi.box import Box
1315 from unittest.mock import MagicMock
1316
1317 box = MagicMock(spec=Box)
1318 box.type = "wLightBox"
1319 box.expect_rgbw = MagicMock(return_value="ff00ff00")
1320 box.last_data = {"rgbw": {}}
1321
1322 def mask(x):
1323 return x
1324
1325 light = Light(
1326 product=box,
1327 alias="test_no_last_color",
1328 methods={"last_color": "rgbw.lastOnColor"},
1329 extended_state={"rgbw": {"colorMode": 4}},
1330 mask=mask,
1331 desired_color="00000000",
1332 color_mode=4,
1333 effect_list=None,
1334 current_effect=None,
1335 )
1336
1337 light._set_last_on_value("test_alias", box, light._off_value)
1338 box.expect_rgbw.assert_not_called()
1339 assert light._last_on_state == light._default_on_value
1340
1341
1342 def test_unit_light_swap_ww_cw():
1343 """Test swap_ww_cw preserves first 3 channels and swaps last 2."""
1344 value = [100, 150, 200, 120, 80]
1345 result = Light.swap_ww_cw(value)
1346 assert result == [100, 150, 200, 80, 120]
1347
1348
1349 def test_unit_light_swap_ww_cw_non_5_channel():
1350 """Test swap_ww_cw returns unchanged for non-5-channel values."""
1351 value = [100, 150, 200]
1352 result = Light.swap_ww_cw(value)
1353 assert result == [100, 150, 200]
1354
1355
1356 def test_unit_light_scale_rgbww_brightness_preserves_ratio():
1357 """Test scale_rgbww_brightness preserves WW:CW ratio within rounding tolerance."""
1358 value = [100, 100, 100, 120, 80]
1359 result = Light.scale_rgbww_brightness(value, 100)
1360 expected_ratio = 120 / 80
1361 actual_ratio = result[3] / result[4]
1362 assert abs(actual_ratio - expected_ratio) < 0.01
1363
1364
1365 def test_unit_light_scale_rgbww_brightness_zero():
1366 """Test scale_rgbww_brightness with zero max."""
1367 value = [0, 0, 0, 0, 0]
1368 result = Light.scale_rgbww_brightness(value, 200)
1369 assert result == [0, 0, 0, 0, 200]