Debian Codegraph @

tests/test_aio.py

raw symbols SCIP
1 from __future__ import annotations
2
3 import asyncio
4 import contextlib
5 import datetime
6 import json
7 import logging
8 import sys
9 import time
10 from unittest.mock import MagicMock, call, patch
11
12 try:
13 from unittest.mock import AsyncMock
14 except ImportError:
15 from unittest.mock import MagicMock as AsyncMock
16
17 import pytest
18
19 from flux_led import DeviceUnavailableException, aiodevice, aioscanner
20 from flux_led.aio import AIOWifiLedBulb
21 from flux_led.aioprotocol import AIOLEDENETProtocol
22 from flux_led.aioscanner import AIOBulbScanner, LEDENETDiscovery
23 from flux_led.const import (
24 COLOR_MODE_CCT,
25 COLOR_MODE_RGB,
26 COLOR_MODE_RGBW,
27 COLOR_MODE_RGBWW,
28 EFFECT_MUSIC,
29 MAX_TEMP,
30 MIN_TEMP,
31 PUSH_UPDATE_INTERVAL,
32 MultiColorEffects,
33 WhiteChannelType,
34 )
35 from flux_led.protocol import (
36 PROTOCOL_LEDENET_8BYTE_AUTO_ON,
37 PROTOCOL_LEDENET_8BYTE_DIMMABLE_EFFECTS,
38 PROTOCOL_LEDENET_9BYTE,
39 PROTOCOL_LEDENET_25BYTE,
40 PROTOCOL_LEDENET_ADDRESSABLE_CHRISTMAS,
41 PROTOCOL_LEDENET_ORIGINAL,
42 LEDENETRawState,
43 PowerRestoreState,
44 PowerRestoreStates,
45 ProtocolLEDENET25Byte,
46 ProtocolLEDENETCCT,
47 ProtocolLEDENETCCTWrapped,
48 RemoteConfig,
49 )
50 from flux_led.scanner import (
51 FluxLEDDiscovery,
52 create_udp_socket,
53 is_legacy_device,
54 merge_discoveries,
55 )
56 from flux_led.timer import LedTimer
57
58 IP_ADDRESS = "127.0.0.1"
59 MODEL_NUM_HEX = "0x35"
60 MODEL = "AZ120444"
61 MODEL_DESCRIPTION = "Bulb RGBCW"
62 FLUX_MAC_ADDRESS = "aabbccddeeff"
63
64 FLUX_DISCOVERY_PARTIAL = FluxLEDDiscovery(
65 ipaddr=IP_ADDRESS,
66 model=MODEL,
67 id=FLUX_MAC_ADDRESS,
68 model_num=None,
69 version_num=None,
70 firmware_date=None,
71 model_info=None,
72 model_description=None,
73 )
74 FLUX_DISCOVERY = FluxLEDDiscovery(
75 ipaddr=IP_ADDRESS,
76 model=MODEL,
77 id=FLUX_MAC_ADDRESS,
78 model_num=0x25,
79 version_num=0x04,
80 firmware_date=datetime.date(2021, 5, 5),
81 model_info=MODEL,
82 model_description=MODEL_DESCRIPTION,
83 )
84 FLUX_DISCOVERY_24G_REMOTE = FluxLEDDiscovery(
85 ipaddr=IP_ADDRESS,
86 model="AK001-ZJ2148",
87 id=FLUX_MAC_ADDRESS,
88 model_num=0x25,
89 version_num=0x04,
90 firmware_date=datetime.date(2021, 5, 5),
91 model_info=MODEL,
92 model_description=MODEL_DESCRIPTION,
93 )
94 FLUX_DISCOVERY_LEGACY = FluxLEDDiscovery(
95 ipaddr=IP_ADDRESS,
96 model=MODEL,
97 id="ACCF23123456",
98 model_num=0x23,
99 version_num=0x04,
100 firmware_date=datetime.date(2021, 5, 5),
101 model_info=MODEL,
102 model_description=MODEL_DESCRIPTION,
103 )
104 FLUX_DISCOVERY_MISSING_HARDWARE = FluxLEDDiscovery(
105 ipaddr=IP_ADDRESS,
106 model=None,
107 id=FLUX_MAC_ADDRESS,
108 model_num=0x25,
109 version_num=0x04,
110 firmware_date=datetime.date(2021, 5, 5),
111 model_info=MODEL,
112 model_description=MODEL_DESCRIPTION,
113 )
114
115
116 class MinJSONEncoder(json.JSONEncoder):
117 def default(self, o):
118 if isinstance(o, set):
119 return list(o)
120 return json.JSONEncoder.default(self, o)
121
122
123 def mock_coro(return_value=None, exception=None):
124 """Return a coro that returns a value or raise an exception."""
125 fut = asyncio.Future()
126 if exception is not None:
127 fut.set_exception(exception)
128 else:
129 fut.set_result(return_value)
130 return fut
131
132
133 @pytest.fixture
134 async def mock_discovery_aio_protocol():
135 """Fixture to mock an asyncio connection."""
136 loop = asyncio.get_running_loop()
137 future = asyncio.Future()
138
139 async def _wait_for_connection():
140 transport, protocol = await future
141 await asyncio.sleep(0)
142 await asyncio.sleep(0)
143 return transport, protocol
144
145 async def _mock_create_datagram_endpoint(func, sock=None):
146 protocol: LEDENETDiscovery = func()
147 transport = MagicMock()
148 protocol.connection_made(transport)
149 with contextlib.suppress(asyncio.InvalidStateError):
150 future.set_result((transport, protocol))
151 return transport, protocol
152
153 with (
154 patch.object(loop, "create_datagram_endpoint", _mock_create_datagram_endpoint),
155 patch.object(aioscanner, "MESSAGE_SEND_INTERLEAVE_DELAY", 0),
156 ):
157 yield _wait_for_connection
158
159
160 @pytest.fixture
161 async def mock_aio_protocol():
162 """Fixture to mock an asyncio connection."""
163 loop = asyncio.get_running_loop()
164 future = asyncio.Future()
165
166 async def _wait_for_connection():
167 transport, protocol = await future
168 await asyncio.sleep(0)
169 await asyncio.sleep(0)
170 await asyncio.sleep(0)
171 return transport, protocol
172
173 async def _mock_create_connection(func, ip, port):
174 protocol: AIOLEDENETProtocol = func()
175 transport = MagicMock()
176 protocol.connection_made(transport)
177 with contextlib.suppress(asyncio.InvalidStateError):
178 future.set_result((transport, protocol))
179 return transport, protocol
180
181 with patch.object(loop, "create_connection", _mock_create_connection):
182 yield _wait_for_connection
183
184
185 @pytest.mark.asyncio
186 async def test_no_initial_response(mock_aio_protocol):
187 """Test we try switching protocol if we get no initial response."""
188 light = AIOWifiLedBulb("192.168.1.166", timeout=0.01)
189 assert light.protocol is None
190
191 def _updated_callback(*args, **kwargs):
192 pass
193
194 task = asyncio.create_task(light.async_setup(_updated_callback))
195 transport, protocol = await mock_aio_protocol()
196 with pytest.raises(RuntimeError):
197 await task
198
199 assert transport.mock_calls == [
200 call.get_extra_info("peername"),
201 call.write(bytearray(b"\x81\x8a\x8b\x96")),
202 call.write_eof(),
203 call.close(),
204 ]
205 assert not light.available
206 assert light.protocol is PROTOCOL_LEDENET_ORIGINAL
207
208
209 @pytest.mark.asyncio
210 async def test_invalid_initial_response(mock_aio_protocol):
211 """Test we try switching protocol if we an unexpected response."""
212 light = AIOWifiLedBulb("192.168.1.166", timeout=0.01)
213
214 def _updated_callback(*args, **kwargs):
215 pass
216
217 task = asyncio.create_task(light.async_setup(_updated_callback))
218 transport, protocol = await mock_aio_protocol()
219 light._aio_protocol.data_received(b"\x31\x25")
220 with pytest.raises(RuntimeError):
221 await task
222
223 assert transport.mock_calls == [
224 call.get_extra_info("peername"),
225 call.write(bytearray(b"\x81\x8a\x8b\x96")),
226 call.write_eof(),
227 call.close(),
228 ]
229 assert not light.available
230
231
232 @pytest.mark.asyncio
233 async def test_cannot_determine_strip_type(mock_aio_protocol):
234 """Test we raise RuntimeError when we cannot determine the strip type."""
235 light = AIOWifiLedBulb("192.168.1.166", timeout=0.01)
236
237 def _updated_callback(*args, **kwargs):
238 pass
239
240 task = asyncio.create_task(light.async_setup(_updated_callback))
241 await mock_aio_protocol()
242 # protocol state
243 light._aio_protocol.data_received(
244 b"\x81\xa3#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\xd5"
245 )
246 with pytest.raises(RuntimeError):
247 await task
248 assert not light.available
249
250
251 @pytest.mark.asyncio
252 async def test_setting_discovery(mock_aio_protocol):
253 """Test we can pass discovery to AIOWifiLedBulb."""
254 light = AIOWifiLedBulb("192.168.1.166", timeout=0.01)
255
256 def _updated_callback(*args, **kwargs):
257 pass
258
259 task = asyncio.create_task(light.async_setup(_updated_callback))
260 await mock_aio_protocol()
261 # protocol state
262 light._aio_protocol.data_received(
263 b"\x81\x35\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xee"
264 )
265 discovery = FluxLEDDiscovery(
266 {
267 "firmware_date": datetime.date(2021, 1, 9),
268 "id": "B4E842E10586",
269 "ipaddr": "192.168.213.259",
270 "model": "AK001-ZJ2145",
271 "model_description": "Bulb RGBCW",
272 "model_info": "ZG-BL-PWM",
273 "model_num": 53,
274 "remote_access_enabled": False,
275 "remote_access_host": None,
276 "remote_access_port": None,
277 "version_num": 98,
278 }
279 )
280
281 await task
282 assert light.available
283 assert light.model == "Bulb RGBCW (0x35)"
284 light.discovery = discovery
285 assert light.model == "Bulb RGBCW (0x35)"
286 assert light.discovery == discovery
287
288
289 @pytest.mark.asyncio
290 async def test_reassemble(mock_aio_protocol):
291 """Test we can reassemble."""
292 light = AIOWifiLedBulb("192.168.1.166")
293
294 def _updated_callback(*args, **kwargs):
295 pass
296
297 task = asyncio.create_task(light.async_setup(_updated_callback))
298 transport, protocol = await mock_aio_protocol()
299 light._aio_protocol.data_received(
300 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
301 )
302 await task
303 assert light.color_modes == {COLOR_MODE_RGBWW, COLOR_MODE_CCT}
304 assert light.protocol == PROTOCOL_LEDENET_9BYTE
305 assert light.model_num == 0x25
306 assert light.model == "Controller RGB/WW/CW (0x25)"
307 assert light.is_on is True
308 assert len(light.effect_list) == 21
309
310 light._aio_protocol.data_received(
311 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
312 b"\x81\x25\x24\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xdf"
313 )
314 await asyncio.sleep(0)
315 assert light.is_on is False
316
317 light._aio_protocol.data_received(b"\x81")
318 light._aio_protocol.data_received(
319 b"\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f"
320 )
321 light._aio_protocol.data_received(b"\xde")
322 await asyncio.sleep(0)
323 assert light.is_on is True
324
325 transport.reset_mock()
326 await light.async_set_device_config()
327 assert len(transport.mock_calls) == 1
328 assert transport.mock_calls[0][0] == "write"
329 assert transport.mock_calls[0][1][0] == b"b\x05\x0fv"
330
331 transport.reset_mock()
332 await light.async_set_device_config(operating_mode="CCT")
333 assert len(transport.mock_calls) == 1
334 assert transport.mock_calls[0][0] == "write"
335 assert transport.mock_calls[0][1][0] == b"b\x02\x0fs"
336
337
338 @pytest.mark.asyncio
339 async def test_extract_from_outer_message(mock_aio_protocol):
340 """Test we can can extract a message wrapped with an outer message."""
341 light = AIOWifiLedBulb("192.168.1.166")
342
343 def _updated_callback(*args, **kwargs):
344 pass
345
346 task = asyncio.create_task(light.async_setup(_updated_callback))
347 await mock_aio_protocol()
348 light._aio_protocol.data_received(
349 b"\xb0\xb1\xb2\xb3\x00\x01\x01\x81\x00\x0e\x81\x1a\x23\x61\x07\x00\xff\x00\x00\x00\x01\x00\x06\x2c\xaf"
350 b"\xb0\xb1\xb2\xb3\x00\x01\x01\x81\x00\x0e\x81\x1a\x23\x61\x07\x00\xff\x00\x00\x00\x01\x00\x06\x2c\xaf"
351 )
352 await task
353 assert light.color_modes == {COLOR_MODE_RGB}
354 assert light.protocol == PROTOCOL_LEDENET_ADDRESSABLE_CHRISTMAS
355 assert light.model_num == 0x1A
356 assert light.model == "String Lights (0x1A)"
357 assert light.is_on is True
358 assert len(light.effect_list) == 101
359 assert light.rgb == (255, 0, 0)
360
361
362 @pytest.mark.asyncio
363 async def test_extract_from_outer_message_and_reassemble(mock_aio_protocol):
364 """Test we can can extract a message wrapped with an outer message."""
365 light = AIOWifiLedBulb("192.168.1.166")
366
367 def _updated_callback(*args, **kwargs):
368 pass
369
370 task = asyncio.create_task(light.async_setup(_updated_callback))
371 await mock_aio_protocol()
372 for byte in b"\xb0\xb1\xb2\xb3\x00\x01\x01\x81\x00\x0e\x81\x1a\x23\x61\x07\x00\xff\x00\x00\x00\x01\x00\x06\x2c\xaf":
373 light._aio_protocol.data_received(bytearray([byte]))
374 await task
375 assert light.color_modes == {COLOR_MODE_RGB}
376 assert light.protocol == PROTOCOL_LEDENET_ADDRESSABLE_CHRISTMAS
377 assert light.model_num == 0x1A
378 assert light.model == "String Lights (0x1A)"
379 assert light.is_on is True
380 assert len(light.effect_list) == 101
381 assert light.rgb == (255, 0, 0)
382
383
384 @pytest.mark.asyncio
385 async def test_turn_on_off(mock_aio_protocol, caplog: pytest.LogCaptureFixture):
386 """Test we can turn on and off."""
387 light = AIOWifiLedBulb("192.168.1.166")
388
389 def _updated_callback(*args, **kwargs):
390 pass
391
392 task = asyncio.create_task(light.async_setup(_updated_callback))
393 await mock_aio_protocol()
394 light._aio_protocol.data_received(
395 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
396 )
397 await task
398
399 data = []
400
401 def _send_data(*args, **kwargs):
402 light._aio_protocol.data_received(data.pop(0))
403
404 with (
405 patch.object(aiodevice, "POWER_STATE_TIMEOUT", 0.010),
406 patch.object(light._aio_protocol, "write", _send_data),
407 ):
408 data = [
409 b"\xf0\x71\x24\x85",
410 b"\x81\x25\x24\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xdf",
411 ]
412 await light.async_turn_off()
413 await asyncio.sleep(0)
414 assert light.is_on is False
415 assert len(data) == 1
416
417 data = [
418 b"\xf0\x71\x24\x85",
419 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde",
420 ]
421 await light.async_turn_on()
422 await asyncio.sleep(0)
423 assert light.is_on is True
424 assert len(data) == 0
425
426 data = [b"\xf0\x71\x24\x85"]
427 await light.async_turn_off()
428 await asyncio.sleep(0)
429 assert light.is_on is False
430 assert len(data) == 0
431
432 data = [
433 b"\x81\x25\x24\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xdf",
434 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde",
435 ]
436 await light.async_turn_on()
437 await asyncio.sleep(0)
438 assert light.is_on is True
439 assert len(data) == 0
440
441 data = [
442 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde",
443 b"\x81\x25\x24\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xdf",
444 ]
445 await light.async_turn_off()
446 await asyncio.sleep(0)
447 assert light.is_on is False
448 assert len(data) == 0
449
450 data = [
451 *(
452 b"\xf0\x71\x24\x85",
453 b"\x81\x25\x24\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xdf",
454 )
455 * 5
456 ]
457 await light.async_turn_on()
458 await asyncio.sleep(0)
459 assert light.is_on is True
460 assert len(data) == 3
461 light._aio_protocol.data_received(
462 b"\x81\x25\x24\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xdf"
463 )
464 assert (
465 light.is_on is True
466 ) # transition time should now be in effect since we forced state
467
468 data = [*(b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde",) * 14]
469 await light.async_turn_off()
470 await asyncio.sleep(0)
471 # If all we get is on 0x81 responses, the bulb failed to turn off
472 assert light.is_on is True
473 assert len(data) == 2
474
475 await asyncio.sleep(0)
476 caplog.clear()
477 caplog.set_level(logging.DEBUG)
478 # Handle the failure case
479 with patch.object(aiodevice, "POWER_STATE_TIMEOUT", 0.010):
480 await asyncio.create_task(light.async_turn_off())
481 assert light.is_on is True
482 assert "Failed to set power state to False (1/6)" in caplog.text
483 assert "Failed to set power state to False (2/6)" in caplog.text
484 assert "Failed to set power state to False (3/6)" in caplog.text
485 assert "Failed to set power state to False (4/6)" in caplog.text
486 assert "Failed to set power state to False (5/6)" in caplog.text
487 assert "Failed to set power state to False (6/6)" in caplog.text
488
489 with (
490 patch.object(light._aio_protocol, "write", _send_data),
491 patch.object(aiodevice, "POWER_STATE_TIMEOUT", 0.010),
492 ):
493 data = [
494 *(
495 b"\x0f\x71\x24\xa4",
496 b"\x81\x25\x24\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xdf",
497 )
498 * 5
499 ]
500 await light.async_turn_off()
501 assert light.is_on is False
502 assert len(data) == 9
503
504 data = [
505 *(
506 b"\xf0\x71\x23\xa3",
507 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde",
508 )
509 * 5
510 ]
511 await light.async_turn_on()
512 assert light.is_on is True
513 assert len(data) == 9
514
515 data = [
516 *(
517 b"\x0f\x71\x24\xa4",
518 b"\x81\x25\x24\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xdf",
519 )
520 * 5
521 ]
522 await light.async_turn_off()
523 assert light.is_on is False
524 assert len(data) == 9
525
526 await asyncio.sleep(0)
527 caplog.clear()
528 caplog.set_level(logging.DEBUG)
529 # Handle the failure case
530 with patch.object(aiodevice, "POWER_STATE_TIMEOUT", 0.010):
531 await asyncio.create_task(light.async_turn_on())
532 assert light.is_on is False
533 assert "Failed to set power state to True (1/6)" in caplog.text
534 assert "Failed to set power state to True (2/6)" in caplog.text
535 assert "Failed to set power state to True (3/6)" in caplog.text
536 assert "Failed to set power state to True (4/6)" in caplog.text
537 assert "Failed to set power state to True (5/6)" in caplog.text
538 assert "Failed to set power state to True (6/6)" in caplog.text
539
540
541 @pytest.mark.asyncio
542 async def test_turn_on_off_via_power_state_message(
543 mock_aio_protocol, caplog: pytest.LogCaptureFixture
544 ):
545 """Test we can turn on and off via power state message."""
546 light = AIOWifiLedBulb("192.168.1.166")
547
548 def _updated_callback(*args, **kwargs):
549 pass
550
551 with patch.object(aiodevice, "POWER_STATE_TIMEOUT", 0.010):
552 task = asyncio.create_task(light.async_setup(_updated_callback))
553 await mock_aio_protocol()
554 light._aio_protocol.data_received(
555 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
556 )
557 await task
558
559 task = asyncio.create_task(light.async_turn_off())
560 # Wait for the future to get added
561 await asyncio.sleep(0)
562 light._ignore_next_power_state_update = False
563 light._aio_protocol.data_received(b"\x0f\x71\x24\xa4")
564 await asyncio.sleep(0)
565 assert light.is_on is False
566 await task
567
568 task = asyncio.create_task(light.async_turn_on())
569 await asyncio.sleep(0)
570 light._ignore_next_power_state_update = False
571 light._aio_protocol.data_received(b"\x0f\x71\x23\xa3")
572 await asyncio.sleep(0)
573 assert light.is_on is True
574 await task
575
576
577 @pytest.mark.asyncio
578 async def test_turn_on_off_via_assessable_state_message(
579 mock_aio_protocol, caplog: pytest.LogCaptureFixture
580 ):
581 """Test we can turn on and off via addressable state message."""
582 light = AIOWifiLedBulb("192.168.1.166")
583
584 def _updated_callback(*args, **kwargs):
585 pass
586
587 with patch.object(aiodevice, "POWER_STATE_TIMEOUT", 0.025):
588 task = asyncio.create_task(light.async_setup(_updated_callback))
589 await mock_aio_protocol()
590 # protocol state
591 light._aio_protocol.data_received(
592 b"\x81\xa3#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\xd5"
593 )
594 # ic sorting
595 light._aio_protocol.data_received(
596 b"\x00\x63\x00\x19\x00\x02\x04\x03\x19\x02\xa0"
597 )
598 await task
599
600 data = None
601
602 def _send_data(*args, **kwargs):
603 light._aio_protocol.data_received(data)
604
605 with patch.object(light._aio_protocol, "write", _send_data):
606 data = b"\xb0\xb1\xb2\xb3\x00\x01\x01\x23\x00\x0e\x81\xa3\x24\x25\xff\x47\x64\xff\xff\x00\x01\x00\x1e\x34\x61"
607 await light.async_turn_off()
608 assert light.is_on is False
609
610 data = b"\xb0\xb1\xb2\xb3\x00\x01\x01\x24\x00\x0e\x81\xa3\x23\x25\x5f\x21\x64\xff\xff\x00\x01\x00\x1e\x6d\xd4"
611 await light.async_turn_on()
612 assert light.is_on is True
613
614
615 @pytest.mark.asyncio
616 async def test_shutdown(mock_aio_protocol):
617 """Test we can shutdown."""
618 light = AIOWifiLedBulb("192.168.1.166")
619
620 def _updated_callback(*args, **kwargs):
621 pass
622
623 task = asyncio.create_task(light.async_setup(_updated_callback))
624 await mock_aio_protocol()
625 light._aio_protocol.data_received(
626 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
627 )
628 await task
629
630 await light.async_stop()
631 await asyncio.sleep(0) # make sure nothing throws
632
633
634 @pytest.mark.asyncio
635 async def test_handling_connection_lost(mock_aio_protocol):
636 """Test we can reconnect."""
637 light = AIOWifiLedBulb("192.168.1.166")
638
639 def _updated_callback(*args, **kwargs):
640 pass
641
642 with patch.object(aiodevice, "POWER_STATE_TIMEOUT", 0.025):
643 task = asyncio.create_task(light.async_setup(_updated_callback))
644 await mock_aio_protocol()
645 light._aio_protocol.data_received(
646 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
647 )
648 await task
649
650 light._aio_protocol.connection_lost(None)
651 await asyncio.sleep(0) # make sure nothing throws
652
653 # Test we reconnect and can turn off
654 task = asyncio.create_task(light.async_turn_off())
655 # Wait for the future to get added
656 await asyncio.sleep(0.1) # wait for reconnect
657 light._aio_protocol.data_received(
658 b"\x81\x25\x24\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xdf"
659 )
660 await asyncio.sleep(0)
661 assert light.is_on is False
662 await task
663
664
665 @pytest.mark.asyncio
666 async def test_handling_unavailable_after_no_response(mock_aio_protocol):
667 """Test we handle the bulb not responding."""
668 light = AIOWifiLedBulb("192.168.1.166")
669
670 def _updated_callback(*args, **kwargs):
671 pass
672
673 task = asyncio.create_task(light.async_setup(_updated_callback))
674 await mock_aio_protocol()
675 light._aio_protocol.data_received(
676 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
677 )
678 await task
679
680 await light.async_update()
681 await light.async_update()
682 await light.async_update()
683 await light.async_update()
684 with pytest.raises(RuntimeError):
685 await light.async_update()
686 assert light.available is False
687
688
689 @pytest.mark.asyncio
690 async def test_handling_unavailable_after_no_response_force(mock_aio_protocol):
691 """Test we handle the bulb not responding."""
692 light = AIOWifiLedBulb("192.168.1.166")
693
694 def _updated_callback(*args, **kwargs):
695 pass
696
697 task = asyncio.create_task(light.async_setup(_updated_callback))
698 transport, original_aio_protocol = await mock_aio_protocol()
699 light._aio_protocol.data_received(
700 b"\x81\xa3#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\xd5"
701 )
702 # ic state
703 light._aio_protocol.data_received(
704 b"\xb0\xb1\xb2\xb3\x00\x01\x01\x00\x00\x0b\x00\x63\x00\x90\x00\x01\x07\x08\x90\x01\x94\xfb"
705 )
706 await task
707 assert light._protocol.power_push_updates is True
708
709 transport.reset_mock()
710 await light.async_update()
711 assert len(transport.mock_calls) == 1
712 assert transport.mock_calls[0][0] == "write"
713 assert (
714 transport.mock_calls[0][1][0]
715 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x01\x00\x04\x81\x8a\x8b\x96\xf9"
716 )
717
718 light._last_update_time = time.monotonic() - (PUSH_UPDATE_INTERVAL + 1)
719 await light.async_update()
720 light._last_update_time = time.monotonic() - (PUSH_UPDATE_INTERVAL + 1)
721 await light.async_update()
722 light._last_update_time = time.monotonic() - (PUSH_UPDATE_INTERVAL + 1)
723 await light.async_update()
724 light._last_update_time = time.monotonic() - (PUSH_UPDATE_INTERVAL + 1)
725 with pytest.raises(RuntimeError):
726 await light.async_update()
727 assert light.available is False
728
729 # simulate reconnect
730 await light.async_update()
731 assert light._aio_protocol != original_aio_protocol
732 light._aio_protocol = original_aio_protocol
733
734 transport.reset_mock()
735 light._aio_protocol.data_received(
736 b"\x81\xa3#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\xd5"
737 )
738 await light.async_update(force=True)
739 assert len(transport.mock_calls) == 1
740 assert transport.mock_calls[0][0] == "write"
741 assert (
742 transport.mock_calls[0][1][0]
743 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x06\x00\x04\x81\x8a\x8b\x96\xfe"
744 )
745 assert light.available is True
746 light._aio_protocol.data_received(
747 b"\x81\xa3#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\xd5"
748 )
749 assert light.available is True
750
751
752 @pytest.mark.asyncio
753 async def test_async_set_levels(mock_aio_protocol, caplog: pytest.LogCaptureFixture):
754 """Test we can set levels."""
755 light = AIOWifiLedBulb("192.168.1.166")
756
757 def _updated_callback(*args, **kwargs):
758 pass
759
760 task = asyncio.create_task(light.async_setup(_updated_callback))
761 transport, protocol = await mock_aio_protocol()
762 light._aio_protocol.data_received(
763 b"\x81\x33\x24\x61\x23\x01\x00\xff\x00\x00\x04\x00\x0f\x6f"
764 )
765 await task
766 assert light.model_num == 0x33
767 assert light.version_num == 4
768 assert light.wiring == "GRB"
769 assert light.wiring_num == 2
770 assert light.wirings == ["RGB", "GRB", "BRG"]
771 assert light.operating_mode is None
772 assert light.dimmable_effects is False
773 assert light.requires_turn_on is True
774 assert light._protocol.power_push_updates is False
775 assert light._protocol.state_push_updates is False
776
777 transport.reset_mock()
778 await light.async_set_device_config()
779 assert len(transport.mock_calls) == 1
780 assert transport.mock_calls[0][0] == "write"
781 assert transport.mock_calls[0][1][0] == b"b\x00\x02\x0fs"
782
783 transport.reset_mock()
784 await light.async_set_device_config(wiring="BRG")
785 assert len(transport.mock_calls) == 1
786 assert transport.mock_calls[0][0] == "write"
787 assert transport.mock_calls[0][1][0] == b"b\x00\x03\x0ft"
788
789 transport.reset_mock()
790 with pytest.raises(ValueError):
791 # ValueError: RGBW command sent to non-RGBW devic
792 await light.async_set_levels(255, 255, 255, 255, 255)
793
794 await light.async_set_levels(255, 0, 0)
795
796 assert transport.mock_calls[0][0] == "write"
797 assert transport.mock_calls[0][1][0] == b"1\xff\x00\x00\x00\x00\x0f?"
798
799 # light is on
800 light._aio_protocol.data_received(
801 b"\x81\x33\x23\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\x65"
802 )
803 transport.reset_mock()
804 await light.async_update()
805 await light.async_update()
806 await light.async_update()
807 await light.async_update()
808 await asyncio.sleep(0)
809 assert len(transport.mock_calls) == 4
810
811 # light is off
812 light._aio_protocol.data_received(
813 b"\x81\x33\x24\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\x66"
814 )
815 transport.reset_mock()
816 await light.async_update()
817 await light.async_update()
818 await light.async_update()
819 await light.async_update()
820 await asyncio.sleep(0)
821 assert len(transport.mock_calls) == 4
822
823 with pytest.raises(ValueError):
824 await light.async_set_preset_pattern(101, 50, 100)
825
826
827 @pytest.mark.asyncio
828 async def test_async_set_levels_0x52(
829 mock_aio_protocol, caplog: pytest.LogCaptureFixture
830 ):
831 """Test we can set levels."""
832 light = AIOWifiLedBulb("192.168.1.166")
833
834 def _updated_callback(*args, **kwargs):
835 pass
836
837 task = asyncio.create_task(light.async_setup(_updated_callback))
838 transport, protocol = await mock_aio_protocol()
839 light._aio_protocol.data_received(
840 b"\x81\x52\x23\x61\x00\x00\xff\x00\x00\x00\x01\x00\x00\x57"
841 )
842 await task
843 assert light.model_num == 0x52
844 assert light.version_num == 1
845 assert light.wiring is None
846 assert light.wiring_num is None
847 assert light.wirings is None
848 assert light.operating_mode is None
849 assert light.dimmable_effects is False
850 assert light.requires_turn_on is True
851 assert light._protocol.power_push_updates is False
852 assert light._protocol.state_push_updates is False
853
854 transport.reset_mock()
855 with pytest.raises(ValueError):
856 # ValueError: RGBW command sent to non-RGBW devic
857 await light.async_set_levels(255, 255, 255, 255, 255)
858
859 transport.reset_mock()
860 await light.async_set_levels(0, 0, 0, 255, 255)
861 assert transport.mock_calls[0][0] == "write"
862 assert transport.mock_calls[0][1][0] == b"1\xff\xff\x00\x00\x00\x0f>"
863
864 transport.reset_mock()
865 await light.async_set_levels(0, 0, 0, 128, 255)
866 assert transport.mock_calls[0][0] == "write"
867 assert transport.mock_calls[0][1][0] == b"1\x80\xff\x00\x00\x00\x0f\xbf"
868
869 transport.reset_mock()
870 await light.async_set_levels(0, 0, 0, 0, 128)
871 assert transport.mock_calls[0][0] == "write"
872 assert transport.mock_calls[0][1][0] == b"1\x00\x80\x00\x00\x00\x0f\xc0"
873
874
875 @pytest.mark.asyncio
876 async def test_async_set_effect(mock_aio_protocol, caplog: pytest.LogCaptureFixture):
877 """Test we can set an effect."""
878 light = AIOWifiLedBulb("192.168.1.166")
879
880 def _updated_callback(*args, **kwargs):
881 pass
882
883 task = asyncio.create_task(light.async_setup(_updated_callback))
884 transport, protocol = await mock_aio_protocol()
885 light._aio_protocol.data_received(
886 b"\x81\xa3#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\xd5"
887 )
888 # ic state
889 light._aio_protocol.data_received(b"\x00\x63\x00\x19\x00\x02\x04\x03\x19\x02\xa0")
890 await task
891 assert light.model_num == 0xA3
892 assert light.dimmable_effects is True
893 assert light.requires_turn_on is False
894 assert light._protocol.power_push_updates is True
895 assert light._protocol.state_push_updates is True
896
897 transport.reset_mock()
898 await light.async_set_effect("random", 50)
899 assert transport.mock_calls[0][0] == "write"
900 assert transport.mock_calls[0][1][0].startswith(b"\xb0\xb1\xb2\xb3")
901
902 transport.reset_mock()
903 await light.async_set_effect("RBM 1", 50)
904 assert transport.mock_calls[0][0] == "write"
905 assert (
906 transport.mock_calls[0][1][0]
907 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x02\x00\x05B\x012d\xd9\x81"
908 )
909 assert light.effect == "RBM 1"
910
911 transport.reset_mock()
912 await light.async_set_brightness(255)
913 assert transport.mock_calls[0][0] == "write"
914 assert (
915 transport.mock_calls[0][1][0]
916 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x03\x00\x05B\x01\x10d\xb7>"
917 )
918
919 transport.reset_mock()
920 await light.async_set_brightness(128)
921 assert transport.mock_calls[0][0] == "write"
922 assert (
923 transport.mock_calls[0][1][0]
924 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x04\x00\x05B\x01\x102\x85\xdb"
925 )
926
927 for i in range(5, 255):
928 transport.reset_mock()
929 await light.async_set_brightness(128)
930 assert transport.mock_calls[0][0] == "write"
931 counter_byte = transport.mock_calls[0][1][0][7]
932 assert counter_byte == i
933
934 transport.reset_mock()
935 await light.async_set_brightness(128)
936 assert transport.mock_calls[0][0] == "write"
937 counter_byte = transport.mock_calls[0][1][0][7]
938 assert counter_byte == 0
939
940 transport.reset_mock()
941 # Verify brightness clamped
942 await light.async_set_effect("RBM 1", 50, brightness=500)
943 assert transport.mock_calls[0][0] == "write"
944 assert (
945 transport.mock_calls[0][1][0]
946 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x01\x00\x05B\x012d\xd9\x80"
947 )
948
949
950 @pytest.mark.asyncio
951 async def test_SK6812RGBW(mock_aio_protocol, caplog: pytest.LogCaptureFixture):
952 """Test we can set set zone colors."""
953 light = AIOWifiLedBulb("192.168.1.166")
954
955 def _updated_callback(*args, **kwargs):
956 pass
957
958 task = asyncio.create_task(light.async_setup(_updated_callback))
959 transport, protocol = await mock_aio_protocol()
960 light._aio_protocol.data_received(
961 b"\x81\xa3#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\xd5"
962 )
963 # ic state
964 light._aio_protocol.data_received(
965 b"\xb0\xb1\xb2\xb3\x00\x01\x01\x00\x00\x0b\x00\x63\x00\x90\x00\x01\x07\x08\x90\x01\x94\xfb"
966 )
967
968 await task
969 assert light.pixels_per_segment == 144
970 assert light.segments == 1
971 assert light.music_pixels_per_segment == 144
972 assert light.music_segments == 1
973 assert light.ic_types == [
974 "WS2812B",
975 "SM16703",
976 "SM16704",
977 "WS2811",
978 "UCS1903",
979 "SK6812",
980 "SK6812RGBW",
981 "INK1003",
982 "UCS2904B",
983 ]
984 assert light.ic_type == "SK6812RGBW"
985 assert light.ic_type_num == 7
986 assert light.operating_mode is None
987 assert light.operating_modes is None
988 assert light.wiring == "WGRB"
989 assert light.wiring_num == 8
990 assert light.wirings == [
991 "RGBW",
992 "RBGW",
993 "GRBW",
994 "GBRW",
995 "BRGW",
996 "BGRW",
997 "WRGB",
998 "WRBG",
999 "WGRB",
1000 "WGBR",
1001 "WBRG",
1002 "WBGR",
1003 ]
1004 assert light.model_num == 0xA3
1005 assert light.dimmable_effects is True
1006 assert light.requires_turn_on is False
1007 assert light.color_mode == COLOR_MODE_RGBW
1008 assert light.color_modes == {COLOR_MODE_RGBW, COLOR_MODE_CCT}
1009 diag = light.diagnostics
1010 assert isinstance(json.dumps(diag, cls=MinJSONEncoder), str)
1011 assert diag["device_state"]["wiring_num"] == 8
1012 assert (
1013 diag["last_messages"]["state"]
1014 == "0x81 0xA3 0x23 0x25 0x01 0x10 0x64 0x00 0x00 0x00 0x04 0x00 0xF0 0xD5"
1015 )
1016 transport.reset_mock()
1017
1018 with patch.object(light, "_async_device_config_resync", mock_coro):
1019 await light.async_set_device_config(ic_type="SK6812RGBW", wiring="WRGB")
1020 assert len(transport.mock_calls) == 1
1021 assert transport.mock_calls[0][0] == "write"
1022 assert (
1023 transport.mock_calls[0][1][0]
1024 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x01\x00\x0bb\x00\x90\x00\x01\x07\x06\x90\x01\xf0\x81\xd6"
1025 )
1026
1027 transport.reset_mock()
1028 with patch.object(aiodevice, "COMMAND_SPACING_DELAY", 0):
1029 await light.async_set_levels(r=255, g=255, b=255, w=255)
1030 assert transport.mock_calls == [
1031 call.write(
1032 bytearray(
1033 b"\xb0\xb1\xb2\xb3\x00\x01\x01\x02\x00\rA\x01\xff\xff\xff\x00\x00\x00`\xff\x00\x00\x9e\x13"
1034 )
1035 ),
1036 call.write(bytearray(b"\xb0\xb1\xb2\xb3\x00\x01\x01\x03\x00\x03G\xffFZ")),
1037 ]
1038
1039 transport.reset_mock()
1040 await light.async_set_levels(w=255)
1041 assert transport.mock_calls == [
1042 call.write(bytearray(b"\xb0\xb1\xb2\xb3\x00\x01\x01\x04\x00\x03G\xffF["))
1043 ]
1044 light._transition_complete_time = 0
1045
1046 light._aio_protocol.data_received(
1047 b"\x81\xa3\x23\x61\x01\x32\x40\x40\x40\x80\x01\x00\x90\xac"
1048 )
1049 assert light.raw_state.warm_white == 0
1050 light._aio_protocol.data_received(
1051 b"\x81\xa3\x23\x61\x01\x32\x40\x40\x40\xe4\x01\x00\x90\x10"
1052 )
1053 assert light.raw_state.warm_white == 255
1054 light._aio_protocol.data_received(
1055 b"\x81\xa3\x23\x61\x01\x32\x40\x40\x40\xb1\x01\x00\x90\xdd"
1056 )
1057 assert light.raw_state.warm_white == 125
1058
1059 transport.reset_mock()
1060 with patch.object(aiodevice, "COMMAND_SPACING_DELAY", 0):
1061 await light.async_set_white_temp(6500, 255)
1062 assert transport.mock_calls == [
1063 call.write(
1064 bytearray(
1065 bytearray(
1066 b"\xb0\xb1\xb2\xb3\x00\x01\x01\x05\x00\rA\x01\xff\xff\xff\x00\x00\x00`\xff\x00\x00\x9e\x16"
1067 )
1068 )
1069 ),
1070 call.write(bytearray(b"\xb0\xb1\xb2\xb3\x00\x01\x01\x06\x00\x03G\x00G_")),
1071 ]
1072
1073
1074 @pytest.mark.asyncio
1075 async def test_ws2812b_a1(mock_aio_protocol, caplog: pytest.LogCaptureFixture):
1076 """Test we can determine ws2812b configuration."""
1077 light = AIOWifiLedBulb("192.168.1.166")
1078
1079 def _updated_callback(*args, **kwargs):
1080 pass
1081
1082 task = asyncio.create_task(light.async_setup(_updated_callback))
1083 transport, protocol = await mock_aio_protocol()
1084 light._aio_protocol.data_received(
1085 b"\x81\xa1#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\xd3"
1086 )
1087 # ic state
1088 light._aio_protocol.data_received(
1089 b"\x63\x00\x32\x04\x00\x00\x00\x00\x00\x00\x02\x9b"
1090 )
1091
1092 await task
1093 assert light._protocol.timer_count == 6
1094 assert light._protocol.timer_len == 14
1095 assert light._protocol.timer_response_len == 88
1096
1097 assert light.pixels_per_segment == 50
1098 assert light.segments is None
1099 assert light.music_pixels_per_segment is None
1100 assert light.music_segments is None
1101 assert light.ic_types == [
1102 "UCS1903",
1103 "SM16703",
1104 "WS2811",
1105 "WS2812B",
1106 "SK6812",
1107 "INK1003",
1108 "WS2801",
1109 "LB1914",
1110 ]
1111 assert light.ic_type == "WS2812B"
1112 assert light.ic_type_num == 4
1113 assert light.operating_mode is None
1114 assert light.operating_modes is None
1115 assert light.wiring == "GRB"
1116 assert light.wiring_num == 2
1117 assert light.wirings == ["RGB", "RBG", "GRB", "GBR", "BRG", "BGR"]
1118 assert light.model_num == 0xA1
1119 assert light.dimmable_effects is False
1120 assert light.requires_turn_on is False
1121
1122 transport.reset_mock()
1123 with patch.object(light, "_async_device_config_resync", mock_coro):
1124 await light.async_set_device_config()
1125 assert len(transport.mock_calls) == 1
1126 assert transport.mock_calls[0][0] == "write"
1127 assert (
1128 transport.mock_calls[0][1][0]
1129 == b"b\x002\x04\x00\x00\x00\x00\x00\x00\x02\xf0\x8a"
1130 )
1131
1132 transport.reset_mock()
1133 with patch.object(light, "_async_device_config_resync", mock_coro):
1134 await light.async_set_device_config(
1135 ic_type="SK6812", wiring="GRB", pixels_per_segment=300
1136 )
1137 assert len(transport.mock_calls) == 1
1138 assert transport.mock_calls[0][0] == "write"
1139 assert (
1140 transport.mock_calls[0][1][0]
1141 == b"b\x01,\x05\x00\x00\x00\x00\x00\x00\x02\xf0\x86"
1142 )
1143
1144
1145 @pytest.mark.asyncio
1146 async def test_ws2811_a2(mock_aio_protocol, caplog: pytest.LogCaptureFixture):
1147 """Test we can determine ws2811 configuration."""
1148 light = AIOWifiLedBulb("192.168.1.166")
1149
1150 def _updated_callback(*args, **kwargs):
1151 pass
1152
1153 task = asyncio.create_task(light.async_setup(_updated_callback))
1154 transport, protocol = await mock_aio_protocol()
1155 light._aio_protocol.data_received(
1156 b"\x81\xa2#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\xd4"
1157 )
1158 # ic state
1159 light._aio_protocol.data_received(b"\x00\x63\x00\x19\x00\x02\x04\x03\x19\x02\xa0")
1160
1161 await task
1162 assert light.pixels_per_segment == 25
1163 assert light.segments == 2
1164 assert light.music_pixels_per_segment == 25
1165 assert light.music_segments == 2
1166 assert light.ic_type == "WS2811B"
1167 assert light.ic_type_num == 4
1168 assert light.operating_mode is None
1169 assert light.operating_modes is None
1170 assert light.wiring == "GBR"
1171 assert light.wiring_num == 3
1172 assert light.wirings == ["RGB", "RBG", "GRB", "GBR", "BRG", "BGR"]
1173 assert light.model_num == 0xA2
1174 assert light.dimmable_effects is True
1175 assert light.requires_turn_on is False
1176
1177 transport.reset_mock()
1178 with patch.object(light, "_async_device_config_resync", mock_coro):
1179 await light.async_set_device_config()
1180 assert len(transport.mock_calls) == 1
1181 assert transport.mock_calls[0][0] == "write"
1182 assert transport.mock_calls[0][1][0] == b"b\x00\x19\x00\x02\x04\x03\x19\x02\xf0\x8f"
1183
1184 transport.reset_mock()
1185 with patch.object(light, "_async_device_config_resync", mock_coro):
1186 await light.async_set_device_config(
1187 ic_type="SK6812", wiring="GRB", pixels_per_segment=300
1188 )
1189 assert len(transport.mock_calls) == 1
1190 assert transport.mock_calls[0][0] == "write"
1191 assert transport.mock_calls[0][1][0] == b"b\x01,\x00\x02\x05\x02\x19\x02\xf0\xa3"
1192
1193 transport.reset_mock()
1194 with patch.object(light, "_async_device_config_resync", mock_coro):
1195 await light.async_set_device_config(
1196 pixels_per_segment=1000,
1197 segments=1000,
1198 music_pixels_per_segment=1000,
1199 music_segments=1000,
1200 )
1201 assert len(transport.mock_calls) == 1
1202 assert transport.mock_calls[0][0] == "write"
1203 assert transport.mock_calls[0][1][0] == b"b\x01,\x00\x06\x04\x03\x96\x06\xf0("
1204
1205
1206 @pytest.mark.asyncio
1207 async def test_ws2812b_older_a3(mock_aio_protocol, caplog: pytest.LogCaptureFixture):
1208 """Test we can determine ws2812b configuration on an older a3."""
1209 light = AIOWifiLedBulb("192.168.1.166")
1210
1211 def _updated_callback(*args, **kwargs):
1212 pass
1213
1214 task = asyncio.create_task(light.async_setup(_updated_callback))
1215 transport, protocol = await mock_aio_protocol()
1216 light._aio_protocol.data_received(
1217 b"\x81\xa3\x23\x61\x01\x32\x00\x64\x00\x00\x01\x00\x1e\x5e"
1218 )
1219 # ic state
1220 light._aio_protocol.data_received(
1221 b"\xb0\xb1\xb2\xb3\x00\x01\x01\x00\x00\x0b\x01\x63\x00\x1e\x00\x0a\x01\x00\x1e\x0a\xb5\x3d"
1222 )
1223
1224 await task
1225 assert light.pixels_per_segment == 30
1226 assert light.segments == 10
1227 assert light.music_pixels_per_segment == 30
1228 assert light.music_segments == 10
1229 assert light.ic_type == "WS2812B"
1230 assert light.ic_type_num == 1
1231 assert light.operating_mode is None
1232 assert light.operating_modes is None
1233 assert light.wiring == "RGB"
1234 assert light.wiring_num == 0
1235 assert light.wirings == ["RGB", "RBG", "GRB", "GBR", "BRG", "BGR"]
1236 assert light.model_num == 0xA3
1237 assert light.dimmable_effects is True
1238 assert light.requires_turn_on is False
1239
1240 transport.reset_mock()
1241 with patch.object(light, "_async_device_config_resync", mock_coro):
1242 await light.async_set_device_config()
1243 assert len(transport.mock_calls) == 1
1244 assert transport.mock_calls[0][0] == "write"
1245 assert (
1246 transport.mock_calls[0][1][0]
1247 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x01\x00\x0bb\x00\x1e\x00\n\x01\x00\x1e\n\xf0\xa3\x1a"
1248 )
1249
1250 transport.reset_mock()
1251 with patch.object(light, "_async_device_config_resync", mock_coro):
1252 await light.async_set_device_config(
1253 ic_type="SK6812", wiring="GRB", pixels_per_segment=300
1254 )
1255 assert len(transport.mock_calls) == 1
1256 assert transport.mock_calls[0][0] == "write"
1257 assert (
1258 transport.mock_calls[0][1][0]
1259 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x02\x00\x0bb\x01,\x00\x06\x06\x02\x1e\n\xf0\xb5?"
1260 )
1261
1262 transport.reset_mock()
1263 with patch.object(light, "_async_device_config_resync", mock_coro):
1264 await light.async_set_device_config(
1265 pixels_per_segment=1000,
1266 segments=1000,
1267 music_pixels_per_segment=1000,
1268 music_segments=1000,
1269 )
1270 assert len(transport.mock_calls) == 1
1271 assert transport.mock_calls[0][0] == "write"
1272 assert (
1273 transport.mock_calls[0][1][0]
1274 == b'\xb0\xb1\xb2\xb3\x00\x01\x01\x03\x00\x0bb\x01,\x00\x06\x01\x00\x96\x06\xf0"\x1a'
1275 )
1276
1277
1278 @pytest.mark.asyncio
1279 async def test_async_set_zones(mock_aio_protocol, caplog: pytest.LogCaptureFixture):
1280 """Test we can set set zone colors."""
1281 light = AIOWifiLedBulb("192.168.1.166")
1282
1283 def _updated_callback(*args, **kwargs):
1284 pass
1285
1286 task = asyncio.create_task(light.async_setup(_updated_callback))
1287 transport, protocol = await mock_aio_protocol()
1288 light._aio_protocol.data_received(
1289 b"\x81\xa3#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\xd5"
1290 )
1291 # ic state
1292 light._aio_protocol.data_received(b"\x00\x63\x00\x19\x00\x02\x04\x03\x19\x02\xa0")
1293 # sometimes the devices responds 2x
1294 light._aio_protocol.data_received(b"\x00\x63\x00\x19\x00\x02\x04\x03\x19\x02\xa0")
1295
1296 await task
1297 assert light.pixels_per_segment == 25
1298 assert light.segments == 2
1299 assert light.music_pixels_per_segment == 25
1300 assert light.music_segments == 2
1301 assert light.ic_types == [
1302 "WS2812B",
1303 "SM16703",
1304 "SM16704",
1305 "WS2811",
1306 "UCS1903",
1307 "SK6812",
1308 "SK6812RGBW",
1309 "INK1003",
1310 "UCS2904B",
1311 ]
1312 assert light.ic_type == "WS2811"
1313 assert light.ic_type_num == 4
1314 assert light.operating_mode is None
1315 assert light.operating_modes is None
1316 assert light.wiring == "GBR"
1317 assert light.wiring_num == 3
1318 assert light.wirings == ["RGB", "RBG", "GRB", "GBR", "BRG", "BGR"]
1319 assert light.model_num == 0xA3
1320 assert light.dimmable_effects is True
1321 assert light.requires_turn_on is False
1322
1323 transport.reset_mock()
1324 with patch.object(light, "_async_device_config_resync", mock_coro):
1325 await light.async_set_device_config()
1326 assert len(transport.mock_calls) == 1
1327 assert transport.mock_calls[0][0] == "write"
1328 assert (
1329 transport.mock_calls[0][1][0]
1330 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x01\x00\x0bb\x00\x19\x00\x02\x04\x03\x19\x02\xf0\x8f\xf2"
1331 )
1332
1333 transport.reset_mock()
1334 with patch.object(light, "_async_device_config_resync", mock_coro):
1335 await light.async_set_device_config(
1336 ic_type="SK6812",
1337 wiring="GRB",
1338 pixels_per_segment=300,
1339 segments=2,
1340 music_pixels_per_segment=150,
1341 music_segments=2,
1342 )
1343 assert len(transport.mock_calls) == 1
1344 assert transport.mock_calls[0][0] == "write"
1345 assert (
1346 transport.mock_calls[0][1][0]
1347 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x02\x00\x0bb\x01,\x00\x02\x06\x02\x96\x02\xf0!\x17"
1348 )
1349
1350 transport.reset_mock()
1351 with patch.object(light, "_async_device_config_resync", mock_coro):
1352 await light.async_set_device_config(
1353 ic_type="SK6812",
1354 wiring="GRB",
1355 pixels_per_segment=300,
1356 segments=2,
1357 music_pixels_per_segment=300,
1358 music_segments=2,
1359 )
1360 assert len(transport.mock_calls) == 1
1361 assert transport.mock_calls[0][0] == "write"
1362 assert (
1363 transport.mock_calls[0][1][0]
1364 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x03\x00\x0bb\x01,\x00\x02\x06\x02\x96\x02\xf0!\x18"
1365 )
1366
1367 transport.reset_mock()
1368
1369 await light.async_set_zones(
1370 [(255, 0, 0), (0, 0, 255)], 100, MultiColorEffects.STROBE
1371 )
1372 assert transport.mock_calls[0][0] == "write"
1373 assert transport.mock_calls[0][1][0] == bytearray(
1374 b"\xb0\xb1\xb2\xb3\x00\x01\x01\x04\x00TY\x00T\xff\x00\x00"
1375 b"\xff\x00\x00\xff\x00\x00\xff\x00\x00\xff\x00\x00\xff\x00\x00\xff"
1376 b"\x00\x00\xff\x00\x00\xff\x00\x00\xff\x00\x00\xff\x00\x00\xff\x00"
1377 b"\x00\x00\x00\xff\x00\x00\xff\x00\x00\xff\x00\x00\xff\x00\x00\xff"
1378 b"\x00\x00\xff\x00\x00\xff\x00\x00\xff\x00\x00\xff\x00\x00\xff\x00"
1379 b"\x00\xff\x00\x00\xff\x00\x00\xff\x00\x1e\x03d\x00\x19R"
1380 )
1381
1382 with pytest.raises(ValueError):
1383 await light.async_set_zones(
1384 [(255, 0, 0) for _ in range(30)],
1385 )
1386
1387
1388 @pytest.mark.asyncio
1389 async def test_async_set_zones_unsupported_device(
1390 mock_aio_protocol, caplog: pytest.LogCaptureFixture
1391 ):
1392 """Test we can set set zone colors raises valueerror on unsupported."""
1393 light = AIOWifiLedBulb("192.168.1.166")
1394
1395 def _updated_callback(*args, **kwargs):
1396 pass
1397
1398 task = asyncio.create_task(light.async_setup(_updated_callback))
1399 transport, protocol = await mock_aio_protocol()
1400 light._aio_protocol.data_received(
1401 b"\x81\x25#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\x57"
1402 )
1403 await task
1404 assert light.model_num == 0x25
1405
1406 transport.reset_mock()
1407 with pytest.raises(ValueError):
1408 await light.async_set_zones(
1409 [(255, 0, 0), (0, 0, 255)], 100, MultiColorEffects.STROBE
1410 )
1411
1412
1413 @pytest.mark.asyncio
1414 async def test_0x06_device_wiring(mock_aio_protocol, caplog: pytest.LogCaptureFixture):
1415 """Test we can get wiring for an 0x06."""
1416 light = AIOWifiLedBulb("192.168.1.166")
1417
1418 def _updated_callback(*args, **kwargs):
1419 pass
1420
1421 task = asyncio.create_task(light.async_setup(_updated_callback))
1422 transport, protocol = await mock_aio_protocol()
1423 light._aio_protocol.data_received(
1424 b"\x81\x06\x24\x61\x24\x01\x00\xff\x00\x00\x03\x00\xf0\x23"
1425 )
1426 await task
1427 assert light.model_num == 0x06
1428 assert light.pixels_per_segment is None
1429 assert light.segments is None
1430 assert light.music_pixels_per_segment is None
1431 assert light.music_segments is None
1432 assert light.ic_types is None
1433 assert light.ic_type is None
1434 assert light.operating_mode == "RGB&W"
1435 assert light.operating_modes == ["RGB&W", "RGB/W"]
1436 assert light.wiring == "GRBW"
1437 assert light.wiring_num == 2
1438 assert light.wirings == ["RGBW", "GRBW", "BRGW"]
1439
1440
1441 @pytest.mark.asyncio
1442 async def test_0x07_device_wiring(mock_aio_protocol, caplog: pytest.LogCaptureFixture):
1443 """Test we can get wiring for an 0x07."""
1444 light = AIOWifiLedBulb("192.168.1.166")
1445
1446 def _updated_callback(*args, **kwargs):
1447 pass
1448
1449 task = asyncio.create_task(light.async_setup(_updated_callback))
1450 transport, protocol = await mock_aio_protocol()
1451 light._aio_protocol.data_received(
1452 b"\x81\x07\x24\x61\xc7\x01\x00\x00\x00\x00\x02\xff\x0f\xe5"
1453 )
1454 await task
1455 assert light.model_num == 0x07
1456 assert light.pixels_per_segment is None
1457 assert light.segments is None
1458 assert light.music_pixels_per_segment is None
1459 assert light.music_segments is None
1460 assert light.ic_types is None
1461 assert light.ic_type is None
1462 assert light.operating_mode == "RGB/CCT"
1463 assert light.operating_modes == ["RGB&CCT", "RGB/CCT"]
1464 assert light.wiring == "CBRGW"
1465 assert light.wiring_num == 12
1466 assert light.wirings == [
1467 "RGBCW",
1468 "GRBCW",
1469 "BRGCW",
1470 "RGBWC",
1471 "GRBWC",
1472 "BRGWC",
1473 "WRGBC",
1474 "WGRBC",
1475 "WBRGC",
1476 "CRGBW",
1477 "CBRBW",
1478 "CBRGW",
1479 "WCRGB",
1480 "WCGRB",
1481 "WCBRG",
1482 ]
1483
1484
1485 @pytest.mark.asyncio
1486 async def test_async_set_music_mode_0x08(
1487 mock_aio_protocol, caplog: pytest.LogCaptureFixture
1488 ):
1489 """Test we can set music mode on an 0x08."""
1490 light = AIOWifiLedBulb("192.168.1.166")
1491
1492 def _updated_callback(*args, **kwargs):
1493 pass
1494
1495 with patch.object(aiodevice, "COMMAND_SPACING_DELAY", 0):
1496 task = asyncio.create_task(light.async_setup(_updated_callback))
1497 transport, protocol = await mock_aio_protocol()
1498 light._aio_protocol.data_received(
1499 b"\x81\x08#\x5d\x01\x10\x64\x00\x00\x00\x04\x00\xf0\x72"
1500 )
1501 await task
1502 assert light.model_num == 0x08
1503 assert light.version_num == 4
1504 assert light.effect == EFFECT_MUSIC
1505 assert light.microphone is True
1506 assert light.protocol == PROTOCOL_LEDENET_8BYTE_DIMMABLE_EFFECTS
1507 assert light.pixels_per_segment is None
1508 assert light.segments is None
1509 assert light.music_pixels_per_segment is None
1510 assert light.music_segments is None
1511 assert light.ic_types is None
1512 assert light.ic_type is None
1513 assert light.operating_mode is None
1514 assert light.operating_modes is None
1515 assert light.wiring is None # How can we get this in music mode?
1516 assert light.wirings == ["RGB", "GRB", "BRG"]
1517
1518 transport.reset_mock()
1519 await light.async_set_music_mode()
1520 assert transport.mock_calls[0][0] == "write"
1521 assert transport.mock_calls[0][1][0] == b"s\x01d\x0f\xe7"
1522 assert transport.mock_calls[1][0] == "write"
1523 assert transport.mock_calls[1][1][0] == b"7\x00\x007"
1524
1525 transport.reset_mock()
1526 await light.async_set_music_mode(effect=2)
1527 assert transport.mock_calls[0][0] == "write"
1528 assert transport.mock_calls[0][1][0] == b"s\x01d\x0f\xe7"
1529 assert transport.mock_calls[1][0] == "write"
1530 assert transport.mock_calls[1][1][0] == b"7\x02\x009"
1531
1532 with pytest.raises(ValueError):
1533 await light.async_set_music_mode(effect=0x08)
1534
1535
1536 @pytest.mark.asyncio
1537 async def test_async_set_music_mode_0x08_v1_firmware(
1538 mock_aio_protocol, caplog: pytest.LogCaptureFixture
1539 ):
1540 """Test we can set music mode on an 0x08 with v1 firmware."""
1541 light = AIOWifiLedBulb("192.168.1.166")
1542
1543 def _updated_callback(*args, **kwargs):
1544 pass
1545
1546 with patch.object(aiodevice, "COMMAND_SPACING_DELAY", 0):
1547 task = asyncio.create_task(light.async_setup(_updated_callback))
1548 transport, protocol = await mock_aio_protocol()
1549 light._aio_protocol.data_received(
1550 b"\x81\x08\x23\x62\x23\x01\x80\x00\x80\x00\x01\x00\x00\x33"
1551 )
1552 await task
1553 assert light.model_num == 0x08
1554 assert light.version_num == 1
1555 assert light.effect == EFFECT_MUSIC
1556 assert light.microphone is True
1557 assert light.raw_state.red == 128
1558 assert light.raw_state.green == 0
1559 assert light.raw_state.blue == 128
1560 assert light.protocol == PROTOCOL_LEDENET_8BYTE_AUTO_ON
1561 # In music mode, we always report 255 otherwise it will likely be 0
1562 assert light.brightness == 255
1563
1564 transport.reset_mock()
1565 await light.async_set_music_mode()
1566 assert len(transport.mock_calls) == 1
1567 assert transport.mock_calls[0][0] == "write"
1568 assert transport.mock_calls[0][1][0] == b"s\x01d\x0f\xe7"
1569
1570
1571 @pytest.mark.asyncio
1572 async def test_async_set_music_mode_0x08_v2_firmware(
1573 mock_aio_protocol, caplog: pytest.LogCaptureFixture
1574 ):
1575 """Test we can set music mode on an 0x08 with v2 firmware."""
1576 light = AIOWifiLedBulb("192.168.1.166")
1577
1578 def _updated_callback(*args, **kwargs):
1579 pass
1580
1581 with patch.object(aiodevice, "COMMAND_SPACING_DELAY", 0):
1582 task = asyncio.create_task(light.async_setup(_updated_callback))
1583 transport, protocol = await mock_aio_protocol()
1584 light._aio_protocol.data_received(
1585 b"\x81\x08\x23\x62\x23\x01\x80\x00\xff\x00\x02\x00\x00\xb3"
1586 )
1587 await task
1588 assert light.model_num == 0x08
1589 assert light.version_num == 2
1590 assert light.effect == EFFECT_MUSIC
1591 assert light.microphone is True
1592 assert light.protocol == PROTOCOL_LEDENET_8BYTE_DIMMABLE_EFFECTS
1593
1594 transport.reset_mock()
1595 await light.async_set_music_mode()
1596 assert transport.mock_calls[0][0] == "write"
1597 assert transport.mock_calls[0][1][0] == b"s\x01d\x0f\xe7"
1598 assert transport.mock_calls[1][0] == "write"
1599 assert transport.mock_calls[1][1][0] == b"7\x00\x007"
1600
1601
1602 @pytest.mark.asyncio
1603 async def test_async_set_music_mode_a2(
1604 mock_aio_protocol, caplog: pytest.LogCaptureFixture
1605 ):
1606 """Test we can set music mode on an 0xA2."""
1607 light = AIOWifiLedBulb("192.168.1.166")
1608
1609 def _updated_callback(*args, **kwargs):
1610 pass
1611
1612 task = asyncio.create_task(light.async_setup(_updated_callback))
1613 transport, protocol = await mock_aio_protocol()
1614 light._aio_protocol.data_received(
1615 b"\x81\xa2#\x62\x01\x10\x64\x00\x00\x00\x04\x00\xf0\x11"
1616 )
1617 # ic state
1618 light._aio_protocol.data_received(b"\x00\x63\x00\x19\x00\x02\x04\x03\x19\x02\xa0")
1619 await task
1620 assert light.model_num == 0xA2
1621 assert light.effect == EFFECT_MUSIC
1622 assert light.microphone is True
1623 assert light._protocol.state_push_updates is False
1624 assert light._protocol.power_push_updates is False
1625
1626 transport.reset_mock()
1627 await light.async_set_music_mode()
1628 assert transport.mock_calls[0][0] == "write"
1629 assert transport.mock_calls[0][1][0] == b"s\x01&\x01d\x00\x00\x00\x00\x00dd\xc7"
1630
1631 transport.reset_mock()
1632 await light.async_set_effect(EFFECT_MUSIC, 100, 100)
1633 assert transport.mock_calls[0][0] == "write"
1634 assert transport.mock_calls[0][1][0] == b"s\x01&\x01d\x00\x00\x00\x00\x00dd\xc7"
1635
1636 # light is on
1637 light._aio_protocol.data_received(
1638 b"\x81\xa2\x23\x62\x01\x10\x64\x00\x00\x00\x04\x00\xf0\x11"
1639 )
1640 transport.reset_mock()
1641 await light.async_update()
1642 await light.async_update()
1643 await light.async_update()
1644 await light.async_update()
1645 await asyncio.sleep(0)
1646 assert len(transport.mock_calls) == 4
1647
1648 # light is off
1649 light._aio_protocol.data_received(
1650 b"\x81\xa2\x24\x62\x01\x10\x64\x00\x00\x00\x04\x00\xf0\x12"
1651 )
1652 transport.reset_mock()
1653 await light.async_update()
1654 await light.async_update()
1655 await light.async_update()
1656 await light.async_update()
1657 await asyncio.sleep(0)
1658 assert len(transport.mock_calls) == 4
1659
1660
1661 @pytest.mark.asyncio
1662 async def test_async_set_music_mode_a3(
1663 mock_aio_protocol, caplog: pytest.LogCaptureFixture
1664 ):
1665 """Test we can set music mode on an 0xA3."""
1666 light = AIOWifiLedBulb("192.168.1.166")
1667
1668 def _updated_callback(*args, **kwargs):
1669 pass
1670
1671 task = asyncio.create_task(light.async_setup(_updated_callback))
1672 transport, protocol = await mock_aio_protocol()
1673 light._aio_protocol.data_received(
1674 b"\x81\xa3#\x62\x01\x10\x64\x00\x00\x00\x04\x00\xf0\x12"
1675 )
1676 # ic state
1677 light._aio_protocol.data_received(b"\x00\x63\x00\x19\x00\x02\x04\x03\x19\x02\xa0")
1678 await task
1679 assert light.model_num == 0xA3
1680 assert light.effect == EFFECT_MUSIC
1681 assert light.microphone is True
1682
1683 transport.reset_mock()
1684 await light.async_set_music_mode()
1685 assert transport.mock_calls[0][0] == "write"
1686 assert transport.mock_calls[0][1][0].startswith(b"\xb0\xb1\xb2\xb3")
1687
1688 with pytest.raises(ValueError):
1689 await light.async_set_music_mode(mode=0x08)
1690
1691 with pytest.raises(ValueError):
1692 await light.async_set_music_mode(effect=0x99)
1693
1694
1695 @pytest.mark.asyncio
1696 async def test_async_set_music_mode_device_without_mic_0x07(
1697 mock_aio_protocol, caplog: pytest.LogCaptureFixture
1698 ):
1699 """Test we can set music mode on an 0x08."""
1700 light = AIOWifiLedBulb("192.168.1.166")
1701
1702 def _updated_callback(*args, **kwargs):
1703 pass
1704
1705 task = asyncio.create_task(light.async_setup(_updated_callback))
1706 transport, protocol = await mock_aio_protocol()
1707 light._aio_protocol.data_received(
1708 b"\x81\x07#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\x39"
1709 )
1710 await task
1711 assert light.model_num == 0x07
1712 assert light.microphone is False
1713
1714 transport.reset_mock()
1715 with pytest.raises(ValueError):
1716 await light.async_set_music_mode()
1717
1718
1719 @pytest.mark.asyncio
1720 async def test_async_set_white_temp_0x35(
1721 mock_aio_protocol, caplog: pytest.LogCaptureFixture
1722 ):
1723 """Test we can set white temp on a 0x35."""
1724 light = AIOWifiLedBulb("192.168.1.166")
1725
1726 def _updated_callback(*args, **kwargs):
1727 pass
1728
1729 task = asyncio.create_task(light.async_setup(_updated_callback))
1730 transport, protocol = await mock_aio_protocol()
1731 light._aio_protocol.data_received(
1732 b"\x81\x35\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xee"
1733 )
1734 await task
1735 assert light.model_num == 0x35
1736
1737 transport.reset_mock()
1738 await light.async_set_white_temp(6500, 255)
1739 assert transport.mock_calls[0][0] == "write"
1740 assert transport.mock_calls[0][1][0] == b"1\x00\x00\x00\x00\xff\x0f\x0fN"
1741
1742
1743 @pytest.mark.asyncio
1744 async def test_setup_0x35_with_ZJ21410(
1745 mock_aio_protocol, caplog: pytest.LogCaptureFixture
1746 ):
1747 """Test we can setup a 0x35 with the ZJ21410 module."""
1748 light = AIOWifiLedBulb("192.168.1.166")
1749
1750 def _updated_callback(*args, **kwargs):
1751 pass
1752
1753 task = asyncio.create_task(light.async_setup(_updated_callback))
1754 transport, protocol = await mock_aio_protocol()
1755 light._aio_protocol.data_received(
1756 b"\xb0\xb1\xb2\xb3\x00\x02\x01\x70\x00\x0e\x81\x35\x23\x61\x17\x04\xd3\xff\x49\x00\x09\x00\xf0\x69\x19"
1757 )
1758 await task
1759 assert light.model_num == 0x35
1760
1761
1762 @pytest.mark.asyncio
1763 async def test_setup_0x44_with_version_num_10(
1764 mock_aio_protocol, caplog: pytest.LogCaptureFixture
1765 ):
1766 """Test we use the right protocol for 044 with v10."""
1767 light = AIOWifiLedBulb("192.168.1.166")
1768
1769 def _updated_callback(*args, **kwargs):
1770 pass
1771
1772 task = asyncio.create_task(light.async_setup(_updated_callback))
1773 transport, protocol = await mock_aio_protocol()
1774 light._aio_protocol.data_received(
1775 b"\x81\x44\x24\x61\x01\x01\xff\x00\xff\x00\x0a\x00\xf0\x44"
1776 )
1777 await task
1778 assert light.model_num == 0x44
1779 assert light.protocol == PROTOCOL_LEDENET_8BYTE_AUTO_ON
1780
1781
1782 @pytest.mark.asyncio
1783 async def test_async_failed_callback(
1784 mock_aio_protocol, caplog: pytest.LogCaptureFixture
1785 ):
1786 """Test we log on failed callback."""
1787 light = AIOWifiLedBulb("192.168.1.166")
1788 caplog.set_level(logging.DEBUG)
1789
1790 def _updated_callback(*args, **kwargs):
1791 raise ValueError("something went wrong")
1792
1793 task = asyncio.create_task(light.async_setup(_updated_callback))
1794 transport, protocol = await mock_aio_protocol()
1795 light._aio_protocol.data_received(
1796 b"\x81\xa3#\x25\x01\x10\x64\x00\x00\x00\x04\x00\xf0\xd5"
1797 )
1798 # ic state
1799 light._aio_protocol.data_received(b"\x00\x63\x00\x19\x00\x02\x04\x03\x19\x02\xa0")
1800 await task
1801 assert light.model_num == 0xA3
1802 assert light.dimmable_effects is True
1803 assert light.requires_turn_on is False
1804 assert "something went wrong" in caplog.text
1805
1806
1807 @pytest.mark.asyncio
1808 async def test_async_set_custom_effect(
1809 mock_aio_protocol, caplog: pytest.LogCaptureFixture
1810 ):
1811 """Test we can set a custom effect."""
1812 light = AIOWifiLedBulb("192.168.1.166")
1813
1814 def _updated_callback(*args, **kwargs):
1815 pass
1816
1817 task = asyncio.create_task(light.async_setup(_updated_callback))
1818 transport, protocol = await mock_aio_protocol()
1819 light._aio_protocol.data_received(
1820 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
1821 )
1822 await task
1823 assert light.model_num == 0x25
1824
1825 transport.reset_mock()
1826
1827 # no values
1828 with pytest.raises(ValueError):
1829 await light.async_set_custom_pattern([], 50, "jump")
1830
1831 await light.async_set_custom_pattern(
1832 [
1833 (255, 0, 0),
1834 (255, 0, 0),
1835 (255, 0, 0),
1836 (255, 0, 0),
1837 (255, 0, 0),
1838 (255, 0, 0),
1839 (255, 0, 0),
1840 (255, 0, 0),
1841 (255, 0, 0),
1842 (255, 0, 0),
1843 (255, 0, 0),
1844 (255, 0, 0),
1845 (255, 0, 0),
1846 (255, 0, 0),
1847 (255, 0, 255),
1848 (255, 0, 0),
1849 (255, 0, 0),
1850 ],
1851 50,
1852 "jump",
1853 )
1854 assert transport.mock_calls[0][0] == "write"
1855 assert (
1856 transport.mock_calls[0][1][0]
1857 == b"Q\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\x00\x00\xff\x00\xff\x00\xff\x00\x00\x00\x10;\xff\x0f\x99"
1858 )
1859
1860
1861 @pytest.mark.asyncio
1862 async def test_async_stop(mock_aio_protocol):
1863 """Test we can stop without throwing."""
1864 light = AIOWifiLedBulb("192.168.1.166")
1865
1866 def _updated_callback(*args, **kwargs):
1867 pass
1868
1869 task = asyncio.create_task(light.async_setup(_updated_callback))
1870 transport, protocol = await mock_aio_protocol()
1871 light._aio_protocol.data_received(
1872 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
1873 )
1874 await task
1875
1876 await light.async_stop()
1877 await asyncio.sleep(0) # make sure nothing throws
1878
1879
1880 @pytest.mark.asyncio
1881 async def test_async_set_brightness_rgbww(mock_aio_protocol):
1882 """Test we can set brightness rgbww."""
1883 light = AIOWifiLedBulb("192.168.1.166")
1884
1885 def _updated_callback(*args, **kwargs):
1886 pass
1887
1888 task = asyncio.create_task(light.async_setup(_updated_callback))
1889 transport, protocol = await mock_aio_protocol()
1890 light._aio_protocol.data_received(
1891 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
1892 )
1893 await task
1894
1895 transport.reset_mock()
1896 await light.async_set_brightness(255)
1897 assert transport.mock_calls[0][0] == "write"
1898 assert transport.mock_calls[0][1][0] == b"1\xff\x00\xd5\xff\xff\x00\x0f\x12"
1899
1900 transport.reset_mock()
1901 await light.async_set_brightness(128)
1902 assert transport.mock_calls[0][0] == "write"
1903 assert transport.mock_calls[0][1][0] == b"1\x80\x00k\x80\x80\x00\x0f+"
1904
1905
1906 @pytest.mark.asyncio
1907 async def test_async_set_brightness_cct_0x25(mock_aio_protocol):
1908 """Test we can set brightness with a 0x25 cct device."""
1909 light = AIOWifiLedBulb("192.168.1.166")
1910
1911 def _updated_callback(*args, **kwargs):
1912 pass
1913
1914 task = asyncio.create_task(light.async_setup(_updated_callback))
1915 transport, protocol = await mock_aio_protocol()
1916 light._aio_protocol.data_received(
1917 b"\x81\x25\x23\x61\x02\x10\xb6\x00\x98\x19\x04\x25\x0f\xdb"
1918 )
1919 await task
1920
1921 transport.reset_mock()
1922 await light.async_set_brightness(255)
1923 assert transport.mock_calls[0][0] == "write"
1924 assert transport.mock_calls[0][1][0] == b"1\x00\x00\x00g\x98\x00\x0f?"
1925 assert light.brightness == 255
1926
1927 transport.reset_mock()
1928 await light.async_set_brightness(128)
1929 assert transport.mock_calls[0][0] == "write"
1930 assert transport.mock_calls[0][1][0] == b"1\x00\x00\x004L\x00\x0f\xc0"
1931 assert light.brightness == 128
1932
1933
1934 @pytest.mark.asyncio
1935 async def test_async_set_brightness_cct_0x07(mock_aio_protocol):
1936 """Test we can set brightness with a 0x07 cct device."""
1937 light = AIOWifiLedBulb("192.168.1.166")
1938
1939 def _updated_callback(*args, **kwargs):
1940 pass
1941
1942 task = asyncio.create_task(light.async_setup(_updated_callback))
1943 transport, protocol = await mock_aio_protocol()
1944 light._aio_protocol.data_received(
1945 b"\x81\x07\x24\x61\xc7\x01\x00\x00\x00\x00\x02\xff\x0f\xe5"
1946 )
1947 await task
1948
1949 transport.reset_mock()
1950 await light.async_set_brightness(255)
1951 assert transport.mock_calls[0][0] == "write"
1952 assert transport.mock_calls[0][1][0] == b"1\x00\x00\x00\x00\xff\x0f\x0fN"
1953 assert light.brightness == 255
1954
1955 transport.reset_mock()
1956 await light.async_set_brightness(128)
1957 assert transport.mock_calls[0][0] == "write"
1958 assert transport.mock_calls[0][1][0] == b"1\x00\x00\x00\x00\x80\x0f\x0f\xcf"
1959 assert light.brightness == 128
1960
1961
1962 @pytest.mark.asyncio
1963 async def test_async_set_brightness_dim(mock_aio_protocol):
1964 """Test we can set brightness with a dim only device."""
1965 light = AIOWifiLedBulb("192.168.1.166")
1966
1967 def _updated_callback(*args, **kwargs):
1968 pass
1969
1970 task = asyncio.create_task(light.async_setup(_updated_callback))
1971 transport, protocol = await mock_aio_protocol()
1972 light._aio_protocol.data_received(
1973 b"\x81\x25\x23\x61\x01\x10\xb6\x00\x98\x19\x04\x25\x0f\xda"
1974 )
1975 await task
1976
1977 transport.reset_mock()
1978 await light.async_set_brightness(255)
1979 assert transport.mock_calls[0][0] == "write"
1980 assert transport.mock_calls[0][1][0] == b"1\x00\x00\x00\xff\xff\x00\x0f>"
1981 assert light.brightness == 255
1982
1983 transport.reset_mock()
1984 await light.async_set_brightness(128)
1985 assert transport.mock_calls[0][0] == "write"
1986 assert transport.mock_calls[0][1][0] == b"1\x00\x00\x00\x80\x80\x00\x0f@"
1987 assert light.brightness == 128
1988
1989
1990 @pytest.mark.asyncio
1991 async def test_async_set_brightness_rgb_0x33(mock_aio_protocol):
1992 """Test we can set brightness with a rgb only device."""
1993 light = AIOWifiLedBulb("192.168.1.166")
1994
1995 def _updated_callback(*args, **kwargs):
1996 pass
1997
1998 task = asyncio.create_task(light.async_setup(_updated_callback))
1999 transport, protocol = await mock_aio_protocol()
2000 light._aio_protocol.data_received(
2001 b"\x81\x33\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xec"
2002 )
2003 await task
2004
2005 transport.reset_mock()
2006 await light.async_set_brightness(255)
2007 assert transport.mock_calls[0][0] == "write"
2008 assert transport.mock_calls[0][1][0] == b"1\xff\x00\xd4\x00\x00\x0f\x13"
2009 assert light.brightness == 255
2010
2011 transport.reset_mock()
2012 await light.async_set_brightness(128)
2013 assert transport.mock_calls[0][0] == "write"
2014 assert transport.mock_calls[0][1][0] == b"1\x80\x00j\x00\x00\x0f*"
2015 assert light.brightness == 128
2016
2017
2018 @pytest.mark.asyncio
2019 async def test_async_set_brightness_rgb_0x25(mock_aio_protocol):
2020 """Test we can set brightness with a 0x25 device."""
2021 light = AIOWifiLedBulb("192.168.1.166")
2022
2023 def _updated_callback(*args, **kwargs):
2024 pass
2025
2026 task = asyncio.create_task(light.async_setup(_updated_callback))
2027 transport, protocol = await mock_aio_protocol()
2028 light._aio_protocol.data_received(
2029 b"\x81\x25\x23\x61\x03\x10\xb6\x00\x98\x19\x04\x25\x0f\xdc"
2030 )
2031 await task
2032
2033 transport.reset_mock()
2034 await light.async_set_brightness(255)
2035 assert transport.mock_calls[0][0] == "write"
2036 assert transport.mock_calls[0][1][0] == b"1\xff\x00\xd4\x00\x00\x00\x0f\x13"
2037 assert light.brightness == 255
2038
2039 transport.reset_mock()
2040 await light.async_set_brightness(128)
2041 assert transport.mock_calls[0][0] == "write"
2042 assert transport.mock_calls[0][1][0] == b"1\x80\x00j\x00\x00\x00\x0f*"
2043 assert light.brightness == 128
2044
2045
2046 @pytest.mark.asyncio
2047 async def test_async_set_brightness_rgbw(mock_aio_protocol):
2048 """Test we can set brightness with a rgbw only device."""
2049 light = AIOWifiLedBulb("192.168.1.166")
2050
2051 def _updated_callback(*args, **kwargs):
2052 pass
2053
2054 task = asyncio.create_task(light.async_setup(_updated_callback))
2055 transport, protocol = await mock_aio_protocol()
2056 light._aio_protocol.data_received(
2057 b"\x81\x25\x23\x61\x04\x10\xb6\x00\x98\x19\x04\x25\x0f\xdd"
2058 )
2059 await task
2060
2061 transport.reset_mock()
2062 await light.async_set_brightness(255)
2063 assert transport.mock_calls[0][0] == "write"
2064 assert transport.mock_calls[0][1][0] == b"1\xff\x00\xd5\xff\xff\x00\x0f\x12"
2065 assert light.brightness == 255
2066
2067 transport.reset_mock()
2068 await light.async_set_brightness(128)
2069 assert transport.mock_calls[0][0] == "write"
2070 assert transport.mock_calls[0][1][0] == b"1\x80\x00k\x80\x80\x00\x0f+"
2071 assert light.brightness == 128
2072
2073
2074 @pytest.mark.asyncio
2075 async def test_0x06_rgbw_cct_warm(mock_aio_protocol, caplog: pytest.LogCaptureFixture):
2076 """Test we can set CCT on RGBW with a warm strip."""
2077 light = AIOWifiLedBulb("192.168.1.166")
2078 assert light.white_channel_channel_type == WhiteChannelType.WARM
2079 light.white_channel_channel_type = WhiteChannelType.WARM
2080 assert light.white_channel_channel_type == WhiteChannelType.WARM
2081
2082 def _updated_callback(*args, **kwargs):
2083 pass
2084
2085 task = asyncio.create_task(light.async_setup(_updated_callback))
2086 transport, protocol = await mock_aio_protocol()
2087 light._aio_protocol.data_received(
2088 b"\x81\x06\x24\x61\x24\x01\x00\xff\x00\x00\x03\x00\xf0\x23"
2089 )
2090 await task
2091 assert light.model_num == 0x06
2092 assert light.operating_mode == "RGB&W"
2093 assert light.min_temp == MIN_TEMP
2094 assert light.max_temp == MAX_TEMP
2095 assert light.color_modes == {COLOR_MODE_RGBW, COLOR_MODE_CCT}
2096
2097 transport.reset_mock()
2098 await light.async_set_white_temp(light.max_temp, 255)
2099 assert transport.mock_calls[0][0] == "write"
2100 assert transport.mock_calls[0][1][0] == b"1\xff\xff\xff\x00\x00\x0f="
2101 assert light.brightness == 255
2102 assert light.raw_state.red == 255
2103 assert light.raw_state.green == 255
2104 assert light.raw_state.blue == 255
2105 assert light.raw_state.warm_white == 0
2106
2107 transport.reset_mock()
2108 await light.async_set_white_temp(light.min_temp, 255)
2109 assert transport.mock_calls[0][0] == "write"
2110 assert transport.mock_calls[0][1][0] == b"1\x00\x00\x00\xff\x00\x0f?"
2111 assert light.brightness == 255
2112 assert light.raw_state.red == 0
2113 assert light.raw_state.green == 0
2114 assert light.raw_state.blue == 0
2115 assert light.raw_state.warm_white == 255
2116
2117
2118 @pytest.mark.asyncio
2119 async def test_0x06_rgbw_cct_natural(
2120 mock_aio_protocol, caplog: pytest.LogCaptureFixture
2121 ):
2122 """Test we can set CCT on RGBW with a natural strip."""
2123 light = AIOWifiLedBulb("192.168.1.166")
2124 light.white_channel_channel_type = WhiteChannelType.NATURAL
2125 assert light.white_channel_channel_type == WhiteChannelType.NATURAL
2126
2127 def _updated_callback(*args, **kwargs):
2128 pass
2129
2130 task = asyncio.create_task(light.async_setup(_updated_callback))
2131 transport, protocol = await mock_aio_protocol()
2132 light._aio_protocol.data_received(
2133 b"\x81\x06\x24\x61\x24\x01\x00\xff\x00\x00\x03\x00\xf0\x23"
2134 )
2135 await task
2136 assert light.model_num == 0x06
2137 assert light.operating_mode == "RGB&W"
2138 assert light.color_modes == {COLOR_MODE_RGBW, COLOR_MODE_CCT}
2139 assert light.min_temp == MAX_TEMP - ((MAX_TEMP - MIN_TEMP) / 2)
2140 assert light.max_temp == MAX_TEMP
2141
2142 transport.reset_mock()
2143 await light.async_set_white_temp(light.max_temp, 255)
2144 assert transport.mock_calls[0][0] == "write"
2145 assert transport.mock_calls[0][1][0] == b"1\xff\xff\xff\x00\x00\x0f="
2146 assert light.brightness == 255
2147 assert light.raw_state.red == 255
2148 assert light.raw_state.blue == 255
2149 assert light.raw_state.green == 255
2150 assert light.raw_state.warm_white == 0
2151
2152 transport.reset_mock()
2153 await light.async_set_white_temp(light.min_temp, 255)
2154 assert transport.mock_calls[0][0] == "write"
2155 assert transport.mock_calls[0][1][0] == b"1\x00\x00\x00\xff\x00\x0f?"
2156 assert light.brightness == 255
2157 assert light.raw_state.red == 0
2158 assert light.raw_state.blue == 0
2159 assert light.raw_state.green == 0
2160 assert light.raw_state.warm_white == 255
2161
2162
2163 @pytest.mark.asyncio
2164 async def test_0x06_rgbw_cct_cold(mock_aio_protocol, caplog: pytest.LogCaptureFixture):
2165 """Test we can set CCT on RGBW with a cold strip."""
2166 light = AIOWifiLedBulb("192.168.1.166")
2167 light.white_channel_channel_type = WhiteChannelType.COLD
2168 assert light.white_channel_channel_type == WhiteChannelType.COLD
2169
2170 def _updated_callback(*args, **kwargs):
2171 pass
2172
2173 task = asyncio.create_task(light.async_setup(_updated_callback))
2174 transport, protocol = await mock_aio_protocol()
2175 light._aio_protocol.data_received(
2176 b"\x81\x06\x24\x61\x24\x01\x00\xff\x00\x00\x03\x00\xf0\x23"
2177 )
2178 await task
2179 assert light.model_num == 0x06
2180 assert light.operating_mode == "RGB&W"
2181 assert light.color_modes == {COLOR_MODE_RGBW}
2182 assert light.min_temp == MAX_TEMP
2183 assert light.max_temp == MAX_TEMP
2184
2185
2186 @pytest.mark.asyncio
2187 @pytest.mark.skipif(sys.version_info[:3][1] in (7,), reason="no AsyncMock in 3.7")
2188 async def test_wrapped_cct_protocol_device(mock_aio_protocol):
2189 """Test a wrapped cct protocol device."""
2190 light = AIOWifiLedBulb("192.168.1.166")
2191
2192 def _updated_callback(*args, **kwargs):
2193 pass
2194
2195 task = asyncio.create_task(light.async_setup(_updated_callback))
2196 transport, original_aio_protocol = await mock_aio_protocol()
2197 light._aio_protocol.data_received(
2198 b"\x81\x1c\x23\x61\x00\x05\x00\x64\x64\x64\x03\x64\x0f\xc8"
2199 )
2200 await task
2201 assert light.getCCT() == (0, 255)
2202 assert light.color_temp == 6500
2203 assert light.brightness == 255
2204 assert isinstance(light._protocol, ProtocolLEDENETCCTWrapped)
2205 assert light._protocol.timer_count == 6
2206 assert light._protocol.timer_len == 14
2207 assert light._protocol.timer_response_len == 88
2208 light._aio_protocol.data_received(
2209 b"\x81\x1c\x23\x61\x00\x05\x00\x00\x00\x00\x03\x64\x00\x8d"
2210 )
2211 assert light.getCCT() == (255, 0)
2212 assert light.color_temp == 2700
2213 assert light.brightness == 255
2214 assert light.dimmable_effects is False
2215 assert light.requires_turn_on is False
2216 assert light._protocol.power_push_updates is True
2217 assert light._protocol.state_push_updates is True
2218
2219 transport.reset_mock()
2220 await light.async_set_brightness(32)
2221 assert transport.mock_calls[0][0] == "write"
2222 assert (
2223 transport.mock_calls[0][1][0]
2224 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x00\x00\t5\xb1\x00\r\x00\x00\x00\x03\xf6\xbd"
2225 )
2226 assert light.brightness == 33
2227
2228 transport.reset_mock()
2229 await light.async_set_brightness(128)
2230 assert transport.mock_calls[0][0] == "write"
2231 assert (
2232 transport.mock_calls[0][1][0]
2233 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x01\x00\t5\xb1\x002\x00\x00\x00\x03\x1b\x08"
2234 )
2235 assert light.brightness == 128
2236
2237 transport.reset_mock()
2238 await light.async_set_brightness(1)
2239 assert transport.mock_calls[0][0] == "write"
2240 assert (
2241 transport.mock_calls[0][1][0]
2242 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x02\x00\t5\xb1\x00\x02\x00\x00\x00\x03\xeb\xa9"
2243 )
2244 assert light.brightness == 0
2245
2246 transport.reset_mock()
2247 await light.async_set_levels(w=0, w2=255)
2248 assert transport.mock_calls[0][0] == "write"
2249 assert (
2250 transport.mock_calls[0][1][0]
2251 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x03\x00\t5\xb1dd\x00\x00\x00\x03\xb16"
2252 )
2253 assert light.getCCT() == (0, 255)
2254 assert light.color_temp == 6500
2255 assert light.brightness == 255
2256
2257 transport.reset_mock()
2258 await light.async_set_effect("random", 50)
2259 assert transport.mock_calls[0][0] == "write"
2260 assert transport.mock_calls[0][1][0].startswith(b"\xb0\xb1\xb2\xb3\x00")
2261
2262 # light is on
2263 light._aio_protocol.data_received(
2264 b"\x81\x1c\x23\x61\x00\x05\x00\x64\x64\x64\x03\x64\x0f\xc8"
2265 )
2266 assert light._last_update_time == aiodevice.NEVER_TIME
2267 transport.reset_mock()
2268 await light.async_update()
2269 await light.async_update()
2270 await light.async_update()
2271 await light.async_update()
2272 await asyncio.sleep(0)
2273 assert len(transport.mock_calls) == 1
2274
2275 # light is off
2276 light._aio_protocol.data_received(
2277 b"\x81\x1c\x24\x61\x00\x05\x00\x64\x64\x64\x03\x64\x0f\xc9"
2278 )
2279 transport.reset_mock()
2280 await light.async_update()
2281 await light.async_update()
2282 await light.async_update()
2283 await light.async_update()
2284 await asyncio.sleep(0)
2285 assert len(transport.mock_calls) == 0
2286
2287 transport.reset_mock()
2288 for _ in range(4):
2289 light._last_update_time = aiodevice.NEVER_TIME
2290 await light.async_update()
2291 await asyncio.sleep(0)
2292 assert len(transport.mock_calls) == 4
2293
2294 light._last_update_time = aiodevice.NEVER_TIME
2295 for _ in range(4):
2296 # First failure should keep the device in
2297 # a failure state until we get to an update
2298 # time
2299 with (
2300 patch.object(
2301 light, "_async_connect", AsyncMock(side_effect=asyncio.TimeoutError)
2302 ),
2303 pytest.raises(DeviceUnavailableException),
2304 ):
2305 await light.async_update()
2306
2307 light._aio_protocol = original_aio_protocol
2308 # Should not raise now that bulb has recovered
2309 light._last_update_time = aiodevice.NEVER_TIME
2310 light._aio_protocol.data_received(
2311 b"\x81\x1c\x24\x61\x00\x05\x00\x64\x64\x64\x03\x64\x0f\xc9"
2312 )
2313 await light.async_update()
2314
2315
2316 @pytest.mark.asyncio
2317 @pytest.mark.skipif(sys.version_info[:3][1] in (7,), reason="no AsyncMock in 3.7")
2318 async def test_cct_protocol_device(mock_aio_protocol):
2319 """Test a original cct protocol device."""
2320 light = AIOWifiLedBulb("192.168.1.166")
2321
2322 def _updated_callback(*args, **kwargs):
2323 pass
2324
2325 task = asyncio.create_task(light.async_setup(_updated_callback))
2326 transport, original_aio_protocol = await mock_aio_protocol()
2327 light._aio_protocol.data_received(
2328 b"\x81\x09\x23\x61\x00\x05\x00\x64\x64\x64\x03\x64\x0f\xb5"
2329 )
2330 await task
2331 assert light.getCCT() == (0, 255)
2332 assert light.color_temp == 6500
2333 assert light.brightness == 255
2334 assert isinstance(light._protocol, ProtocolLEDENETCCT)
2335 assert light._protocol.timer_count == 6
2336 assert light._protocol.timer_len == 14
2337 assert light._protocol.timer_response_len == 88
2338 light._aio_protocol.data_received(
2339 b"\x81\x1c\x23\x61\x00\x05\x00\x00\x00\x00\x03\x64\x00\x8d"
2340 )
2341 assert light.getCCT() == (255, 0)
2342 assert light.color_temp == 2700
2343 assert light.brightness == 255
2344 assert light.dimmable_effects is False
2345 assert light.requires_turn_on is True
2346 assert light._protocol.power_push_updates is True
2347 assert light._protocol.state_push_updates is False
2348
2349 transport.reset_mock()
2350 await light.async_set_brightness(32)
2351 assert transport.mock_calls[0][0] == "write"
2352 assert transport.mock_calls[0][1][0] == b"5\xb1\x00\r\x00\x00\x00\x03\xf6"
2353 assert light.brightness == 33
2354
2355 transport.reset_mock()
2356 await light.async_set_brightness(128)
2357 assert transport.mock_calls[0][0] == "write"
2358 assert transport.mock_calls[0][1][0] == b"5\xb1\x002\x00\x00\x00\x03\x1b"
2359 assert light.brightness == 128
2360
2361 transport.reset_mock()
2362 await light.async_set_brightness(1)
2363 assert transport.mock_calls[0][0] == "write"
2364 assert transport.mock_calls[0][1][0] == b"5\xb1\x00\x02\x00\x00\x00\x03\xeb"
2365 assert light.brightness == 0
2366
2367 transport.reset_mock()
2368 await light.async_set_levels(w=0, w2=255)
2369 assert transport.mock_calls[0][0] == "write"
2370 assert transport.mock_calls[0][1][0] == b"5\xb1dd\x00\x00\x00\x03\xb1"
2371 assert light.getCCT() == (0, 255)
2372 assert light.color_temp == 6500
2373 assert light.brightness == 255
2374
2375 transport.reset_mock()
2376 await light.async_set_effect("random", 50)
2377 assert transport.mock_calls[0][0] == "write"
2378 assert transport.mock_calls[0][1][0].startswith(b"5\xb1")
2379
2380 # light is on
2381 light._aio_protocol.data_received(
2382 b"\x81\x1c\x23\x61\x00\x05\x00\x64\x64\x64\x03\x64\x0f\xc8"
2383 )
2384 assert light._last_update_time == aiodevice.NEVER_TIME
2385 transport.reset_mock()
2386 await light.async_update()
2387 await light.async_update()
2388 await light.async_update()
2389 await light.async_update()
2390 await asyncio.sleep(0)
2391 assert len(transport.mock_calls) == 1
2392
2393 # light is off
2394 light._aio_protocol.data_received(
2395 b"\x81\x1c\x24\x61\x00\x05\x00\x64\x64\x64\x03\x64\x0f\xc9"
2396 )
2397 transport.reset_mock()
2398 await light.async_update()
2399 await light.async_update()
2400 await light.async_update()
2401 await light.async_update()
2402 await asyncio.sleep(0)
2403 assert len(transport.mock_calls) == 0
2404
2405 transport.reset_mock()
2406 for _ in range(4):
2407 light._last_update_time = aiodevice.NEVER_TIME
2408 await light.async_update()
2409 await asyncio.sleep(0)
2410 assert len(transport.mock_calls) == 4
2411
2412 light._last_update_time = aiodevice.NEVER_TIME
2413 for _ in range(4):
2414 # First failure should keep the device in
2415 # a failure state until we get to an update
2416 # time
2417 with (
2418 patch.object(
2419 light, "_async_connect", AsyncMock(side_effect=asyncio.TimeoutError)
2420 ),
2421 pytest.raises(DeviceUnavailableException),
2422 ):
2423 await light.async_update()
2424
2425 light._aio_protocol = original_aio_protocol
2426
2427 # Should not raise now that bulb has recovered
2428 light._last_update_time = aiodevice.NEVER_TIME
2429 light._aio_protocol.data_received(
2430 b"\x81\x1c\x24\x61\x00\x05\x00\x64\x64\x64\x03\x64\x0f\xc9"
2431 )
2432 await light.async_update()
2433
2434
2435 @pytest.mark.asyncio
2436 async def test_christmas_protocol_device_turn_on(mock_aio_protocol):
2437 """Test a christmas protocol device."""
2438 light = AIOWifiLedBulb("192.168.1.166")
2439
2440 def _updated_callback(*args, **kwargs):
2441 pass
2442
2443 task = asyncio.create_task(light.async_setup(_updated_callback))
2444 transport, protocol = await mock_aio_protocol()
2445 light._aio_protocol.data_received(
2446 b"\x81\x1a\x23\x61\x00\x00\x00\xff\x00\x00\x01\x00\x06\x25"
2447 )
2448 await task
2449 assert light.rgb == (0, 255, 0)
2450 assert light.brightness == 255
2451 assert len(light.effect_list) == 101
2452 assert light.protocol == PROTOCOL_LEDENET_ADDRESSABLE_CHRISTMAS
2453 assert light.dimmable_effects is False
2454 assert light.requires_turn_on is False
2455 assert light._protocol.power_push_updates is True
2456 assert light._protocol.state_push_updates is True
2457
2458 data = []
2459 written = []
2460
2461 def _send_data(*args, **kwargs):
2462 written.append(args[0])
2463 light._aio_protocol.data_received(data.pop(0))
2464
2465 with (
2466 patch.object(aiodevice, "POWER_STATE_TIMEOUT", 0.010),
2467 patch.object(light._aio_protocol, "write", _send_data),
2468 ):
2469 data = [
2470 b"\x81\x1a\x23\x61\x00\x00\x00\xff\x00\x00\x01\x00\x06\x25",
2471 b"\x81\x25\x24\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xdf",
2472 ]
2473 await light.async_turn_off()
2474 await asyncio.sleep(0)
2475 assert light.is_on is False
2476 assert len(data) == 0
2477
2478 assert written == [
2479 b"\xb0\xb1\xb2\xb3\x00\x01\x01\x00\x00\x04q$\x0f\xa4\x14",
2480 b"\xb0\xb1\xb2\xb3\x00\x01\x01\x01\x00\x04\x81\x8a\x8b\x96\xf9",
2481 ]
2482
2483
2484 @pytest.mark.asyncio
2485 async def test_christmas_protocol_device(mock_aio_protocol):
2486 """Test a christmas protocol device."""
2487 light = AIOWifiLedBulb("192.168.1.166")
2488
2489 def _updated_callback(*args, **kwargs):
2490 pass
2491
2492 task = asyncio.create_task(light.async_setup(_updated_callback))
2493 transport, protocol = await mock_aio_protocol()
2494 light._aio_protocol.data_received(
2495 b"\x81\x1a\x23\x61\x00\x00\x00\xff\x00\x00\x01\x00\x06\x25"
2496 )
2497 await task
2498 assert light.rgb == (0, 255, 0)
2499 assert light.brightness == 255
2500 assert len(light.effect_list) == 101
2501 assert light.protocol == PROTOCOL_LEDENET_ADDRESSABLE_CHRISTMAS
2502 assert light.dimmable_effects is False
2503 assert light.requires_turn_on is False
2504 assert light._protocol.power_push_updates is True
2505 assert light._protocol.state_push_updates is True
2506
2507 transport.reset_mock()
2508 await light.async_set_brightness(255)
2509 assert transport.mock_calls[0][0] == "write"
2510 assert (
2511 transport.mock_calls[0][1][0]
2512 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x00\x00\x0d\x3b\xa1<dd\x00\x00\x00\x00\x00\x00\x00\xe0\x95"
2513 )
2514 assert light.brightness == 255
2515
2516 transport.reset_mock()
2517 await light.async_set_brightness(128)
2518 assert transport.mock_calls[0][0] == "write"
2519 assert (
2520 transport.mock_calls[0][1][0]
2521 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x01\x00\r;\xa1<d2\x00\x00\x00\x00\x00\x00\x00\xae2"
2522 )
2523 assert light.brightness == 128
2524
2525 transport.reset_mock()
2526 await light.async_set_levels(r=255, g=255, b=255)
2527 assert transport.mock_calls[0][0] == "write"
2528 assert (
2529 transport.mock_calls[0][1][0]
2530 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x02\x00\r;\xa1\x00\x00\x64\x00\x00\x00\x00\x00\x00\x00@W"
2531 )
2532 assert light.brightness == 255
2533
2534 transport.reset_mock()
2535 await light.async_set_effect("Twinkle Green", 50)
2536 assert transport.mock_calls[0][0] == "write"
2537 assert (
2538 transport.mock_calls[0][1][0]
2539 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x03\x00\x048\n\x10Rs"
2540 )
2541 light._transition_complete_time = 0
2542 light._aio_protocol.data_received(
2543 b"\x81\x1a\x23\x25\x0a\x00\x0f\x01\x00\x00\x01\x00\x06\x04"
2544 )
2545 assert light.effect == "Twinkle Green"
2546 assert light.speed == 100
2547
2548 transport.reset_mock()
2549 await light.async_set_effect("Strobe Red, Green", 100)
2550 assert transport.mock_calls[0][0] == "write"
2551 assert (
2552 transport.mock_calls[0][1][0]
2553 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x04\x00\x048=\x01v\xbc"
2554 )
2555
2556 light._transition_complete_time = 0
2557 light._aio_protocol.data_received(
2558 b"\x81\x1a\x23\x25\x3d\x00\x0f\x01\x00\x00\x01\x00\x06\x37"
2559 )
2560 assert light.effect == "Strobe Red, Green"
2561 assert light.speed == 100
2562
2563 with pytest.raises(ValueError):
2564 await light.async_set_preset_pattern(101, 50, 100)
2565
2566 light._transition_complete_time = 0
2567 light._aio_protocol.data_received(
2568 b"\x81\x1a\x23\x61\x07\x00\x66\x00\x66\x00\x01\x00\x06\xf9"
2569 )
2570 assert light.effect is None
2571 assert light.rgb == (102, 0, 102)
2572 assert light.speed == 100
2573
2574 transport.reset_mock()
2575 await light.async_set_zones([(255, 0, 0), (0, 0, 255)])
2576 assert transport.mock_calls[0][0] == "write"
2577 assert transport.mock_calls[0][1][0] == (
2578 b"\xb0\xb1\xb2\xb3\x00\x01\x01\x05\x004\xa0\x00\x06\x00\x01\xff"
2579 b"\x00\x00\x00\x00\xff\x00\x02\xff\x00\x00\x00\x00\xff\x00\x03\xff"
2580 b"\x00\x00\x00\x00\xff\x00\x04\x00\x00\xff\x00\x00\xff\x00\x05\x00"
2581 b"\x00\xff\x00\x00\xff\x00\x06\x00\x00\xff\x00\x00\xff\xaf_"
2582 )
2583
2584 transport.reset_mock()
2585 await light.async_set_zones(
2586 [(255, 0, 0), (0, 0, 255), (0, 255, 0), (255, 255, 255)]
2587 )
2588 assert transport.mock_calls[0][0] == "write"
2589 assert transport.mock_calls[0][1][0] == (
2590 b"\xb0\xb1\xb2\xb3\x00\x01\x01\x06\x004\xa0\x00\x06\x00\x01\xff"
2591 b"\x00\x00\x00\x00\xff\x00\x02\x00\x00\xff\x00\x00\xff\x00\x03\x00"
2592 b"\xff\x00\x00\x00\xff\x00\x04\xff\xff\xff\x00\x00\xff\x00\x05\xff"
2593 b"\xff\xff\x00\x00\xff\x00\x06\xff\xff\xff\x00\x00\xff\xa9T"
2594 )
2595
2596 with pytest.raises(ValueError):
2597 await light.async_set_zones(
2598 [
2599 (255, 0, 0),
2600 (0, 0, 255),
2601 (0, 255, 0),
2602 (255, 255, 255),
2603 (255, 255, 255),
2604 (255, 255, 255),
2605 (255, 255, 255),
2606 ]
2607 )
2608
2609
2610 @pytest.mark.asyncio
2611 async def test_async_get_time(mock_aio_protocol, caplog: pytest.LogCaptureFixture):
2612 """Test we can get the time."""
2613 light = AIOWifiLedBulb("192.168.1.166")
2614
2615 def _updated_callback(*args, **kwargs):
2616 pass
2617
2618 task = asyncio.create_task(light.async_setup(_updated_callback))
2619 transport, protocol = await mock_aio_protocol()
2620 light._aio_protocol.data_received(
2621 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
2622 )
2623 # ic state
2624 await task
2625 assert light.model_num == 0x25
2626 task = asyncio.ensure_future(light.async_get_time())
2627 await asyncio.sleep(0)
2628 # Invalid time
2629 light._aio_protocol.data_received(b"\x0f\x11\x14\x32\x01\x02\x106\x02\x07\x00\xac")
2630 light._aio_protocol.data_received(b"\x0f\x11\x14\x16\x01\x02\x106\x02\x07\x00\x9c")
2631 time = await task
2632 assert time == datetime.datetime(2022, 1, 2, 16, 54, 2)
2633 assert light._protocol.parse_get_time(b"\x0f") is None
2634
2635
2636 @pytest.mark.asyncio
2637 async def test_async_get_times_out(mock_aio_protocol, caplog: pytest.LogCaptureFixture):
2638 """Test we can get the time."""
2639 light = AIOWifiLedBulb("192.168.1.166", timeout=0.001)
2640
2641 def _updated_callback(*args, **kwargs):
2642 pass
2643
2644 task = asyncio.create_task(light.async_setup(_updated_callback))
2645 transport, protocol = await mock_aio_protocol()
2646 light._aio_protocol.data_received(
2647 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
2648 )
2649 # ic state
2650 await task
2651 assert light.model_num == 0x25
2652 task = asyncio.ensure_future(light.async_get_time())
2653 await asyncio.sleep(0)
2654 time = await task
2655 assert time is None
2656
2657
2658 @pytest.mark.asyncio
2659 async def test_async_set_time(mock_aio_protocol, caplog: pytest.LogCaptureFixture):
2660 """Test we can set the time."""
2661 light = AIOWifiLedBulb("192.168.1.166")
2662
2663 def _updated_callback(*args, **kwargs):
2664 pass
2665
2666 task = asyncio.create_task(light.async_setup(_updated_callback))
2667 transport, protocol = await mock_aio_protocol()
2668 light._aio_protocol.data_received(
2669 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
2670 )
2671 # ic state
2672 await task
2673 assert light.model_num == 0x25
2674
2675 transport.reset_mock()
2676 await light.async_set_time(datetime.datetime(2020, 1, 1, 1, 1, 1))
2677 assert transport.mock_calls[0][0] == "write"
2678 assert (
2679 transport.mock_calls[0][1][0]
2680 == b"\x10\x14\x14\x01\x01\x01\x01\x01\x03\x00\x0fO"
2681 )
2682
2683 transport.reset_mock()
2684 await light.async_set_time()
2685 assert transport.mock_calls[0][0] == "write"
2686 assert transport.mock_calls[0][1][0].startswith(b"\x10")
2687
2688
2689 @pytest.mark.asyncio
2690 async def test_async_set_time_legacy_device(
2691 mock_aio_protocol, caplog: pytest.LogCaptureFixture
2692 ):
2693 """Test we can set the time on a legacy device."""
2694 light = AIOWifiLedBulb("192.168.1.166")
2695 light.discovery = FLUX_DISCOVERY_LEGACY
2696
2697 def _updated_callback(*args, **kwargs):
2698 pass
2699
2700 task = asyncio.create_task(light.async_setup(_updated_callback))
2701 transport, protocol = await mock_aio_protocol()
2702 light._aio_protocol.data_received(b"f\x03$A!\x08\x01\x19P\x01\x99")
2703 # ic state
2704 await task
2705 assert light.model_num == 0x03
2706
2707 transport.reset_mock()
2708 await light.async_set_time(datetime.datetime(2020, 1, 1, 1, 1, 1))
2709 assert transport.mock_calls[0][0] == "write"
2710 assert (
2711 transport.mock_calls[0][1][0] == b"\x10\x14\x14\x01\x01\x01\x01\x01\x03\x00\x0f"
2712 )
2713
2714 transport.reset_mock()
2715 await light.async_set_time()
2716 assert transport.mock_calls[0][0] == "write"
2717 assert transport.mock_calls[0][1][0].startswith(b"\x10")
2718
2719
2720 @pytest.mark.asyncio
2721 async def test_async_get_timers_9byte_device(
2722 mock_aio_protocol, caplog: pytest.LogCaptureFixture
2723 ):
2724 """Test we can get the timers from a 9 byte device."""
2725 light = AIOWifiLedBulb("192.168.1.166")
2726
2727 def _updated_callback(*args, **kwargs):
2728 pass
2729
2730 task = asyncio.create_task(light.async_setup(_updated_callback))
2731 transport, protocol = await mock_aio_protocol()
2732 light._aio_protocol.data_received(
2733 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
2734 )
2735 await task
2736 assert light.model_num == 0x25
2737 task = asyncio.ensure_future(light.async_get_timers())
2738 await asyncio.sleep(0)
2739 light._aio_protocol.data_received(
2740 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"
2741 )
2742 timers = await task
2743 assert len(timers) == 6
2744 assert len(timers[0].toBytes()) == 15
2745 assert timers[0].toBytes() == b"\xf0\x16\x01\x04\x00+\x00\x00a\x19G\xff\x00\x00\xf0"
2746 assert str(timers[0]) == "[ON ] 00:43 Once: 2022-01-04 Color: (25, 71, 255)"
2747 assert (
2748 timers[1].toBytes() == b"\xf0\x16\x01\x04\x04,\x00\x00a\x7f\xff\x00\x00\x00\xf0"
2749 )
2750 assert str(timers[1]) == "[ON ] 04:44 Once: 2022-01-04 Color: chartreuse"
2751 assert (
2752 timers[2].toBytes()
2753 == b"\xf0\x16\x01\x03\x16\x1f\x00\x00a\xff\x00\x00\x00\x00\xf0"
2754 )
2755 assert str(timers[2]) == "[ON ] 22:31 Once: 2022-01-03 Color: red"
2756 assert (
2757 timers[3].toBytes()
2758 == b"\xf0\x16\x01\x03\x17\x13\x00\x00a\x81\x81\x81\x00\x00\xf0"
2759 )
2760 assert str(timers[3]) == "[ON ] 23:19 Once: 2022-01-03 Color: (129, 129, 129)"
2761 assert (
2762 timers[4].toBytes() == b"\xf0\x16\x01\x03\x17(\x00\x00a\x00\xff\x00\x00\x00\xf0"
2763 )
2764 assert str(timers[4]) == "[ON ] 23:40 Once: 2022-01-03 Color: lime"
2765 assert timers[5].toBytes() == b"\xf0\x16\x01\x04\x07,\x00\x00a!\x00\xff\x00\x00\xf0"
2766 assert str(timers[5]) == "[ON ] 07:44 Once: 2022-01-04 Color: (33, 0, 255)"
2767
2768 with pytest.raises(ValueError):
2769 light._protocol.parse_get_timers(b"\x0f")
2770
2771
2772 @pytest.mark.asyncio
2773 async def test_async_get_timers_socket_device(
2774 mock_aio_protocol, caplog: pytest.LogCaptureFixture
2775 ):
2776 """Test we can get the timers."""
2777 light = AIOWifiLedBulb("192.168.1.166")
2778
2779 def _updated_callback(*args, **kwargs):
2780 pass
2781
2782 task = asyncio.create_task(light.async_setup(_updated_callback))
2783 transport, protocol = await mock_aio_protocol()
2784 light._aio_protocol.data_received(
2785 b"\x81\x97\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\x50"
2786 )
2787 light._aio_protocol.data_received(b"\xf0\x32\xf0\xf0\xf0\xf0\xe2")
2788
2789 await task
2790 assert light.model_num == 0x97
2791 task = asyncio.ensure_future(light.async_get_timers())
2792 await asyncio.sleep(0)
2793 light._aio_protocol.data_received(
2794 b"\x0f\x22\xf0\x00\x00\x00\x11\x2f\x00\xfe\x23\x00\x00\x00\xf0\x00\x00\x00\x11\x30\x00\xfe\x23\x00\x00\x00\xf0\x00\x00\x00\x11\x30\x00\xfe\x24\x00\x00\x00\xf0\x00\x00\x00\x11\x30\x00\xfe\x23\x00\x00\x00\xf0\x00\x00\x00\x11\x30\x00\xfe\x23\x00\x00\x00\xf0\x00\x00\x00\x11\x30\x00\xfe\x23\x00\x00\x00\xf0\x00\x00\x00\x11\x31\x00\xfe\x24\x00\x00\x00\xf0\x00\x00\x00\x11\x31\x00\xfe\x23\x00\x00\x00\x00\xc4"
2795 )
2796 timers = await task
2797 assert len(timers) == 8
2798 assert len(timers[0].toBytes()) == 12
2799 assert timers[0].toBytes() == b"\xf0\x00\x00\x00\x11/\x00\xfe\x00\x00\x00\x00"
2800 assert str(timers[0]) == "[ON ] 17:47 SuMoTuWeThFrSa "
2801 assert str(timers[1]) == "[ON ] 17:48 SuMoTuWeThFrSa "
2802 assert str(timers[2]) == "[OFF] 17:48 SuMoTuWeThFrSa "
2803 assert str(timers[3]) == "[ON ] 17:48 SuMoTuWeThFrSa "
2804
2805
2806 @pytest.mark.asyncio
2807 async def test_sockets_push_updates(
2808 mock_aio_protocol, caplog: pytest.LogCaptureFixture
2809 ):
2810 """Test we can get the timers."""
2811 socket = AIOWifiLedBulb("192.168.1.166")
2812
2813 def _updated_callback(*args, **kwargs):
2814 pass
2815
2816 task = asyncio.create_task(socket.async_setup(_updated_callback))
2817 transport, protocol = await mock_aio_protocol()
2818 socket._aio_protocol.data_received(
2819 b"\x81\x97\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\x50"
2820 )
2821 socket._aio_protocol.data_received(b"\xf0\x32\xf0\xf0\xf0\xf0\xe2")
2822
2823 await task
2824 assert socket.model_num == 0x97
2825 assert socket._protocol.power_push_updates is True
2826 assert socket._protocol.state_push_updates is True
2827
2828
2829 @pytest.mark.asyncio
2830 async def test_async_get_timers_8_byte_device(
2831 mock_aio_protocol, caplog: pytest.LogCaptureFixture
2832 ):
2833 """Test we can get the timers from an 8 byte device."""
2834 light = AIOWifiLedBulb("192.168.1.166")
2835
2836 def _updated_callback(*args, **kwargs):
2837 pass
2838
2839 task = asyncio.create_task(light.async_setup(_updated_callback))
2840 transport, protocol = await mock_aio_protocol()
2841 light._aio_protocol.data_received(
2842 b"\x81\x33\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xec"
2843 )
2844
2845 await task
2846 assert light.model_num == 0x33
2847 task = asyncio.ensure_future(light.async_get_timers())
2848 await asyncio.sleep(0)
2849 light._aio_protocol.data_received(
2850 b"\x0f\x22\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\xdd"
2851 )
2852 timers = await task
2853 assert len(timers) == 6
2854 assert len(timers[0].toBytes()) == 14
2855 assert (
2856 timers[0].toBytes()
2857 == b"\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
2858 )
2859 assert str(timers[0]) == "Unset"
2860
2861
2862 @pytest.mark.asyncio
2863 async def test_async_get_timers_times_out(
2864 mock_aio_protocol, caplog: pytest.LogCaptureFixture
2865 ):
2866 """Test getting timers times out."""
2867 light = AIOWifiLedBulb("192.168.1.166", timeout=0.001)
2868
2869 def _updated_callback(*args, **kwargs):
2870 pass
2871
2872 task = asyncio.create_task(light.async_setup(_updated_callback))
2873 transport, protocol = await mock_aio_protocol()
2874 light._aio_protocol.data_received(
2875 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
2876 )
2877 # ic state
2878 await task
2879 assert light.model_num == 0x25
2880 task = asyncio.ensure_future(light.async_get_timers())
2881 await asyncio.sleep(0)
2882 time = await task
2883 assert time is None
2884
2885
2886 @pytest.mark.asyncio
2887 async def test_async_set_timers(mock_aio_protocol, caplog: pytest.LogCaptureFixture):
2888 """Test we can set timers."""
2889 light = AIOWifiLedBulb("192.168.1.166")
2890
2891 def _updated_callback(*args, **kwargs):
2892 pass
2893
2894 task = asyncio.create_task(light.async_setup(_updated_callback))
2895 transport, protocol = await mock_aio_protocol()
2896 light._aio_protocol.data_received(
2897 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
2898 )
2899 await task
2900 assert light.model_num == 0x25
2901
2902 transport.reset_mock()
2903 await light.async_set_timers(
2904 [LedTimer(b"\xf0\x00\x00\x00\x0c-\x00>a\x00\x80\x00\x00\xf0") for _ in range(6)]
2905 )
2906 assert transport.mock_calls[0][0] == "write"
2907 assert (
2908 transport.mock_calls[0][1][0]
2909 == b"!\xf0\x00\x00\x00\x0c-\x00>a\x00\x80\x00\x00\x00\xf0\xf0\x00\x00\x00\x0c-\x00>a\x00\x80\x00\x00\x00\xf0\xf0\x00\x00\x00\x0c-\x00>a\x00\x80\x00\x00\x00\xf0\xf0\x00\x00\x00\x0c-\x00>a\x00\x80\x00\x00\x00\xf0\xf0\x00\x00\x00\x0c-\x00>a\x00\x80\x00\x00\x00\xf0\xf0\x00\x00\x00\x0c-\x00>a\x00\x80\x00\x00\x00\xf0\x00\xf0a"
2910 )
2911
2912 caplog.clear()
2913 transport.reset_mock()
2914 await light.async_set_timers(
2915 [LedTimer(b"\xf0\x00\x00\x00\x0c-\x00>a\x00\x80\x00\x00\xf0") for _ in range(7)]
2916 )
2917 assert transport.mock_calls[0][0] == "write"
2918 assert (
2919 transport.mock_calls[0][1][0]
2920 == b"!\xf0\x00\x00\x00\x0c-\x00>a\x00\x80\x00\x00\x00\xf0\xf0\x00\x00\x00\x0c-\x00>a\x00\x80\x00\x00\x00\xf0\xf0\x00\x00\x00\x0c-\x00>a\x00\x80\x00\x00\x00\xf0\xf0\x00\x00\x00\x0c-\x00>a\x00\x80\x00\x00\x00\xf0\xf0\x00\x00\x00\x0c-\x00>a\x00\x80\x00\x00\x00\xf0\xf0\x00\x00\x00\x0c-\x00>a\x00\x80\x00\x00\x00\xf0\x00\xf0a"
2921 )
2922 assert "too many timers, truncating list" in caplog.text
2923
2924 transport.reset_mock()
2925 await light.async_set_timers(
2926 [LedTimer(b"\xf0\x00\x00\x00\x0c-\x00>a\x00\x80\x00\x00\xf0") for _ in range(2)]
2927 )
2928 assert transport.mock_calls[0][0] == "write"
2929 assert (
2930 transport.mock_calls[0][1][0]
2931 == b"!\xf0\x00\x00\x00\x0c-\x00>a\x00\x80\x00\x00\x00\xf0\xf0\x00\x00\x00\x0c-\x00>a\x00\x80\x00\x00\x00\xf0\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0\xbd"
2932 )
2933
2934
2935 @pytest.mark.asyncio
2936 async def test_async_enable_remote_access(mock_aio_protocol):
2937 """Test we can enable remote access."""
2938 light = AIOWifiLedBulb("192.168.1.166")
2939
2940 def _updated_callback(*args, **kwargs):
2941 pass
2942
2943 task = asyncio.create_task(light.async_setup(_updated_callback))
2944 transport, protocol = await mock_aio_protocol()
2945 light._aio_protocol.data_received(
2946 b"\x81\x25\x23\x61\x04\x10\xb6\x00\x98\x19\x04\x25\x0f\xdd"
2947 )
2948 await task
2949
2950 with patch(
2951 "flux_led.aiodevice.AIOBulbScanner.async_enable_remote_access",
2952 return_value=mock_coro(True),
2953 ) as mock_async_enable_remote_access:
2954 await light.async_enable_remote_access("host", 1234)
2955
2956 assert mock_async_enable_remote_access.mock_calls == [
2957 call("192.168.1.166", "host", 1234)
2958 ]
2959
2960
2961 @pytest.mark.asyncio
2962 async def test_async_disable_remote_access(mock_aio_protocol):
2963 """Test we can disable remote access."""
2964 light = AIOWifiLedBulb("192.168.1.166")
2965
2966 def _updated_callback(*args, **kwargs):
2967 pass
2968
2969 task = asyncio.create_task(light.async_setup(_updated_callback))
2970 transport, protocol = await mock_aio_protocol()
2971 light._aio_protocol.data_received(
2972 b"\x81\x25\x23\x61\x04\x10\xb6\x00\x98\x19\x04\x25\x0f\xdd"
2973 )
2974 await task
2975
2976 with patch(
2977 "flux_led.aiodevice.AIOBulbScanner.async_disable_remote_access",
2978 return_value=mock_coro(True),
2979 ) as mock_async_disable_remote_access:
2980 await light.async_disable_remote_access()
2981
2982 assert mock_async_disable_remote_access.mock_calls == [call("192.168.1.166")]
2983
2984
2985 @pytest.mark.asyncio
2986 async def test_async_reboot(mock_aio_protocol):
2987 """Test we can reboot."""
2988 light = AIOWifiLedBulb("192.168.1.166")
2989
2990 def _updated_callback(*args, **kwargs):
2991 pass
2992
2993 task = asyncio.create_task(light.async_setup(_updated_callback))
2994 transport, protocol = await mock_aio_protocol()
2995 light._aio_protocol.data_received(
2996 b"\x81\x25\x23\x61\x04\x10\xb6\x00\x98\x19\x04\x25\x0f\xdd"
2997 )
2998 await task
2999
3000 with patch(
3001 "flux_led.aiodevice.AIOBulbScanner.async_reboot",
3002 return_value=mock_coro(True),
3003 ) as mock_async_reboot:
3004 await light.async_reboot()
3005
3006 assert mock_async_reboot.mock_calls == [call("192.168.1.166")]
3007
3008
3009 @pytest.mark.asyncio
3010 async def test_power_state_response_processing(
3011 mock_aio_protocol, caplog: pytest.LogCaptureFixture
3012 ):
3013 """Test we can turn on and off via power state message."""
3014 light = AIOWifiLedBulb("192.168.1.166")
3015
3016 def _updated_callback(*args, **kwargs):
3017 pass
3018
3019 task = asyncio.create_task(light.async_setup(_updated_callback))
3020 await mock_aio_protocol()
3021 light._aio_protocol.data_received(
3022 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
3023 )
3024 await task
3025 light._aio_protocol.data_received(b"\xf0\x32\xf0\xf0\xf0\xf0\xe2")
3026 assert light.power_restore_states == PowerRestoreStates(
3027 channel1=PowerRestoreState.LAST_STATE,
3028 channel2=PowerRestoreState.LAST_STATE,
3029 channel3=PowerRestoreState.LAST_STATE,
3030 channel4=PowerRestoreState.LAST_STATE,
3031 )
3032 light._aio_protocol.data_received(b"\xf0\x32\x0f\xf0\xf0\xf0\x01")
3033 assert light.power_restore_states == PowerRestoreStates(
3034 channel1=PowerRestoreState.ALWAYS_ON,
3035 channel2=PowerRestoreState.LAST_STATE,
3036 channel3=PowerRestoreState.LAST_STATE,
3037 channel4=PowerRestoreState.LAST_STATE,
3038 )
3039 light._aio_protocol.data_received(b"\xf0\x32\xff\xf0\xf0\xf0\xf1")
3040 assert light.power_restore_states == PowerRestoreStates(
3041 channel1=PowerRestoreState.ALWAYS_OFF,
3042 channel2=PowerRestoreState.LAST_STATE,
3043 channel3=PowerRestoreState.LAST_STATE,
3044 channel4=PowerRestoreState.LAST_STATE,
3045 )
3046
3047
3048 @pytest.mark.asyncio
3049 async def test_async_set_power_restore_state(
3050 mock_aio_protocol, caplog: pytest.LogCaptureFixture
3051 ):
3052 """Test we can set power restore state and report it."""
3053 socket = AIOWifiLedBulb("192.168.1.166")
3054
3055 def _updated_callback(*args, **kwargs):
3056 pass
3057
3058 task = asyncio.create_task(socket.async_setup(_updated_callback))
3059 transport, protocol = await mock_aio_protocol()
3060 socket._aio_protocol.data_received(
3061 b"\x81\x97\x24\x24\x00\x00\x00\x00\x00\x00\x02\x00\x00\x62"
3062 )
3063 # power restore state
3064 socket._aio_protocol.data_received(b"\x0f\x32\xf0\xf0\xf0\xf0\x01")
3065 await task
3066 assert socket.model_num == 0x97
3067 assert socket.power_restore_states == PowerRestoreStates(
3068 channel1=PowerRestoreState.LAST_STATE,
3069 channel2=PowerRestoreState.LAST_STATE,
3070 channel3=PowerRestoreState.LAST_STATE,
3071 channel4=PowerRestoreState.LAST_STATE,
3072 )
3073
3074 transport.reset_mock()
3075 await socket.async_set_power_restore(
3076 channel1=PowerRestoreState.ALWAYS_ON,
3077 channel2=PowerRestoreState.ALWAYS_ON,
3078 channel3=PowerRestoreState.ALWAYS_ON,
3079 channel4=PowerRestoreState.ALWAYS_ON,
3080 )
3081 assert transport.mock_calls[0][0] == "write"
3082 assert transport.mock_calls[0][1][0] == b"1\x0f\x0f\x0f\x0f\xf0]"
3083
3084
3085 @pytest.mark.asyncio
3086 async def test_async_set_power_restore_state_fails(
3087 mock_aio_protocol, caplog: pytest.LogCaptureFixture
3088 ):
3089 """Test we raise if we do not get a power restore state."""
3090 socket = AIOWifiLedBulb("192.168.1.166", timeout=0.01)
3091
3092 def _updated_callback(*args, **kwargs):
3093 pass
3094
3095 task = asyncio.create_task(socket.async_setup(_updated_callback))
3096 transport, protocol = await mock_aio_protocol()
3097 socket._aio_protocol.data_received(
3098 b"\x81\x97\x24\x24\x00\x00\x00\x00\x00\x00\x02\x00\x00\x62"
3099 )
3100 # power restore state not sent
3101 with pytest.raises(RuntimeError):
3102 await task
3103
3104
3105 @pytest.mark.asyncio
3106 async def test_remote_config_queried(
3107 mock_aio_protocol, caplog: pytest.LogCaptureFixture
3108 ):
3109 """Test power state is queried if discovery shows a compatible remote."""
3110 light = AIOWifiLedBulb("192.168.1.166")
3111 light.discovery = FLUX_DISCOVERY_24G_REMOTE
3112
3113 def _updated_callback(*args, **kwargs):
3114 pass
3115
3116 with patch.object(aiodevice, "DEVICE_CONFIG_WAIT_SECONDS", 0):
3117 task = asyncio.create_task(light.async_setup(_updated_callback))
3118 transport, protocol = await mock_aio_protocol()
3119 light._aio_protocol.data_received(
3120 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
3121 )
3122 light._aio_protocol.data_received(
3123 b"\xb0\xb1\xb2\xb3\x00\x01\x01\x5e\x00\x0e\x2b\x01\x00\x00\x00\x00\x29\x00\x00\x00\x00\x00\x00\x55\xde"
3124 )
3125 await task
3126
3127 assert light.remote_config == RemoteConfig.DISABLED
3128 assert light.paired_remotes == 0
3129 assert transport.mock_calls == [
3130 call.get_extra_info("peername"),
3131 call.write(bytearray(b"\x81\x8a\x8b\x96")),
3132 call.write(
3133 bytearray(b"\xb0\xb1\xb2\xb3\x00\x01\x01\x00\x00\x04+,-\x84\xd4")
3134 ),
3135 ]
3136
3137
3138 @pytest.mark.asyncio
3139 async def test_remote_config_response_processing(
3140 mock_aio_protocol, caplog: pytest.LogCaptureFixture
3141 ):
3142 """Test we can turn on and off via power state message."""
3143 light = AIOWifiLedBulb("192.168.1.166")
3144 light.discovery = FLUX_DISCOVERY_24G_REMOTE
3145
3146 def _updated_callback(*args, **kwargs):
3147 pass
3148
3149 with patch.object(aiodevice, "DEVICE_CONFIG_WAIT_SECONDS", 0):
3150 task = asyncio.create_task(light.async_setup(_updated_callback))
3151 await mock_aio_protocol()
3152 light._aio_protocol.data_received(
3153 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
3154 )
3155 light._aio_protocol.data_received(
3156 b"\xb0\xb1\xb2\xb3\x00\x01\x01\x5e\x00\x0e\x2b\x01\x00\x00\x00\x00\x29\x00\x00\x00\x00\x00\x00\x55\xde"
3157 )
3158
3159 await task
3160 light._aio_protocol.data_received(
3161 b"\xb0\xb1\xb2\xb3\x00\x01\x01\x5e\x00\x0e\x2b\x01\x00\x00\x00\x00\x29\x00\x00\x00\x00\x00\x00\x55\xde"
3162 )
3163 assert light.remote_config == RemoteConfig.DISABLED
3164 assert light.paired_remotes == 0
3165
3166 light._aio_protocol.data_received(
3167 b"\xb0\xb1\xb2\xb3\x00\x01\x01\x45\x00\x0e\x2b\x02\x00\x00\x00\x00\x29\x00\x00\x00\x00\x00\x00\x56\xc7"
3168 )
3169 assert light.remote_config == RemoteConfig.OPEN
3170 assert light.paired_remotes == 0
3171
3172 light._aio_protocol.data_received(
3173 b"\xb0\xb1\xb2\xb3\x00\x01\x01\xe3\x00\x0e\x2b\x03\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x30\x19"
3174 )
3175 assert light.remote_config == RemoteConfig.PAIRED_ONLY
3176 assert light.paired_remotes == 2
3177
3178
3179 @pytest.mark.asyncio
3180 async def test_async_config_remotes(
3181 mock_aio_protocol, caplog: pytest.LogCaptureFixture
3182 ):
3183 """Test we can configure remotes."""
3184 light = AIOWifiLedBulb("192.168.1.166")
3185 light.discovery = FLUX_DISCOVERY_24G_REMOTE
3186
3187 def _updated_callback(*args, **kwargs):
3188 pass
3189
3190 with patch.object(aiodevice, "DEVICE_CONFIG_WAIT_SECONDS", 0):
3191 task = asyncio.create_task(light.async_setup(_updated_callback))
3192 transport, protocol = await mock_aio_protocol()
3193 light._aio_protocol.data_received(
3194 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
3195 )
3196 light._aio_protocol.data_received(
3197 b"\xb0\xb1\xb2\xb3\x00\x01\x01\x5e\x00\x0e\x2b\x01\x00\x00\x00\x00\x29\x00\x00\x00\x00\x00\x00\x55\xde"
3198 )
3199
3200 await task
3201 light._aio_protocol.data_received(
3202 b"\xb0\xb1\xb2\xb3\x00\x01\x01\xe3\x00\x0e\x2b\x03\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x30\x19"
3203 )
3204 assert light.remote_config == RemoteConfig.PAIRED_ONLY
3205 assert light.paired_remotes == 2
3206
3207 transport.reset_mock()
3208 await light.async_config_remotes(RemoteConfig.DISABLED)
3209 assert transport.mock_calls[0][0] == "write"
3210 assert (
3211 transport.mock_calls[0][1][0]
3212 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x01\x00\x10*\x01\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x0f5C"
3213 )
3214
3215 transport.reset_mock()
3216 await light.async_config_remotes(RemoteConfig.OPEN)
3217 assert transport.mock_calls[0][0] == "write"
3218 assert (
3219 transport.mock_calls[0][1][0]
3220 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x03\x00\x10*\x02\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x0f6G"
3221 )
3222
3223 transport.reset_mock()
3224 await light.async_config_remotes(RemoteConfig.PAIRED_ONLY)
3225 assert transport.mock_calls[0][0] == "write"
3226 assert (
3227 transport.mock_calls[0][1][0]
3228 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x05\x00\x10*\x03\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x0f7K"
3229 )
3230
3231
3232 @pytest.mark.asyncio
3233 async def test_async_unpair_remotes(
3234 mock_aio_protocol, caplog: pytest.LogCaptureFixture
3235 ):
3236 """Test we can unpair remotes."""
3237 light = AIOWifiLedBulb("192.168.1.166")
3238 light.discovery = FLUX_DISCOVERY_24G_REMOTE
3239
3240 def _updated_callback(*args, **kwargs):
3241 pass
3242
3243 with patch.object(aiodevice, "DEVICE_CONFIG_WAIT_SECONDS", 0):
3244 task = asyncio.create_task(light.async_setup(_updated_callback))
3245 transport, protocol = await mock_aio_protocol()
3246 light._aio_protocol.data_received(
3247 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
3248 )
3249 light._aio_protocol.data_received(
3250 b"\xb0\xb1\xb2\xb3\x00\x01\x01\x5e\x00\x0e\x2b\x01\x00\x00\x00\x00\x29\x00\x00\x00\x00\x00\x00\x55\xde"
3251 )
3252
3253 await task
3254 light._aio_protocol.data_received(
3255 b"\xb0\xb1\xb2\xb3\x00\x01\x01\xe3\x00\x0e\x2b\x03\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x30\x19"
3256 )
3257 assert light.remote_config == RemoteConfig.PAIRED_ONLY
3258 assert light.paired_remotes == 2
3259
3260 transport.reset_mock()
3261 await light.async_unpair_remotes()
3262 assert transport.mock_calls[0][0] == "write"
3263 assert (
3264 transport.mock_calls[0][1][0]
3265 == b"\xb0\xb1\xb2\xb3\x00\x01\x01\x01\x00\x10*\xff\xff\x01\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\xf0\x16\x05"
3266 )
3267
3268
3269 @pytest.mark.asyncio
3270 async def test_async_config_remotes_unsupported_device(
3271 mock_aio_protocol, caplog: pytest.LogCaptureFixture
3272 ):
3273 """Test we can configure remotes."""
3274 light = AIOWifiLedBulb("192.168.1.166")
3275
3276 def _updated_callback(*args, **kwargs):
3277 pass
3278
3279 task = asyncio.create_task(light.async_setup(_updated_callback))
3280 transport, protocol = await mock_aio_protocol()
3281 light._aio_protocol.data_received(
3282 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
3283 )
3284 await task
3285 assert light.paired_remotes is None
3286
3287 with pytest.raises(ValueError):
3288 await light.async_config_remotes(RemoteConfig.PAIRED_ONLY)
3289
3290 with pytest.raises(ValueError):
3291 await light.async_unpair_remotes()
3292
3293
3294 @pytest.mark.asyncio
3295 @pytest.mark.skipif(sys.version_info[:3][1] in (7,), reason="no AsyncMock in 3.7")
3296 async def test_async_config_remotes_no_response(
3297 mock_aio_protocol, caplog: pytest.LogCaptureFixture
3298 ):
3299 """Test device supports remote config but does not respond."""
3300 light = AIOWifiLedBulb("192.168.1.166", timeout=0.001)
3301 light.discovery = FLUX_DISCOVERY_24G_REMOTE
3302
3303 def _updated_callback(*args, **kwargs):
3304 pass
3305
3306 task = asyncio.create_task(light.async_setup(_updated_callback))
3307 transport, protocol = await mock_aio_protocol()
3308 light._aio_protocol.data_received(
3309 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
3310 )
3311 await task
3312 assert light.paired_remotes is None
3313 assert "Could not determine 2.4ghz remote config" in caplog.text
3314
3315
3316 @pytest.mark.asyncio
3317 async def test_partial_discovery(mock_aio_protocol, caplog: pytest.LogCaptureFixture):
3318 """Test discovery that is missing hardware data."""
3319 light = AIOWifiLedBulb("192.168.1.166")
3320 light.discovery = FLUX_DISCOVERY_MISSING_HARDWARE
3321
3322 def _updated_callback(*args, **kwargs):
3323 pass
3324
3325 task = asyncio.create_task(light.async_setup(_updated_callback))
3326 transport, protocol = await mock_aio_protocol()
3327 light._aio_protocol.data_received(
3328 b"\x81\x25\x23\x61\x05\x10\xb6\x00\x98\x19\x04\x25\x0f\xde"
3329 )
3330 light._aio_protocol.data_received(
3331 b"\xb0\xb1\xb2\xb3\x00\x01\x01\x5e\x00\x0e\x2b\x01\x00\x00\x00\x00\x29\x00\x00\x00\x00\x00\x00\x55\xde"
3332 )
3333 await task
3334 assert light.hardware is None
3335
3336
3337 @pytest.mark.asyncio
3338 async def test_async_scanner(mock_discovery_aio_protocol):
3339 """Test scanner."""
3340 scanner = AIOBulbScanner()
3341
3342 task = asyncio.ensure_future(
3343 scanner.async_scan(timeout=0.1, address="192.168.213.252")
3344 )
3345 transport, protocol = await mock_discovery_aio_protocol()
3346 protocol.datagram_received(b"HF-A11ASSISTHREAD", ("127.0.0.1", 48899))
3347 protocol.datagram_received(
3348 b"192.168.1.193,DC4F22E6462E,AK001-ZJ200", ("192.168.1.193", 48899)
3349 )
3350 protocol.datagram_received(
3351 b"+ok=25_18_20170908_Armacost\r", ("192.168.1.193", 48899)
3352 )
3353 protocol.datagram_received(
3354 b"+ok=TCP,8806,mhc8806us.magichue.net\r", ("192.168.1.193", 48899)
3355 )
3356
3357 protocol.datagram_received(
3358 b"192.168.213.252,B4E842E10588,AK001-ZJ2145", ("192.168.213.252", 48899)
3359 )
3360 protocol.datagram_received(
3361 b"192.168.198.198,B4E842E10522,AK001-ZJ2149", ("192.168.198.198", 48899)
3362 )
3363 protocol.datagram_received(
3364 b"192.168.198.197,B4E842E10521,AK001-ZJ2146", ("192.168.198.197", 48899)
3365 )
3366 protocol.datagram_received(
3367 b"192.168.198.196,B4E842E10520,AK001-ZJ2144", ("192.168.198.196", 48899)
3368 )
3369 protocol.datagram_received(
3370 b"192.168.211.230,A020A61D892B,AK001-ZJ100", ("192.168.211.230", 48899)
3371 )
3372 protocol.datagram_received(
3373 b"+ok=TCP,GARBAGE,ra8816us02.magichue.net\r", ("192.168.213.252", 48899)
3374 )
3375 protocol.datagram_received(
3376 b"192.168.213.259,B4E842E10586,AK001-ZJ2145", ("192.168.213.259", 48899)
3377 )
3378 protocol.datagram_received(
3379 b"+ok=TCP,8816,ra8816us02.magichue.net\r", ("192.168.213.252", 48899)
3380 )
3381 protocol.datagram_received(
3382 b"+ok=TCP,8806,mhc8806us.magichue.net", ("192.168.211.230", 48899)
3383 )
3384 protocol.datagram_received(b"AT+LVER\r", ("127.0.0.1", 48899))
3385 protocol.datagram_received(
3386 b"+ok=GARBAGE_GARBAGE_GARBAGE_ZG-BL\r", ("192.168.213.252", 48899)
3387 )
3388 protocol.datagram_received(
3389 b"+ok=08_15_20210204_ZG-BL\r", ("192.168.213.252", 48899)
3390 )
3391 protocol.datagram_received(b"+ok=52_3_20210204\r", ("192.168.198.198", 48899))
3392 protocol.datagram_received(b"+ok=62_3\r", ("192.168.198.197", 48899))
3393 protocol.datagram_received(b"+ok=41_3_202\r", ("192.168.198.196", 48899))
3394
3395 protocol.datagram_received(
3396 b"+ok=35_62_20210109_ZG-BL-PWM\r", ("192.168.213.259", 48899)
3397 )
3398 protocol.datagram_received(
3399 b"192.168.213.65,F4CFA23E1AAF,AK001-ZJ2104", ("192.168.213.65", 48899)
3400 )
3401 protocol.datagram_received(
3402 b"+ok=33_11_20170307_IR_mini\r\n", ("192.168.211.230", 48899)
3403 )
3404 protocol.datagram_received(b"+ok=", ("192.168.213.65", 48899))
3405 protocol.datagram_received(b"+ok=A2_33_20200428_ZG-LX\r", ("192.168.213.65", 48899))
3406 protocol.datagram_received(b"+ok=", ("192.168.213.259", 48899))
3407 protocol.datagram_received(
3408 b"+ok=TCP,8816,ra8816us02.magichue.net\r", ("192.168.198.196", 48899)
3409 )
3410 data = await task
3411 assert data == [
3412 {
3413 "firmware_date": datetime.date(2017, 9, 8),
3414 "id": "DC4F22E6462E",
3415 "ipaddr": "192.168.1.193",
3416 "model": "AK001-ZJ200",
3417 "model_description": "Controller RGB/WW/CW",
3418 "model_info": "Armacost",
3419 "model_num": 37,
3420 "remote_access_enabled": True,
3421 "remote_access_host": "mhc8806us.magichue.net",
3422 "remote_access_port": 8806,
3423 "version_num": 24,
3424 },
3425 {
3426 "firmware_date": datetime.date(2021, 2, 4),
3427 "id": "B4E842E10588",
3428 "ipaddr": "192.168.213.252",
3429 "model": "AK001-ZJ2145",
3430 "model_description": "Controller RGB with MIC",
3431 "model_info": "ZG-BL",
3432 "model_num": 8,
3433 "remote_access_enabled": True,
3434 "remote_access_host": "ra8816us02.magichue.net",
3435 "remote_access_port": 8816,
3436 "version_num": 21,
3437 },
3438 {
3439 "firmware_date": datetime.date(2021, 2, 4),
3440 "id": "B4E842E10522",
3441 "ipaddr": "192.168.198.198",
3442 "model": "AK001-ZJ2149",
3443 "model_description": "Bulb CCT",
3444 "model_info": None,
3445 "model_num": 82,
3446 "remote_access_enabled": None,
3447 "remote_access_host": None,
3448 "remote_access_port": None,
3449 "version_num": 3,
3450 },
3451 {
3452 "firmware_date": None,
3453 "id": "B4E842E10521",
3454 "ipaddr": "192.168.198.197",
3455 "model": "AK001-ZJ2146",
3456 "model_description": "Controller CCT",
3457 "model_info": None,
3458 "model_num": 98,
3459 "remote_access_enabled": None,
3460 "remote_access_host": None,
3461 "remote_access_port": None,
3462 "version_num": 3,
3463 },
3464 {
3465 "firmware_date": None,
3466 "id": "B4E842E10520",
3467 "ipaddr": "192.168.198.196",
3468 "model": "AK001-ZJ2144",
3469 "model_description": "Controller Dimmable",
3470 "model_info": None,
3471 "model_num": 65,
3472 "remote_access_enabled": True,
3473 "remote_access_host": "ra8816us02.magichue.net",
3474 "remote_access_port": 8816,
3475 "version_num": 3,
3476 },
3477 {
3478 "firmware_date": datetime.date(2017, 3, 7),
3479 "id": "A020A61D892B",
3480 "ipaddr": "192.168.211.230",
3481 "model": "AK001-ZJ100",
3482 "model_description": "Controller RGB IR Mini",
3483 "model_info": "IR_mini",
3484 "model_num": 51,
3485 "remote_access_enabled": True,
3486 "remote_access_host": "mhc8806us.magichue.net",
3487 "remote_access_port": 8806,
3488 "version_num": 17,
3489 },
3490 {
3491 "firmware_date": datetime.date(2021, 1, 9),
3492 "id": "B4E842E10586",
3493 "ipaddr": "192.168.213.259",
3494 "model": "AK001-ZJ2145",
3495 "model_description": "Bulb RGBCW",
3496 "model_info": "ZG-BL-PWM",
3497 "model_num": 53,
3498 "remote_access_enabled": False,
3499 "remote_access_host": None,
3500 "remote_access_port": None,
3501 "version_num": 98,
3502 },
3503 {
3504 "firmware_date": datetime.date(2020, 4, 28),
3505 "id": "F4CFA23E1AAF",
3506 "ipaddr": "192.168.213.65",
3507 "model": "AK001-ZJ2104",
3508 "model_description": "Addressable v2",
3509 "model_info": "ZG-LX",
3510 "model_num": 162,
3511 "remote_access_enabled": False,
3512 "remote_access_host": None,
3513 "remote_access_port": None,
3514 "version_num": 51,
3515 },
3516 ]
3517
3518
3519 @pytest.mark.asyncio
3520 async def test_async_scanner_specific_address(mock_discovery_aio_protocol):
3521 """Test scanner with a specific address."""
3522 scanner = AIOBulbScanner()
3523
3524 task = asyncio.ensure_future(
3525 scanner.async_scan(timeout=10, address="192.168.213.252")
3526 )
3527 transport, protocol = await mock_discovery_aio_protocol()
3528 protocol.datagram_received(
3529 b"192.168.213.252,B4E842E10588,AK001-ZJ2145", ("192.168.213.252", 48899)
3530 )
3531 protocol.datagram_received(
3532 b"+ok=08_15_20210204_ZG-BL\r", ("192.168.213.252", 48899)
3533 )
3534 protocol.datagram_received(
3535 b"+ok=TCP,8816,ra8816us02.magichue.net\r", ("192.168.213.252", 48899)
3536 )
3537 data = await task
3538 assert data == [
3539 {
3540 "firmware_date": datetime.date(2021, 2, 4),
3541 "id": "B4E842E10588",
3542 "ipaddr": "192.168.213.252",
3543 "model": "AK001-ZJ2145",
3544 "model_description": "Controller RGB with MIC",
3545 "model_info": "ZG-BL",
3546 "model_num": 8,
3547 "version_num": 21,
3548 "remote_access_enabled": True,
3549 "remote_access_host": "ra8816us02.magichue.net",
3550 "remote_access_port": 8816,
3551 }
3552 ]
3553 assert scanner.getBulbInfoByID("B4E842E10588") == {
3554 "firmware_date": datetime.date(2021, 2, 4),
3555 "id": "B4E842E10588",
3556 "ipaddr": "192.168.213.252",
3557 "model": "AK001-ZJ2145",
3558 "model_description": "Controller RGB with MIC",
3559 "model_info": "ZG-BL",
3560 "model_num": 8,
3561 "version_num": 21,
3562 "remote_access_enabled": True,
3563 "remote_access_host": "ra8816us02.magichue.net",
3564 "remote_access_port": 8816,
3565 }
3566 assert scanner.getBulbInfo() == [
3567 {
3568 "firmware_date": datetime.date(2021, 2, 4),
3569 "id": "B4E842E10588",
3570 "ipaddr": "192.168.213.252",
3571 "model": "AK001-ZJ2145",
3572 "model_description": "Controller RGB with MIC",
3573 "model_info": "ZG-BL",
3574 "model_num": 8,
3575 "version_num": 21,
3576 "remote_access_enabled": True,
3577 "remote_access_host": "ra8816us02.magichue.net",
3578 "remote_access_port": 8816,
3579 }
3580 ]
3581
3582
3583 @pytest.mark.asyncio
3584 async def test_async_scanner_specific_address_legacy_device(
3585 mock_discovery_aio_protocol,
3586 ):
3587 """Test scanner with a specific address of a legacy device."""
3588 scanner = AIOBulbScanner()
3589
3590 task = asyncio.ensure_future(
3591 scanner.async_scan(timeout=10, address="192.168.213.252")
3592 )
3593 transport, protocol = await mock_discovery_aio_protocol()
3594 protocol.datagram_received(
3595 b"192.168.213.252,ACCF232E5124,HF-A11-ZJ002", ("192.168.213.252", 48899)
3596 )
3597 protocol.datagram_received(b"+ok=15\r\n\r\n", ("192.168.213.252", 48899))
3598 protocol.datagram_received(b"+ERR=-2\r\n\r\n", ("192.168.213.252", 48899))
3599 data = await task
3600 assert data == [
3601 {
3602 "firmware_date": None,
3603 "id": "ACCF232E5124",
3604 "ipaddr": "192.168.213.252",
3605 "model": "HF-A11-ZJ002",
3606 "model_description": None,
3607 "model_info": None,
3608 "model_num": None,
3609 "remote_access_enabled": None,
3610 "remote_access_host": None,
3611 "remote_access_port": None,
3612 "version_num": 21,
3613 }
3614 ]
3615 assert is_legacy_device(data[0]) is True
3616
3617
3618 @pytest.mark.asyncio
3619 async def test_async_scanner_times_out_with_nothing(mock_discovery_aio_protocol):
3620 """Test scanner."""
3621 scanner = AIOBulbScanner()
3622
3623 task = asyncio.ensure_future(scanner.async_scan(timeout=0.025))
3624 transport, protocol = await mock_discovery_aio_protocol()
3625 data = await task
3626 assert data == []
3627
3628
3629 @pytest.mark.asyncio
3630 async def test_async_scanner_times_out_with_nothing_specific_address(
3631 mock_discovery_aio_protocol,
3632 ):
3633 """Test scanner."""
3634 scanner = AIOBulbScanner()
3635
3636 task = asyncio.ensure_future(
3637 scanner.async_scan(timeout=0.025, address="192.168.213.252")
3638 )
3639 transport, protocol = await mock_discovery_aio_protocol()
3640 data = await task
3641 assert data == []
3642
3643
3644 @pytest.mark.asyncio
3645 async def test_async_scanner_falls_back_to_any_source_port_if_socket_in_use():
3646 """Test port fallback."""
3647 hold_socket = create_udp_socket(AIOBulbScanner.DISCOVERY_PORT)
3648 assert hold_socket.getsockname() == ("0.0.0.0", 48899)
3649 random_socket = create_udp_socket(AIOBulbScanner.DISCOVERY_PORT)
3650 assert random_socket.getsockname() != ("0.0.0.0", 48899)
3651
3652
3653 @pytest.mark.asyncio
3654 async def test_async_scanner_enable_remote_access(mock_discovery_aio_protocol):
3655 """Test scanner enabling remote access with a specific address."""
3656 scanner = AIOBulbScanner()
3657
3658 task = asyncio.ensure_future(
3659 scanner.async_enable_remote_access(
3660 timeout=10,
3661 address="192.168.213.252",
3662 remote_access_host="ra8815us02.magichue.net",
3663 remote_access_port=8815,
3664 )
3665 )
3666 transport, protocol = await mock_discovery_aio_protocol()
3667 protocol.datagram_received(
3668 b"192.168.213.252,B4E842E10588,AK001-ZJ2145", ("192.168.213.252", 48899)
3669 )
3670 protocol.datagram_received(b"+ok\r", ("192.168.213.252", 48899))
3671 protocol.datagram_received(b"+ok\r", ("192.168.213.252", 48899))
3672 await task
3673 assert transport.mock_calls == [
3674 call.sendto(b"HF-A11ASSISTHREAD", ("192.168.213.252", 48899)),
3675 call.sendto(
3676 b"AT+SOCKB=TCP,8815,ra8815us02.magichue.net\r", ("192.168.213.252", 48899)
3677 ),
3678 call.sendto(b"AT+Z\r", ("192.168.213.252", 48899)),
3679 call.close(),
3680 ]
3681
3682
3683 @pytest.mark.asyncio
3684 async def test_async_scanner_disable_remote_access(mock_discovery_aio_protocol):
3685 """Test scanner disable remote access with a specific address."""
3686 scanner = AIOBulbScanner()
3687
3688 task = asyncio.ensure_future(
3689 scanner.async_disable_remote_access(
3690 timeout=10,
3691 address="192.168.213.252",
3692 )
3693 )
3694 transport, protocol = await mock_discovery_aio_protocol()
3695 protocol.datagram_received(
3696 b"192.168.213.252,B4E842E10588,AK001-ZJ2145", ("192.168.213.252", 48899)
3697 )
3698 protocol.datagram_received(b"+ok\r", ("192.168.213.252", 48899))
3699 protocol.datagram_received(b"+ok\r", ("192.168.213.252", 48899))
3700 await task
3701 assert transport.mock_calls == [
3702 call.sendto(b"HF-A11ASSISTHREAD", ("192.168.213.252", 48899)),
3703 call.sendto(b"AT+SOCKB=NONE\r", ("192.168.213.252", 48899)),
3704 call.sendto(b"AT+Z\r", ("192.168.213.252", 48899)),
3705 call.close(),
3706 ]
3707
3708
3709 @pytest.mark.asyncio
3710 async def test_async_scanner_reboot(mock_discovery_aio_protocol):
3711 """Test scanner reboot with a specific address."""
3712 scanner = AIOBulbScanner()
3713
3714 task = asyncio.ensure_future(
3715 scanner.async_reboot(
3716 timeout=10,
3717 address="192.168.213.252",
3718 )
3719 )
3720 transport, protocol = await mock_discovery_aio_protocol()
3721 protocol.datagram_received(
3722 b"192.168.213.252,B4E842E10588,AK001-ZJ2145", ("192.168.213.252", 48899)
3723 )
3724 protocol.datagram_received(b"+ok\r", ("192.168.213.252", 48899))
3725 await task
3726 assert transport.mock_calls == [
3727 call.sendto(b"HF-A11ASSISTHREAD", ("192.168.213.252", 48899)),
3728 call.sendto(b"AT+Z\r", ("192.168.213.252", 48899)),
3729 call.close(),
3730 ]
3731
3732
3733 @pytest.mark.asyncio
3734 async def test_async_scanner_disable_remote_access_timeout(mock_discovery_aio_protocol):
3735 """Test scanner disable remote access with a specific address failure."""
3736 scanner = AIOBulbScanner()
3737 task = asyncio.ensure_future(
3738 scanner.async_disable_remote_access(
3739 timeout=0.02,
3740 address="192.168.213.252",
3741 )
3742 )
3743 transport, protocol = await mock_discovery_aio_protocol()
3744 protocol.datagram_received(
3745 b"192.168.213.252,B4E842E10588,AK001-ZJ2145", ("192.168.213.252", 48899)
3746 )
3747 protocol.datagram_received(b"+ok\r", ("192.168.213.252", 48899))
3748 with pytest.raises(asyncio.TimeoutError):
3749 await task
3750 assert transport.mock_calls == [
3751 call.sendto(b"HF-A11ASSISTHREAD", ("192.168.213.252", 48899)),
3752 call.sendto(b"AT+SOCKB=NONE\r", ("192.168.213.252", 48899)),
3753 call.sendto(b"AT+Z\r", ("192.168.213.252", 48899)),
3754 call.close(),
3755 ]
3756
3757
3758 def test_merge_discoveries() -> None:
3759 """Unit test to make sure we can merge two discoveries."""
3760 full = FLUX_DISCOVERY.copy()
3761 partial = FLUX_DISCOVERY_PARTIAL.copy()
3762 merge_discoveries(partial, full)
3763 assert partial == FLUX_DISCOVERY
3764 assert full == FLUX_DISCOVERY
3765
3766 full = FLUX_DISCOVERY.copy()
3767 partial = FLUX_DISCOVERY_PARTIAL.copy()
3768 merge_discoveries(full, partial)
3769 assert full == FLUX_DISCOVERY
3770
3771
3772 @pytest.mark.asyncio
3773 async def test_armacost():
3774 """Test armacost uses port 34001."""
3775 discovery = FluxLEDDiscovery(
3776 {
3777 "firmware_date": datetime.date(2017, 9, 8),
3778 "id": "DC4F22E6462E",
3779 "ipaddr": "192.168.1.193",
3780 "model": "AK001-ZJ200",
3781 "model_description": "Controller RGB/WW/CW",
3782 "model_info": "Armacost",
3783 "model_num": 37,
3784 "remote_access_enabled": True,
3785 "remote_access_host": "mhc8806us.magichue.net",
3786 "remote_access_port": 8806,
3787 "version_num": 24,
3788 }
3789 )
3790 light = AIOWifiLedBulb("192.168.1.193")
3791 light.discovery = discovery
3792 assert light.port == 34001
3793
3794
3795 @pytest.mark.asyncio
3796 async def test_not_armacost():
3797 """Test not armacost uses 5577."""
3798 discovery = FluxLEDDiscovery(
3799 {
3800 "firmware_date": datetime.date(2021, 2, 4),
3801 "id": "B4E842E10588",
3802 "ipaddr": "192.168.213.252",
3803 "model": "AK001-ZJ2145",
3804 "model_description": "Controller RGB with MIC",
3805 "model_info": "ZG-BL",
3806 "model_num": 8,
3807 "remote_access_enabled": True,
3808 "remote_access_host": "ra8816us02.magichue.net",
3809 "remote_access_port": 8816,
3810 "version_num": 21,
3811 }
3812 )
3813 light = AIOWifiLedBulb("192.168.213.252")
3814 light.discovery = discovery
3815 assert light.port == 5577
3816
3817
3818 def test_extended_state_to_state_full_cool_white():
3819 proto = ProtocolLEDENET25Byte()
3820
3821 # Simulated extended state response payload (starts with EA 81)
3822 raw_state = bytes(
3823 (
3824 0xEA,
3825 0x81,
3826 0x01,
3827 0x00,
3828 0x35,
3829 0x0A,
3830 0x23,
3831 0x61,
3832 0x00,
3833 0x0A,
3834 0x0F,
3835 0x00,
3836 0x00,
3837 0x00,
3838 0x64,
3839 0x64,
3840 0x00,
3841 0x00,
3842 0x00,
3843 0x00,
3844 0xF5,
3845 )
3846 )
3847
3848 assert proto.is_valid_extended_state_response(raw_state) is True
3849
3850 state = proto.extended_state_to_state(raw_state)
3851 assert len(state) == 14
3852
3853 raw_state = LEDENETRawState(*state)
3854
3855 # Validate fields
3856 assert raw_state.power_state == 0x23 # power
3857 assert raw_state.preset_pattern == 0x61 # preset
3858 assert raw_state.red == 0 # red
3859 assert raw_state.green == 0 # green
3860 assert raw_state.blue == 0 # blue
3861 assert raw_state.warm_white == 0 # warm white
3862 assert raw_state.cool_white == 255 # cool white
3863
3864
3865 def test_extended_state_to_state_full_warm_white():
3866 proto = ProtocolLEDENET25Byte()
3867
3868 # Simulated extended state response payload (starts with EA 81)
3869 raw_state = bytes(
3870 (
3871 0xEA,
3872 0x81,
3873 0x01,
3874 0x00,
3875 0x35,
3876 0x0A,
3877 0x23,
3878 0x61,
3879 0x00,
3880 0x0A,
3881 0x0F,
3882 0x00,
3883 0x00,
3884 0x00,
3885 0x00,
3886 0x64,
3887 0x00,
3888 0x00,
3889 0x00,
3890 0x00,
3891 0x94,
3892 )
3893 )
3894
3895 assert proto.is_valid_extended_state_response(raw_state) is True
3896
3897 state = proto.extended_state_to_state(raw_state)
3898 assert len(state) == 14
3899
3900 raw_state = LEDENETRawState(*state)
3901
3902 # Validate fields
3903 assert raw_state.power_state == 0x23 # power
3904 assert raw_state.preset_pattern == 0x61 # preset
3905 assert raw_state.red == 0 # red
3906 assert raw_state.green == 0 # green
3907 assert raw_state.blue == 0 # blue
3908 assert raw_state.warm_white == 255 # warm white
3909 assert raw_state.cool_white == 0 # cool white
3910
3911
3912 def test_extended_state_to_state_full_red():
3913 proto = ProtocolLEDENET25Byte()
3914
3915 # Simulated extended state response payload (starts with EA 81)
3916 raw_state = bytes(
3917 (
3918 0xEA,
3919 0x81,
3920 0x01,
3921 0x00,
3922 0x35,
3923 0x0A,
3924 0x23,
3925 0x61,
3926 0x00,
3927 0x0A,
3928 0xF0,
3929 0x00,
3930 0x64,
3931 0x64,
3932 0x00,
3933 0x00,
3934 0x00,
3935 0x00,
3936 0x00,
3937 0x00,
3938 0xF8,
3939 )
3940 )
3941
3942 assert proto.is_valid_extended_state_response(raw_state) is True
3943
3944 state = proto.extended_state_to_state(raw_state)
3945 assert len(state) == 14
3946
3947 raw_state = LEDENETRawState(*state)
3948
3949 # Validate fields
3950 assert raw_state.power_state == 0x23 # power
3951 assert raw_state.preset_pattern == 0x61 # preset
3952 assert raw_state.red == 255 # red
3953 assert raw_state.green == 0 # green
3954 assert raw_state.blue == 0 # blue
3955 assert raw_state.warm_white == 0 # warm white
3956 assert raw_state.cool_white == 0 # cool white
3957
3958
3959 def test_extended_state_to_state_full_green():
3960 proto = ProtocolLEDENET25Byte()
3961
3962 # Simulated extended state response payload (starts with EA 81)
3963 raw_state = bytes(
3964 (
3965 0xEA,
3966 0x81,
3967 0x01,
3968 0x00,
3969 0x35,
3970 0x0A,
3971 0x23,
3972 0x61,
3973 0x00,
3974 0x0A,
3975 0xF0,
3976 0x3C,
3977 0x64,
3978 0x64,
3979 0x00,
3980 0x00,
3981 0x00,
3982 0x00,
3983 0x00,
3984 0x00,
3985 0x1A,
3986 )
3987 )
3988
3989 assert proto.is_valid_extended_state_response(raw_state) is True
3990
3991 state = proto.extended_state_to_state(raw_state)
3992 assert len(state) == 14
3993
3994 raw_state = LEDENETRawState(*state)
3995
3996 # Validate fields
3997 assert raw_state.power_state == 0x23 # power
3998 assert raw_state.preset_pattern == 0x61 # preset
3999 assert raw_state.red == 0 # red
4000 assert raw_state.green == 255 # green
4001 assert raw_state.blue == 0 # blue
4002 assert raw_state.warm_white == 0 # warm white
4003 assert raw_state.cool_white == 0 # cool white
4004
4005
4006 def test_extended_state_to_state_full_blue():
4007 proto = ProtocolLEDENET25Byte()
4008
4009 # Simulated extended state response payload (starts with EA 81)
4010 raw_state = bytes(
4011 (
4012 0xEA,
4013 0x81,
4014 0x01,
4015 0x00,
4016 0x35,
4017 0x0A,
4018 0x23,
4019 0x61,
4020 0x00,
4021 0x0A,
4022 0xF0,
4023 0x78,
4024 0x64,
4025 0x64,
4026 0x00,
4027 0x00,
4028 0x00,
4029 0x00,
4030 0x00,
4031 0x00,
4032 0x57,
4033 )
4034 )
4035
4036 assert proto.is_valid_extended_state_response(raw_state) is True
4037
4038 state = proto.extended_state_to_state(raw_state)
4039 assert len(state) == 14
4040
4041 raw_state = LEDENETRawState(*state)
4042
4043 # Validate fields
4044 assert raw_state.power_state == 0x23 # power
4045 assert raw_state.preset_pattern == 0x61 # preset
4046 assert raw_state.red == 0 # red
4047 assert raw_state.green == 0 # green
4048 assert raw_state.blue == 255 # blue
4049 assert raw_state.warm_white == 0 # warm white
4050 assert raw_state.cool_white == 0 # cool white
4051
4052
4053 def test_extended_state_to_state_full_yellow():
4054 proto = ProtocolLEDENET25Byte()
4055
4056 # Simulated extended state response payload (starts with EA 81)
4057 raw_state = bytes(
4058 (
4059 0xEA,
4060 0x81,
4061 0x01,
4062 0x00,
4063 0x35,
4064 0x0A,
4065 0x23,
4066 0x61,
4067 0x00,
4068 0x0A,
4069 0xF0,
4070 0x1E,
4071 0x64,
4072 0x64,
4073 0x00,
4074 0x00,
4075 0x00,
4076 0x00,
4077 0x00,
4078 0x00,
4079 0x73,
4080 )
4081 )
4082
4083 assert proto.is_valid_extended_state_response(raw_state) is True
4084
4085 state = proto.extended_state_to_state(raw_state)
4086 assert len(state) == 14
4087
4088 raw_state = LEDENETRawState(*state)
4089
4090 # Validate fields
4091 assert raw_state.power_state == 0x23 # power
4092 assert raw_state.preset_pattern == 0x61 # preset
4093 assert raw_state.red == 255 # red
4094 assert raw_state.green == 255 # green
4095 assert raw_state.blue == 0 # blue
4096 assert raw_state.warm_white == 0 # warm white
4097 assert raw_state.cool_white == 0 # cool white
4098
4099
4100 def test_extended_state_to_state_full_purple():
4101 proto = ProtocolLEDENET25Byte()
4102
4103 # Simulated extended state response payload (starts with EA 81)
4104 raw_state = bytes(
4105 (
4106 0xEA,
4107 0x81,
4108 0x01,
4109 0x00,
4110 0x35,
4111 0x0A,
4112 0x23,
4113 0x61,
4114 0x00,
4115 0x0A,
4116 0xF0,
4117 0x96,
4118 0x64,
4119 0x64,
4120 0x00,
4121 0x00,
4122 0x00,
4123 0x00,
4124 0x00,
4125 0x00,
4126 0x7A,
4127 )
4128 )
4129
4130 assert proto.is_valid_extended_state_response(raw_state) is True
4131
4132 state = proto.extended_state_to_state(raw_state)
4133 assert len(state) == 14
4134
4135 raw_state = LEDENETRawState(*state)
4136
4137 # Validate fields
4138 assert raw_state.power_state == 0x23 # power
4139 assert raw_state.preset_pattern == 0x61 # preset
4140 assert raw_state.red == 255 # red
4141 assert raw_state.green == 0 # green
4142 assert raw_state.blue == 255 # blue
4143 assert raw_state.warm_white == 0 # warm white
4144 assert raw_state.cool_white == 0 # cool white
4145
4146
4147 def test_extended_state_to_state_full_speed_effect():
4148 proto = ProtocolLEDENET25Byte()
4149
4150 # Simulated extended state response payload (starts with EA 81)
4151 raw_state = bytes(
4152 (
4153 0xEA,
4154 0x81,
4155 0x01,
4156 0x00,
4157 0x35,
4158 0x0A,
4159 0x23,
4160 0x25,
4161 0x00,
4162 0x64,
4163 0xF0,
4164 0x0B,
4165 0xE4,
4166 0x64,
4167 0x00,
4168 0x00,
4169 0x00,
4170 0x00,
4171 0x00,
4172 0x00,
4173 0x56,
4174 )
4175 )
4176
4177 assert proto.is_valid_extended_state_response(raw_state) is True
4178
4179 state = proto.extended_state_to_state(raw_state)
4180 assert len(state) == 14
4181
4182 raw_state = LEDENETRawState(*state)
4183
4184 # Validate fields
4185 assert raw_state.power_state == 0x23 # power
4186 assert raw_state.preset_pattern == 0x25 # preset
4187 assert raw_state.red == 255 # red
4188 assert raw_state.green == 0 # green
4189 assert raw_state.blue == 0 # blue
4190 assert raw_state.warm_white == 0 # warm white
4191 assert raw_state.cool_white == 0 # cool white
4192
4193
4194 def test_extended_state_too_short():
4195 proto = ProtocolLEDENET25Byte()
4196 assert proto.extended_state_to_state(b"\xea\x81") == b""
4197
4198
4199 @pytest.mark.asyncio
4200 @pytest.mark.parametrize(
4201 "label,hue_byte,sat_byte,val_byte,expected_rgb",
4202 [
4203 ("red", 0x00, 100, 100, (255, 0, 0)),
4204 ("yellow", 0x1E, 100, 100, (255, 255, 0)),
4205 ("green", 0x3C, 100, 100, (0, 255, 0)),
4206 ("blue", 0x78, 100, 100, (0, 0, 255)),
4207 ],
4208 )
4209 async def test_extended_state_color_parsing(
4210 label, hue_byte, sat_byte, val_byte, expected_rgb
4211 ):
4212 proto = ProtocolLEDENET25Byte()
4213
4214 print(
4215 f"values → hue: {hue_byte} ({type(hue_byte)}), sat: {sat_byte} ({type(sat_byte)}), val: {val_byte} ({type(val_byte)})"
4216 )
4217
4218 raw_state = bytes(
4219 [
4220 0xEA,
4221 0x81,
4222 0x01,
4223 0x00,
4224 0x35,
4225 0x0A,
4226 0x23,
4227 0x61,
4228 0x00,
4229 0x0A,
4230 0xF0,
4231 hue_byte,
4232 sat_byte,
4233 val_byte,
4234 0x00,
4235 0x00,
4236 0x00,
4237 0x00,
4238 0x00,
4239 0x00,
4240 0x7A,
4241 ]
4242 )
4243
4244 result = proto.extended_state_to_state(raw_state)
4245 rgb = tuple(result[6:9])
4246 assert all(abs(a - b) <= 1 for a, b in zip(rgb, expected_rgb)), (
4247 f"{label} RGB mismatch: got {rgb}, expected {expected_rgb}"
4248 )
4249
4250
4251 @pytest.mark.asyncio
4252 async def test_setup_0x35_with_version_num_10(
4253 mock_aio_protocol, caplog: pytest.LogCaptureFixture
4254 ):
4255 """Test we use the right protocol for 0x35 with v10."""
4256 light = AIOWifiLedBulb("192.168.1.166")
4257
4258 def _updated_callback(*args, **kwargs):
4259 pass
4260
4261 task = asyncio.create_task(light.async_setup(_updated_callback))
4262 transport, protocol = await mock_aio_protocol()
4263 light._aio_protocol.data_received(bytes.fromhex("81352361306400ffff000a00f0c6"))
4264 await task
4265 assert light.model_num == 0x35
4266 assert light.protocol == PROTOCOL_LEDENET_25BYTE
4267 assert light.white_active is False
4268 light._aio_protocol.data_received(
4269 bytes(
4270 (
4271 0xB0,
4272 0xB1,
4273 0xB2,
4274 0xB3,
4275 0x00,
4276 0x02,
4277 0x02,
4278 0x17,
4279 0x00,
4280 0x14,
4281 0xEA,
4282 0x81,
4283 0x01,
4284 0x00,
4285 0x35,
4286 0x0A,
4287 0x23,
4288 0x61,
4289 0x24,
4290 0x64,
4291 0x0F,
4292 0x00,
4293 0x00,
4294 0x00,
4295 0x64,
4296 0x64,
4297 0x00,
4298 0x00,
4299 0x00,
4300 0x00,
4301 0x83,
4302 )
4303 )
4304 )
4305 assert light.white_active is True