Debian Codegraph @

flux_led/protocol.py

raw symbols SCIP
1 """FluxLED Protocols."""
2
3 from __future__ import annotations
4
5 import colorsys
6 import contextlib
7 import datetime
8 import logging
9 from abc import abstractmethod
10 from dataclasses import dataclass
11 from enum import Enum
12 from typing import NamedTuple
13
14 from .const import (
15 COLOR_MODE_RGB,
16 COLOR_MODE_RGBW,
17 MUSIC_PIXELS_MAX,
18 MUSIC_PIXELS_PER_SEGMENT_MAX,
19 MUSIC_SEGMENTS_MAX,
20 PIXELS_MAX,
21 PIXELS_PER_SEGMENT_MAX,
22 SEGMENTS_MAX,
23 TRANSITION_GRADUAL,
24 TRANSITION_JUMP,
25 TRANSITION_STROBE,
26 LevelWriteMode,
27 LevelWriteModeData,
28 MultiColorEffects,
29 )
30 from .timer import LedTimer
31 from .utils import (
32 scaled_color_temp_to_white_levels,
33 utils,
34 white_levels_to_scaled_color_temp,
35 )
36
37
38 class RemoteConfig(Enum):
39 DISABLED = 0x01
40 OPEN = 0x02
41 PAIRED_ONLY = 0x03
42
43
44 class PowerRestoreState(Enum):
45 ALWAYS_OFF = 0xFF
46 ALWAYS_ON = 0x0F
47 LAST_STATE = 0xF0
48
49
50 class MusicMode(Enum):
51 STRIP = 0x26
52 LIGHT_SCREEN = 0x27
53
54
55 @dataclass
56 class LEDENETAddressableDeviceConfiguration:
57 pixels_per_segment: int # pixels per segment
58 segments: int | None # number of segments
59 music_pixels_per_segment: int | None # music pixels per segment
60 music_segments: int | None # number of music segments
61 wirings: list[str] # available wirings in the current mode
62 wiring: str | None # RGB/BRG/GBR etc
63 wiring_num: int | None # RGB/BRG/GBR number
64 num_to_wiring: dict[int, str]
65 wiring_to_num: dict[str, int]
66 ic_type: str | None # WS2812B UCS.. etc
67 ic_type_num: int | None # WS2812B UCS.. number etc
68 operating_mode: str | None # RGB, RGBW
69
70
71 @dataclass
72 class PowerRestoreStates:
73 channel1: PowerRestoreState | None
74 channel2: PowerRestoreState | None
75 channel3: PowerRestoreState | None
76 channel4: PowerRestoreState | None
77
78
79 _LOGGER = logging.getLogger(__name__)
80
81
82 # Protocol names
83 PROTOCOL_LEDENET_ORIGINAL = "LEDENET_ORIGINAL"
84 PROTOCOL_LEDENET_ORIGINAL_RGBW = "LEDENET_ORIGINAL_RGBW"
85 PROTOCOL_LEDENET_ORIGINAL_CCT = "LEDENET_ORIGINAL_CCT"
86 PROTOCOL_LEDENET_9BYTE = "LEDENET"
87 PROTOCOL_LEDENET_9BYTE_AUTO_ON = "LEDENET_AUTO_ON"
88 PROTOCOL_LEDENET_9BYTE_DIMMABLE_EFFECTS = "LEDENET_DIMMABLE_EFFECTS"
89 PROTOCOL_LEDENET_SOCKET = "LEDENET_SOCKET"
90 PROTOCOL_LEDENET_8BYTE = "LEDENET_8BYTE" # Previously was called None
91 PROTOCOL_LEDENET_8BYTE_AUTO_ON = "LEDENET_8BYTES_AUTO_ON"
92 PROTOCOL_LEDENET_8BYTE_DIMMABLE_EFFECTS = "LEDENET_8BYTE_DIMMABLE_EFFECTS"
93 PROTOCOL_LEDENET_ADDRESSABLE_A1 = "LEDENET_ADDRESSABLE_A1"
94 PROTOCOL_LEDENET_ADDRESSABLE_A2 = "LEDENET_ADDRESSABLE_A2"
95 PROTOCOL_LEDENET_ADDRESSABLE_A3 = "LEDENET_ADDRESSABLE_A3"
96 PROTOCOL_LEDENET_CCT = "LEDENET_CCT"
97 PROTOCOL_LEDENET_CCT_WRAPPED = "LEDENET_CCT_WRAPPED"
98 PROTOCOL_LEDENET_ADDRESSABLE_CHRISTMAS = "LEDENET_CHRISTMAS"
99 PROTOCOL_LEDENET_25BYTE = "LEDENET_25_BYTE"
100
101 TRANSITION_BYTES = {
102 TRANSITION_JUMP: 0x3B,
103 TRANSITION_STROBE: 0x3C,
104 TRANSITION_GRADUAL: 0x3A,
105 }
106
107
108 LEDNET_MUSIC_MODE_RESPONSE_LEN = 13 # 72 01 26 01 00 00 00 00 00 00 64 64 62
109 LEDENET_POWER_RESTORE_RESPONSE_LEN = 7
110 LEDENET_ORIGINAL_STATE_RESPONSE_LEN = 11
111 LEDENET_STATE_RESPONSE_LEN = 14
112 LEDENET_POWER_RESPONSE_LEN = 4
113 LEDENET_ADDRESSABLE_STATE_RESPONSE_LEN = 25
114 LEDENET_A1_DEVICE_CONFIG_RESPONSE_LEN = 12
115 LEDENET_DEVICE_CONFIG_RESPONSE_LEN = 11
116 LEDENET_REMOTE_CONFIG_RESPONSE_LEN = 14 # 2b 03 00 00 00 00 29 00 00 00 00 00 00 57
117 LEDENET_TIME_RESPONSE_LEN = 12 # 10 14 16 01 02 10 26 20 07 00 0f a9
118 LEDENET_TIMERS_8BYTE_RESPONSE_LEN = 88
119 LEDENET_TIMERS_9BYTE_RESPONSE_LEN = 94
120 LEDENET_TIMERS_SOCKET_RESPONSE_LEN = 100
121
122 MSG_ORIGINAL_POWER_STATE = "original_power_state"
123 MSG_ORIGINAL_STATE = "original_state"
124 MSG_POWER_RESTORE_STATE = "power_restore_state"
125 MSG_POWER_STATE = "power_state"
126 MSG_STATE = "state"
127 MSG_TIME = "time"
128 MSG_TIMERS = "timers"
129 MSG_MUSIC_MODE_STATE = "music_mode_state"
130 MSG_ADDRESSABLE_STATE = "addressable_state"
131 MSG_DEVICE_CONFIG = "device_config"
132 MSG_A1_DEVICE_CONFIG = "a1_device_config"
133 MSG_REMOTE_CONFIG = "remote_config"
134
135 OUTER_MESSAGE_FIRST_BYTE = 0xB0
136
137 MSG_UNIQUE_START = {
138 (0x01, 0x11): MSG_TIME,
139 (0xF0, 0x11): MSG_TIME,
140 (0x0F, 0x11): MSG_TIME,
141 (0x00, 0x11): MSG_TIME,
142 (0x01, 0x22): MSG_TIMERS,
143 (0xF0, 0x22): MSG_TIMERS,
144 (0x0F, 0x22): MSG_TIMERS,
145 (0x00, 0x22): MSG_TIMERS,
146 (0x01, 0x71): MSG_POWER_STATE,
147 (0xF0, 0x71): MSG_POWER_STATE,
148 (0x0F, 0x71): MSG_POWER_STATE,
149 (0x00, 0x71): MSG_POWER_STATE,
150 (0x01, 0x32): MSG_POWER_RESTORE_STATE,
151 (0xF0, 0x32): MSG_POWER_RESTORE_STATE,
152 (0x0F, 0x32): MSG_POWER_RESTORE_STATE,
153 (0x00, 0x32): MSG_POWER_RESTORE_STATE,
154 (0x78,): MSG_ORIGINAL_POWER_STATE,
155 (0x66,): MSG_ORIGINAL_STATE,
156 (0x81,): MSG_STATE,
157 (0x01, 0x63): MSG_DEVICE_CONFIG,
158 (0x00, 0x63): MSG_DEVICE_CONFIG,
159 (0xF0, 0x63): MSG_DEVICE_CONFIG,
160 (0x0F, 0x63): MSG_DEVICE_CONFIG,
161 (0x63,): MSG_A1_DEVICE_CONFIG,
162 (0x72,): MSG_MUSIC_MODE_STATE,
163 (0x2B,): MSG_REMOTE_CONFIG,
164 }
165
166 MSG_LENGTHS = {
167 MSG_TIME: LEDENET_TIME_RESPONSE_LEN,
168 MSG_REMOTE_CONFIG: LEDENET_REMOTE_CONFIG_RESPONSE_LEN,
169 MSG_MUSIC_MODE_STATE: LEDNET_MUSIC_MODE_RESPONSE_LEN,
170 MSG_POWER_STATE: LEDENET_POWER_RESPONSE_LEN,
171 MSG_POWER_RESTORE_STATE: LEDENET_POWER_RESTORE_RESPONSE_LEN,
172 MSG_ORIGINAL_POWER_STATE: LEDENET_POWER_RESPONSE_LEN,
173 MSG_ORIGINAL_STATE: LEDENET_ORIGINAL_STATE_RESPONSE_LEN,
174 MSG_STATE: LEDENET_STATE_RESPONSE_LEN,
175 MSG_ADDRESSABLE_STATE: LEDENET_ADDRESSABLE_STATE_RESPONSE_LEN,
176 MSG_DEVICE_CONFIG: LEDENET_DEVICE_CONFIG_RESPONSE_LEN,
177 MSG_A1_DEVICE_CONFIG: LEDENET_A1_DEVICE_CONFIG_RESPONSE_LEN,
178 }
179
180 OUTER_MESSAGE_WRAPPER_FIRST_BYTES = [OUTER_MESSAGE_FIRST_BYTE, 0xB1, 0xB2, 0xB3, 0x00]
181 OUTER_MESSAGE_WRAPPER = [*OUTER_MESSAGE_WRAPPER_FIRST_BYTES, 0x01]
182 OUTER_MESSAGE_WRAPPER_START_LEN = 10
183 CHECKSUM_LEN = 1
184
185
186 POWER_RESTORE_BYTES_TO_POWER_RESTORE = {
187 restore_state.value: restore_state for restore_state in PowerRestoreState
188 }
189
190
191 REMOTE_CONFIG_BYTES_TO_REMOTE_CONFIG = {
192 remote_config.value: remote_config for remote_config in RemoteConfig
193 }
194
195
196 def _message_type_from_start_of_msg(data: bytes) -> str | None:
197 if len(data) > 1:
198 return MSG_UNIQUE_START.get(
199 (data[0], data[1]), MSG_UNIQUE_START.get((data[0],))
200 )
201 return MSG_UNIQUE_START.get((data[0],)) if len(data) else None
202
203
204 class LEDENETOriginalRawState(NamedTuple):
205 head: int
206 model_num: int
207 power_state: int
208 preset_pattern: int
209 mode: int
210 speed: int
211 red: int
212 green: int
213 blue: int
214 warm_white: int
215 check_sum: int
216 cool_white: int
217
218
219 # typical response:
220 # pos 0 1 2 3 4 5 6 7 8 9 10
221 # 66 01 24 39 21 0a ff 00 00 01 99
222 # | | | | | | | | | | |
223 # | | | | | | | | | | checksum
224 # | | | | | | | | | warmwhite
225 # | | | | | | | | blue
226 # | | | | | | | green
227 # | | | | | | red
228 # | | | | | speed: 0f = highest f0 is lowest
229 # | | | | <don't know yet>
230 # | | | preset pattern
231 # | | off(24)/on(23)
232 # | model_num (type)
233 # msg head
234 #
235
236
237 class LEDENETRawState(NamedTuple):
238 head: int
239 model_num: int
240 power_state: int
241 preset_pattern: int
242 mode: int
243 speed: int
244 red: int
245 green: int
246 blue: int
247 warm_white: int
248 version_number: int
249 cool_white: int
250 color_mode: int
251 check_sum: int
252
253
254 # response from a 5-channel LEDENET controller:
255 # pos 0 1 2 3 4 5 6 7 8 9 10 11 12 13
256 # 81 25 23 61 21 06 38 05 06 f9 01 00 0f 9d
257 # | | | | | | | | | | | | | |
258 # | | | | | | | | | | | | | checksum
259 # | | | | | | | | | | | | color mode (f0 colors were set, 0f whites, 00 all were set)
260 # | | | | | | | | | | | cool-white 0x00 to 0xFF
261 # | | | | | | | | | | version number
262 # | | | | | | | | | warmwhite 0x00 to 0xFF
263 # | | | | | | | | blue 0x00 to 0xFF
264 # | | | | | | | green 0x00 to 0xFF
265 # | | | | | | red 0x00 to 0xFF
266 # | | | | | speed: 0x01 = highest 0x1f is lowest
267 # | | | | Mode WW(01), WW+CW(02), RGB(03), RGBW(04), RGBWW(05)
268 # | | | preset pattern
269 # | | off(24)/on(23)
270 # | model_num (type)
271 # msg head
272 #
273 RGB_NUM_TO_WIRING = {1: "RGB", 2: "GRB", 3: "BRG"}
274 RGB_WIRING_TO_NUM = {v: k for k, v in RGB_NUM_TO_WIRING.items()}
275 RGBW_NUM_TO_WIRING = {1: "RGBW", 2: "GRBW", 3: "BRGW"}
276 RGBW_WIRING_TO_NUM = {v: k for k, v in RGBW_NUM_TO_WIRING.items()}
277 RGBW_NUM_TO_MODE = {4: "RGB&W", 6: "RGB/W"}
278 RGBW_MODE_TO_NUM = {v: k for k, v in RGBW_NUM_TO_MODE.items()}
279 RGBWW_NUM_TO_WIRING = {
280 1: "RGBCW",
281 2: "GRBCW",
282 3: "BRGCW",
283 4: "RGBWC",
284 5: "GRBWC",
285 6: "BRGWC",
286 7: "WRGBC",
287 8: "WGRBC",
288 9: "WBRGC",
289 10: "CRGBW",
290 11: "CBRBW",
291 12: "CBRGW",
292 13: "WCRGB",
293 14: "WCGRB",
294 15: "WCBRG",
295 }
296 RGBWW_WIRING_TO_NUM = {v: k for k, v in RGBWW_NUM_TO_WIRING.items()}
297 RGBWW_NUM_TO_MODE = {5: "RGB&CCT", 7: "RGB/CCT"}
298 RGBWW_MODE_TO_NUM = {v: k for k, v in RGBWW_NUM_TO_MODE.items()}
299
300 ADDRESSABLE_RGB_NUM_TO_WIRING = {
301 0: "RGB",
302 1: "RBG",
303 2: "GRB",
304 3: "GBR",
305 4: "BRG",
306 5: "BGR",
307 }
308 ADDRESSABLE_RGB_WIRING_TO_NUM = {v: k for k, v in ADDRESSABLE_RGB_NUM_TO_WIRING.items()}
309 ADDRESSABLE_RGBW_NUM_TO_WIRING = {
310 0: "RGBW",
311 1: "RBGW",
312 2: "GRBW",
313 3: "GBRW",
314 4: "BRGW",
315 5: "BGRW",
316 6: "WRGB",
317 7: "WRBG",
318 8: "WGRB",
319 9: "WGBR",
320 10: "WBRG",
321 11: "WBGR",
322 }
323 ADDRESSABLE_RGBW_WIRING_TO_NUM = {
324 v: k for k, v in ADDRESSABLE_RGBW_NUM_TO_WIRING.items()
325 }
326
327
328 A1_NUM_TO_PROTOCOL = {
329 1: "UCS1903",
330 2: "SM16703",
331 3: "WS2811",
332 4: "WS2812B",
333 5: "SK6812",
334 6: "INK1003",
335 7: "WS2801",
336 8: "LB1914",
337 }
338 A1_PROTOCOL_TO_NUM = {v: k for k, v in A1_NUM_TO_PROTOCOL.items()}
339
340 A1_NUM_TO_OPERATING_MODE = {
341 1: COLOR_MODE_RGB,
342 2: COLOR_MODE_RGB,
343 3: COLOR_MODE_RGB,
344 4: COLOR_MODE_RGB,
345 5: COLOR_MODE_RGB,
346 6: COLOR_MODE_RGB,
347 7: COLOR_MODE_RGB,
348 8: COLOR_MODE_RGB,
349 }
350 A1_OPERATING_MODE_TO_NUM = {v: k for k, v in A1_NUM_TO_OPERATING_MODE.items()}
351
352 A2_NUM_TO_PROTOCOL = {
353 1: "UCS1903",
354 2: "SM16703",
355 3: "WS2811",
356 4: "WS2811B",
357 5: "SK6812",
358 6: "INK1003",
359 7: "WS2801",
360 8: "WS2815",
361 9: "APA102",
362 10: "TM1914",
363 11: "UCS2904B",
364 }
365 A2_PROTOCOL_TO_NUM = {v: k for k, v in A2_NUM_TO_PROTOCOL.items()}
366
367 A2_NUM_TO_OPERATING_MODE = {
368 1: COLOR_MODE_RGB,
369 2: COLOR_MODE_RGB,
370 3: COLOR_MODE_RGB,
371 4: COLOR_MODE_RGB,
372 5: COLOR_MODE_RGB,
373 6: COLOR_MODE_RGB,
374 7: COLOR_MODE_RGB,
375 8: COLOR_MODE_RGB,
376 9: COLOR_MODE_RGB,
377 10: COLOR_MODE_RGB,
378 11: COLOR_MODE_RGB,
379 }
380 A2_OPERATING_MODE_TO_NUM = {v: k for k, v in A2_NUM_TO_OPERATING_MODE.items()}
381
382 NEW_ADDRESSABLE_NUM_TO_PROTOCOL = {
383 1: "WS2812B",
384 2: "SM16703",
385 3: "SM16704",
386 4: "WS2811",
387 5: "UCS1903",
388 6: "SK6812",
389 7: "SK6812RGBW",
390 8: "INK1003",
391 9: "UCS2904B",
392 }
393 NEW_ADDRESSABLE_PROTOCOL_TO_NUM = {
394 v: k for k, v in NEW_ADDRESSABLE_NUM_TO_PROTOCOL.items()
395 }
396
397 NEW_ADDRESSABLE_NUM_TO_OPERATING_MODE = {
398 1: COLOR_MODE_RGB,
399 2: COLOR_MODE_RGB,
400 3: COLOR_MODE_RGB,
401 4: COLOR_MODE_RGB,
402 5: COLOR_MODE_RGB,
403 6: COLOR_MODE_RGB,
404 7: COLOR_MODE_RGBW,
405 8: COLOR_MODE_RGB,
406 9: COLOR_MODE_RGB,
407 }
408 NEW_ADDRESSABLE_OPERATING_MODE_TO_NUM = {
409 v: k for k, v in NEW_ADDRESSABLE_NUM_TO_OPERATING_MODE.items()
410 }
411
412
413 class ProtocolBase:
414 """The base protocol."""
415
416 power_state_response_length = MSG_LENGTHS[MSG_POWER_STATE]
417 level_write_modes = LevelWriteModeData(ALL=0x00, COLORS=0xF0, WHITES=0x0F)
418
419 def __init__(self) -> None:
420 self._counter = -1
421 super().__init__()
422
423 @property
424 def speed_is_delay(self) -> bool:
425 """If True the speed is a delay."""
426 return True
427
428 @property
429 def requires_turn_on(self) -> bool:
430 """If True the device must be turned on before setting level/patterns/modes."""
431 return True
432
433 @property
434 def power_push_updates(self) -> bool:
435 """If True the protocol pushes power state updates when controlled via ir/rf/app."""
436 return False
437
438 @property
439 def state_push_updates(self) -> bool:
440 """If True the protocol pushes state updates when controlled via ir/rf/app."""
441 return False
442
443 @property
444 def zones(self) -> bool:
445 """If the protocol supports zones."""
446 return False
447
448 def _increment_counter(self) -> int:
449 """Increment the counter byte."""
450 self._counter += 1
451 if self._counter == 255:
452 self._counter = 0
453 return self._counter
454
455 def is_valid_power_restore_state_response(self, msg: bytes) -> bool:
456 """Check if a power state response is valid."""
457 return (
458 _message_type_from_start_of_msg(msg) == MSG_POWER_RESTORE_STATE
459 and len(msg) == LEDENET_POWER_RESTORE_RESPONSE_LEN
460 and self.is_checksum_correct(msg)
461 )
462
463 def is_valid_outer_message(self, data: bytes) -> bool:
464 """Check if a message is a valid outer message."""
465 if not data.startswith(bytearray(OUTER_MESSAGE_WRAPPER_FIRST_BYTES)):
466 return False
467 return self.is_checksum_correct(data)
468
469 def extract_inner_message(self, msg: bytes) -> bytes:
470 """Extract the inner message from a wrapped message."""
471 return msg[10:-1]
472
473 def is_valid_device_config_response(self, data: bytes) -> bool:
474 """Check if a message is a valid ic state response."""
475 return False
476
477 def expected_response_length(self, data: bytes) -> int:
478 """
479 Return the number of bytes expected in the response.
480
481 If the response is unknown, we assume the response is
482 a complete message since we have no way of knowing otherwise.
483 """
484 if data[0] == OUTER_MESSAGE_FIRST_BYTE: # This is a wrapper message
485 if len(data) < OUTER_MESSAGE_WRAPPER_START_LEN:
486 return OUTER_MESSAGE_WRAPPER_START_LEN
487 inner_msg_len = (data[8] << 8) + data[9]
488 return (
489 OUTER_MESSAGE_WRAPPER_START_LEN # Includes the two bytes that are the size of the inner message
490 + inner_msg_len # The inner message itself (with checksum)
491 + CHECKSUM_LEN # The checksum of the full message
492 )
493
494 msg_type = _message_type_from_start_of_msg(data)
495 if msg_type is None:
496 return len(data)
497 if msg_type == MSG_TIMERS:
498 return self.timer_response_len
499 return MSG_LENGTHS[msg_type]
500
501 @abstractmethod
502 def construct_state_query(self) -> bytearray:
503 """The bytes to send for a query request."""
504
505 @abstractmethod
506 def is_valid_state_response(self, raw_state: bytes) -> bool:
507 """Check if a state response is valid."""
508
509 def is_valid_extended_state_response(self, raw_state: bytes) -> bool:
510 return False
511
512 @abstractmethod
513 def extended_state_to_state(self, raw_state: bytes) -> bytes:
514 """Convert an extended state response to a state response."""
515
516 def is_checksum_correct(self, msg: bytes) -> bool:
517 """Check a checksum of a message."""
518 expected_sum = sum(msg[0:-1]) & 0xFF
519 if expected_sum != msg[-1]:
520 _LOGGER.warning(
521 "Checksum mismatch: Expected %s, got %s", expected_sum, msg[-1]
522 )
523 return False
524 return True
525
526 @abstractmethod
527 def is_valid_power_state_response(self, msg: bytes) -> bool:
528 """Check if a power state response is valid."""
529
530 @property
531 def on_byte(self) -> int:
532 """The on byte."""
533 return 0x23
534
535 @property
536 def off_byte(self) -> int:
537 """The off byte."""
538 return 0x24
539
540 @property
541 def dimmable_effects(self) -> bool:
542 """Protocol supports dimmable effects."""
543 return False
544
545 @abstractmethod
546 def construct_state_change(self, turn_on: int) -> bytearray:
547 """The bytes to send for a state change request."""
548
549 def construct_power_restore_state_query(self) -> bytearray:
550 """The bytes to send for a query power restore state."""
551 return self.construct_message(bytearray([0x32, 0x3A, 0x3B, 0x0F]))
552
553 def construct_get_time(self) -> bytearray:
554 """Construct a get time command."""
555 return self.construct_message(bytearray([0x11, 0x1A, 0x1B, 0x0F]))
556
557 def is_valid_get_time_response(self, msg: bytes) -> bool:
558 """Check if the response is a valid time response."""
559 return (
560 _message_type_from_start_of_msg(msg) == MSG_TIME
561 and len(msg) == LEDENET_TIME_RESPONSE_LEN
562 and self.is_checksum_correct(msg)
563 )
564
565 def parse_get_time(self, rx: bytes) -> datetime.datetime | None:
566 """Parse a get time command."""
567 if self.is_valid_get_time_response(rx):
568 with contextlib.suppress(Exception):
569 return datetime.datetime(
570 rx[3] + 2000, rx[4], rx[5], rx[6], rx[7], rx[8]
571 )
572 return None
573
574 def construct_set_time(self, time: datetime.datetime | None) -> bytearray:
575 """Construct a set time command."""
576 dt = time or datetime.datetime.now()
577 return self.construct_message(
578 bytearray(
579 [
580 0x10,
581 0x14,
582 dt.year - 2000,
583 dt.month,
584 dt.day,
585 dt.hour,
586 dt.minute,
587 dt.second,
588 dt.isoweekday(), # day of week
589 0x00,
590 0x0F,
591 ]
592 )
593 )
594
595 def construct_get_timers(self) -> bytearray:
596 """The bytes to get timers."""
597 return self.construct_message(bytearray([0x22, 0x2A, 0x2B, 0x0F]))
598
599 @property
600 def timer_response_len(self) -> int:
601 """Return the time response len."""
602 return LEDENET_TIMERS_8BYTE_RESPONSE_LEN
603
604 @property
605 def timer_len(self) -> int:
606 """Return a single timer len."""
607 return 14
608
609 @property
610 def timer_count(self) -> int:
611 """Return the number of timers."""
612 return 6
613
614 def is_valid_timers_response(self, msg: bytes) -> bool:
615 """Check if the response is a valid timers response."""
616 return (
617 _message_type_from_start_of_msg(msg) == MSG_TIMERS
618 and len(msg) == self.timer_response_len
619 and self.is_checksum_correct(msg)
620 )
621
622 def parse_get_timers(self, msg: bytes) -> list[LedTimer]:
623 """Parse get timers."""
624 if not self.is_valid_timers_response(msg):
625 raise ValueError(f"Timers response not valid: {msg!r}")
626 start = 2
627 timer_list = []
628 timer_bytes_len = self.timer_len
629 # pass in the timer_len-byte timer structs
630 for _ in range(self.timer_count):
631 timer_bytes = msg[start:][:timer_bytes_len]
632 timer = LedTimer(timer_bytes)
633 timer_list.append(timer)
634 start += timer_bytes_len
635 return timer_list
636
637 def construct_set_timers(self, timer_list: list[LedTimer]) -> bytearray:
638 """Construct a set timers message."""
639 # remove inactive or expired timers from list
640 for t in timer_list:
641 t.length = self.timer_len
642 if not t.isActive() or t.isExpired():
643 timer_list.remove(t)
644
645 # truncate if more than 6
646 if len(timer_list) > self.timer_count:
647 _LOGGER.warning("too many timers, truncating list")
648 del timer_list[self.timer_count :]
649
650 # pad list to 6 with inactive timers
651 if len(timer_list) != self.timer_count:
652 for i in range(self.timer_count - len(timer_list)):
653 timer_list.append(LedTimer(length=self.timer_len))
654
655 msg = bytearray([0x21])
656 for t in timer_list:
657 msg.extend(t.toBytes())
658 msg.extend(bytearray([0x00, 0xF0]))
659 return self.construct_message(msg)
660
661 def construct_power_restore_state_change(
662 self, restore_state: PowerRestoreStates
663 ) -> bytearray:
664 """
665 The bytes to send for a power restore state change.
666
667 Set power on state to keep last state
668 31f0f0f0f0f0e1
669 Set power on state to always on
670 310ff0f0f0f000
671 Set power on state to always off
672 31fff0f0f0f0f0
673 """
674 return self.construct_message(
675 bytearray(
676 [
677 0x31,
678 restore_state.channel1.value if restore_state.channel1 else 0x00,
679 restore_state.channel2.value if restore_state.channel2 else 0x00,
680 restore_state.channel3.value if restore_state.channel3 else 0x00,
681 restore_state.channel4.value if restore_state.channel4 else 0x00,
682 0xF0,
683 ]
684 )
685 )
686
687 @abstractmethod
688 def construct_music_mode(
689 self,
690 sensitivity: int,
691 brightness: int,
692 mode: int | None,
693 effect: int | None,
694 foreground_color: tuple[int, int, int] | None = None,
695 background_color: tuple[int, int, int] | None = None,
696 ) -> list[bytearray]:
697 """The bytes to send to set music mode."""
698
699 @abstractmethod
700 def construct_levels_change(
701 self,
702 persist: int,
703 red: int | None,
704 green: int | None,
705 blue: int | None,
706 warm_white: int | None,
707 cool_white: int | None,
708 write_mode: LevelWriteMode | int,
709 ) -> list[bytearray]:
710 """The bytes to send for a level change request."""
711
712 @abstractmethod
713 def construct_preset_pattern(
714 self, pattern: int, speed: int, brightness: int
715 ) -> bytearray:
716 """The bytes to send for a preset pattern."""
717
718 def construct_custom_effect(
719 self, rgb_list: list[tuple[int, int, int]], speed: int, transition_type: str
720 ) -> bytearray:
721 """The bytes to send for a custom effect."""
722 msg = bytearray()
723 first_color = True
724 for rgb in rgb_list:
725 if first_color:
726 lead_byte = 0x51
727 first_color = False
728 else:
729 lead_byte = 0
730 r, g, b = rgb
731 msg.extend(bytearray([lead_byte, r, g, b]))
732 # pad out empty slots
733 if len(rgb_list) != 16:
734 for i in range(16 - len(rgb_list)):
735 msg.extend(bytearray([0, 1, 2, 3]))
736 msg.append(0x00)
737 msg.append(utils.speedToDelay(speed))
738 msg.append(
739 TRANSITION_BYTES.get(transition_type, TRANSITION_BYTES[TRANSITION_GRADUAL])
740 ) # default to "gradual"
741 msg.append(0xFF)
742 msg.append(0x0F)
743 return self.construct_message(msg)
744
745 @property
746 @abstractmethod
747 def name(self) -> str:
748 """The name of the protocol."""
749
750 @property
751 @abstractmethod
752 def state_response_length(self) -> int:
753 """The length of the query response."""
754
755 @abstractmethod
756 def construct_message(self, raw_bytes: bytearray) -> bytearray:
757 """Original protocol uses no checksum."""
758
759 def construct_wrapped_message(
760 self,
761 msg: bytearray,
762 inner_pre_constructed: bool = False,
763 version: int = 0x01,
764 ) -> bytearray:
765 """Construct a wrapped message."""
766 inner_msg = msg if inner_pre_constructed else self.construct_message(msg)
767 inner_msg_len = len(inner_msg)
768 return self.construct_message(
769 bytearray(
770 [
771 *OUTER_MESSAGE_WRAPPER,
772 version,
773 self._increment_counter(),
774 inner_msg_len >> 8,
775 inner_msg_len & 0xFF,
776 *inner_msg,
777 ]
778 )
779 )
780
781 @abstractmethod
782 def named_raw_state(
783 self, raw_state: bytes
784 ) -> LEDENETOriginalRawState | LEDENETRawState:
785 """Convert raw_state to a namedtuple."""
786
787 @abstractmethod
788 def is_valid_remote_config_response(self, msg: bytes) -> bool:
789 """Check if a remote config response is valid."""
790 return _message_type_from_start_of_msg(
791 msg
792 ) == MSG_REMOTE_CONFIG and self.is_checksum_correct(msg)
793
794 def construct_query_remote_config(self) -> bytearray:
795 """Construct a remote config query"""
796 return self.construct_wrapped_message(bytearray([0x2B, 0x2C, 0x2D]))
797
798 def construct_remote_config(self, remote_config: RemoteConfig) -> bytearray:
799 """Construct an remote config."""
800 # 2a 02 ff ff ff ff ff 00 00 00 00 00 00 00 0f
801 return self.construct_wrapped_message(
802 bytearray(
803 [
804 0x2A,
805 remote_config.value,
806 0xFF,
807 0xFF,
808 0xFF,
809 0xFF,
810 0xFF,
811 0x00,
812 0x00,
813 0x00,
814 0x00,
815 0x00,
816 0x00,
817 0x00,
818 0x0F,
819 ]
820 )
821 )
822
823 def construct_unpair_remotes(self) -> bytearray:
824 """Construct an unpair remotes command."""
825 return self.construct_wrapped_message(
826 bytearray(
827 [
828 0x2A,
829 0xFF,
830 0xFF,
831 0x01,
832 0xFF,
833 0xFF,
834 0xFF,
835 0x00,
836 0x00,
837 0x00,
838 0x00,
839 0x00,
840 0x00,
841 0x00,
842 0xF0,
843 ]
844 )
845 )
846
847 def get_write_all_colors(self) -> set[int]:
848 return {self.level_write_modes.ALL, self.level_write_modes.COLORS}
849
850 def get_write_all_whites(self) -> set[int]:
851 return {self.level_write_modes.ALL, self.level_write_modes.WHITES}
852
853
854 class ProtocolLEDENETOriginal(ProtocolBase):
855 """The original LEDENET protocol with no checksums."""
856
857 @property
858 def name(self) -> str:
859 """The name of the protocol."""
860 return PROTOCOL_LEDENET_ORIGINAL
861
862 @property
863 def state_response_length(self) -> int:
864 """The length of the query response."""
865 return LEDENET_ORIGINAL_STATE_RESPONSE_LEN
866
867 def is_valid_power_state_response(self, msg: bytes) -> bool:
868 """Check if a power state response is valid."""
869 return len(msg) == self.power_state_response_length and msg[0] == 0x78
870
871 def is_valid_state_response(self, raw_state: bytes) -> bool:
872 """Check if a state response is valid."""
873 return len(raw_state) == self.state_response_length and raw_state[0] == 0x66
874
875 def construct_preset_pattern(
876 self, pattern: int, speed: int, brightness: int
877 ) -> bytearray:
878 """The bytes to send for a preset pattern."""
879 delay = utils.speedToDelay(speed)
880 return self.construct_message(bytearray([0xBB, pattern, delay, 0x44]))
881
882 def construct_state_query(self) -> bytearray:
883 """The bytes to send for a query request."""
884 return self.construct_message(bytearray([0xEF, 0x01, 0x77]))
885
886 def construct_state_change(self, turn_on: int) -> bytearray:
887 """The bytes to send for a state change request."""
888 return self.construct_message(
889 bytearray([0xCC, self.on_byte if turn_on else self.off_byte, 0x33])
890 )
891
892 def construct_levels_change(
893 self,
894 persist: int,
895 red: int | None,
896 green: int | None,
897 blue: int | None,
898 warm_white: int | None,
899 cool_white: int | None,
900 write_mode: LevelWriteMode | int,
901 ) -> list[bytearray]:
902 """The bytes to send for a level change request."""
903 # sample message for original LEDENET protocol (w/o checksum at end)
904 # 0 1 2 3 4
905 # 56 90 fa 77 aa
906 # | | | | |
907 # | | | | terminator
908 # | | | blue
909 # | | green
910 # | red
911 # head
912 return [
913 self.construct_message(
914 bytearray([0x56, red or 0x00, green or 0x00, blue or 0x00, 0xAA])
915 )
916 ]
917
918 def construct_message(self, raw_bytes: bytearray) -> bytearray:
919 """Original protocol uses no checksum."""
920 return raw_bytes
921
922 def named_raw_state(self, raw_state: bytes) -> LEDENETOriginalRawState:
923 """Convert raw_state to a namedtuple."""
924 raw_bytearray = bytearray([*raw_state, 0])
925 return LEDENETOriginalRawState(*raw_bytearray)
926
927
928 class ProtocolLEDENETOriginalRGBW(ProtocolLEDENETOriginal):
929 @property
930 def name(self) -> str:
931 """The name of the protocol."""
932 return PROTOCOL_LEDENET_ORIGINAL_RGBW
933
934 def construct_levels_change(
935 self,
936 persist: int,
937 red: int | None,
938 green: int | None,
939 blue: int | None,
940 warm_white: int | None,
941 cool_white: int | None,
942 write_mode: LevelWriteMode | int,
943 ) -> list[bytearray]:
944 """The bytes to send for a level change request."""
945 # sample message for original LEDENET RGBW protocol (w/o checksum at end)
946 return [
947 self.construct_message(
948 bytearray(
949 [
950 0x56,
951 red or 0x00,
952 green or 0x00,
953 blue or 0x00,
954 warm_white or 0x00,
955 write_mode.value
956 if isinstance(write_mode, LevelWriteMode)
957 else write_mode,
958 0xAA,
959 ]
960 )
961 )
962 ]
963
964
965 class ProtocolLEDENETOriginalCCT(ProtocolLEDENETOriginal):
966 @property
967 def name(self) -> str:
968 """The name of the protocol."""
969 return PROTOCOL_LEDENET_ORIGINAL_CCT
970
971 def construct_levels_change(
972 self,
973 persist: int,
974 red: int | None,
975 green: int | None,
976 blue: int | None,
977 warm_white: int | None,
978 cool_white: int | None,
979 write_mode: LevelWriteMode | int,
980 ) -> list[bytearray]:
981 """The bytes to send for a level change request."""
982 # sample message for original LEDENET protocol (w/o checksum at end)
983 # 0 1 2 3 4
984 # 56 90 fa 77 aa
985 # | | | | |
986 # | | | | terminator
987 # | | | blue
988 # | | green
989 # | red
990 # head
991 return [
992 self.construct_message(bytearray([0x56, red or 0x00, green or 0x00, 0xAA]))
993 ]
994
995
996 class ProtocolLEDENET8Byte(ProtocolBase):
997 """The newer LEDENET protocol with checksums that uses 8 bytes to set state."""
998
999 @property
1000 def name(self) -> str:
1001 """The name of the protocol."""
1002 return PROTOCOL_LEDENET_8BYTE
1003
1004 @property
1005 def state_response_length(self) -> int:
1006 """The length of the query response."""
1007 return LEDENET_STATE_RESPONSE_LEN
1008
1009 def is_valid_power_state_response(self, msg: bytes) -> bool:
1010 """Check if a power state response is valid."""
1011 if (
1012 len(msg) != self.power_state_response_length
1013 or not self._is_start_of_power_state_response(msg)
1014 or msg[1] != 0x71
1015 or msg[2] not in (self.on_byte, self.off_byte)
1016 ):
1017 return False
1018 return True # checksum does not always match
1019
1020 def _is_start_of_power_state_response(self, data: bytes) -> bool:
1021 """Check if a message is the start of a state response."""
1022 return _message_type_from_start_of_msg(data) == MSG_POWER_STATE
1023
1024 def is_valid_state_response(self, raw_state: bytes) -> bool:
1025 """Check if a state response is valid."""
1026 if len(raw_state) != self.state_response_length:
1027 return False
1028 if raw_state[0] != 129:
1029 return False
1030 return self.is_checksum_correct(raw_state)
1031
1032 def construct_state_change(self, turn_on: int) -> bytearray:
1033 """
1034 The bytes to send for a state change request.
1035
1036 Alternate messages
1037
1038 Off 3b 24 00 00 00 00 00 00 00 32 00 00 91
1039 On 3b 23 00 00 00 00 00 00 00 32 00 00 90
1040 """
1041 return self.construct_message(
1042 bytearray([0x71, self.on_byte if turn_on else self.off_byte, 0x0F])
1043 )
1044
1045 def construct_preset_pattern(
1046 self, pattern: int, speed: int, brightness: int
1047 ) -> bytearray:
1048 """The bytes to send for a preset pattern."""
1049 delay = utils.speedToDelay(speed)
1050 return self.construct_message(bytearray([0x61, pattern, delay, 0x0F]))
1051
1052 def construct_levels_change(
1053 self,
1054 persist: int,
1055 red: int | None,
1056 green: int | None,
1057 blue: int | None,
1058 warm_white: int | None,
1059 cool_white: int | None,
1060 write_mode: LevelWriteMode | int,
1061 ) -> list[bytearray]:
1062 """The bytes to send for a level change request."""
1063 # sample message for 8-byte protocols (w/ checksum at end)
1064 # 0 1 2 3 4 5 6
1065 # 31 90 fa 77 00 00 0f
1066 # | | | | | | |
1067 # | | | | | | terminator
1068 # | | | | | write mask / white2 (see below)
1069 # | | | | white
1070 # | | | blue
1071 # | | green
1072 # | red
1073 # persistence (31 for true / 41 for false)
1074 #
1075 # byte 5 can have different values depending on the type
1076 # of device:
1077 # For devices that support 2 types of white value (warm and cold
1078 # white) this value is the cold white value. These use the LEDENET
1079 # protocol. If a second value is not given, reuse the first white value.
1080 #
1081 # For devices that cannot set both rbg and white values at the same time
1082 # (including devices that only support white) this value
1083 # specifies if this command is to set white value (0f) or the rgb
1084 # value (f0).
1085 #
1086 # For all other rgb and rgbw devices, the value is 00
1087 return [
1088 self.construct_message(
1089 bytearray(
1090 [
1091 0x31 if persist else 0x41,
1092 red or 0x00,
1093 green or 0x00,
1094 blue or 0x00,
1095 warm_white or 0x00,
1096 write_mode.value
1097 if isinstance(write_mode, LevelWriteMode)
1098 else write_mode,
1099 0x0F,
1100 ]
1101 )
1102 )
1103 ]
1104
1105 def construct_message(self, raw_bytes: bytearray) -> bytearray:
1106 """Calculate checksum of byte array and add to end."""
1107 csum = sum(raw_bytes) & 0xFF
1108 raw_bytes.append(csum)
1109 return raw_bytes
1110
1111 def construct_state_query(self) -> bytearray:
1112 """The bytes to send for a query request."""
1113 return self.construct_message(bytearray([0x81, 0x8A, 0x8B]))
1114
1115 def named_raw_state(self, raw_state: bytes) -> LEDENETRawState:
1116 """Convert raw_state to a namedtuple."""
1117 return LEDENETRawState(*raw_state)
1118
1119 def construct_music_mode(
1120 self,
1121 sensitivity: int,
1122 brightness: int,
1123 mode: int | None,
1124 effect: int | None,
1125 foreground_color: tuple[int, int, int] | None = None,
1126 background_color: tuple[int, int, int] | None = None,
1127 ) -> list[bytearray]:
1128 """
1129 The bytes to send for music mode.
1130
1131 Known messages
1132 73 01 4d 0f d0
1133 ^^
1134 Likely sensitivity from 0-100 (0x64)
1135 73 01 64 0f e7
1136 73 01 4a 0f cd
1137 73 01 4b 0f ce
1138 73 01 00 0f 83
1139 73 01 1b 0f 9e
1140 73 01 05 0f 88
1141 73 01 02 0f 85
1142 73 01 06 0f 89
1143 73 01 05 0f 88
1144 73 01 10 0f 93
1145 73 01 4d 0f d0
1146 73 01 64 0f e7
1147
1148 Pause music mode
1149 73 00 59 0f db
1150 ^^
1151 On/off byte
1152
1153 Mic
1154 37 00 00 37 Fade In
1155 ^^
1156 Mic effect
1157 37 01 00 38 Gradual
1158 37 02 00 39 Jump
1159 37 03 00 3a Strobe
1160 """
1161 # Valid modes for old protocol
1162 # 0x01 - Gradual
1163 return [self.construct_message(bytearray([0x73, 0x01, sensitivity, 0x0F]))]
1164
1165 def construct_device_config(
1166 self,
1167 operating_mode: int | None,
1168 wiring: int | None,
1169 ic_type: int | None, # ic type
1170 pixels_per_segment: int | None, # pixels per segment
1171 segments: int | None, # number of segments
1172 music_pixels_per_segment: int | None, # music pixels per segment
1173 music_segments: int | None, # number of music segments
1174 ) -> bytearray:
1175 """
1176 The bytes to send to change device config.
1177
1178 RGBW 0x06
1179 62 06 02 0f 79 - RGB/W GRB W
1180 62 04 02 0f 77 - RGB&W GRB W
1181 62 04 01 0f 77 - RGB&W RGB W
1182 62 04 03 0f 77 - RGB&W BRG W
1183
1184 RGBCW 0x07
1185
1186 62 05 0f 0f 85 - RGB&CCT / WCBRG
1187 62 07 0f 0f 87 - RGB/CCT / WCBRG
1188 62 07 01 0f 79 - RGB/CCT / RGBCW
1189 62 07 02 0f 7a - RGB/CCT / GRBCW
1190 62 07 0c 0f 84 - RGB/CCT / CBRGW
1191
1192 RGB 0x33 / 0x08
1193
1194 62 00 01 0f 73 - RGB
1195 62 00 02 0f 73 - GRB
1196 62 00 03 0f 73 - BRG
1197
1198 0x25
1199 62 01 0f 72 - DIM
1200 62 02 0f 73 - CCT
1201 62 03 0f 74 - RGB
1202 62 04 0f 74 - RGB&W
1203 62 05 0f 74 - RGB&CCT
1204 """
1205 msg = bytearray([0x62, operating_mode or 0x00])
1206 if wiring:
1207 msg.append(wiring)
1208 msg.append(0x0F)
1209 return self.construct_message(msg)
1210
1211
1212 class ProtocolLEDENET8ByteAutoOn(ProtocolLEDENET8Byte):
1213 """Protocol that uses 8 bytes, and turns on by changing levels or effects."""
1214
1215 @property
1216 def requires_turn_on(self) -> bool:
1217 """If True the device must be turned on before setting level/patterns/modes."""
1218 return False
1219
1220 @property
1221 def name(self) -> str:
1222 """The name of the protocol."""
1223 return PROTOCOL_LEDENET_8BYTE_AUTO_ON
1224
1225
1226 # This protocol also supports Candle mode but its not currently implemented here
1227 class ProtocolLEDENET8ByteDimmableEffects(ProtocolLEDENET8ByteAutoOn):
1228 """Protocol that uses 8 bytes, and supports dimmable effects and auto on by changing levels or effects."""
1229
1230 @property
1231 def dimmable_effects(self) -> bool:
1232 """Protocol supports dimmable effects."""
1233 return True
1234
1235 @property
1236 def power_push_updates(self) -> bool:
1237 """If True the protocol pushes power state updates when controlled via ir/rf/app."""
1238 return True
1239
1240 @property
1241 def state_push_updates(self) -> bool:
1242 """If True the protocol pushes state updates when controlled via ir/rf/app."""
1243 return True
1244
1245 @property
1246 def name(self) -> str:
1247 """The name of the protocol."""
1248 return PROTOCOL_LEDENET_8BYTE_DIMMABLE_EFFECTS
1249
1250 def construct_preset_pattern(
1251 self, pattern: int, speed: int, brightness: int
1252 ) -> bytearray:
1253 """The bytes to send for a preset pattern."""
1254 delay = utils.speedToDelay(speed)
1255 return self.construct_message(bytearray([0x38, pattern, delay, brightness]))
1256
1257 def construct_music_mode(
1258 self,
1259 sensitivity: int,
1260 brightness: int,
1261 mode: int | None,
1262 effect: int | None,
1263 foreground_color: tuple[int, int, int] | None = None,
1264 background_color: tuple[int, int, int] | None = None,
1265 ) -> list[bytearray]:
1266 """
1267 The bytes to send for music mode.
1268
1269 Known messages
1270 73 01 4d 0f d0
1271 ^^
1272 Likely sensitivity from 0-100 (0x64)
1273 73 01 64 0f e7
1274 73 01 4a 0f cd
1275 73 01 4b 0f ce
1276 73 01 00 0f 83
1277 73 01 1b 0f 9e
1278 73 01 05 0f 88
1279 73 01 02 0f 85
1280 73 01 06 0f 89
1281 73 01 05 0f 88
1282 73 01 10 0f 93
1283 73 01 4d 0f d0
1284 73 01 64 0f e7
1285
1286 Pause music mode
1287 73 00 59 0f db
1288 ^^
1289 On/off byte
1290
1291 Mic
1292 37 00 00 37 Fade In
1293 ^^
1294 Mic effect
1295 37 01 00 38 Gradual
1296 37 02 00 39 Jump
1297 37 03 00 3a Strobe
1298 """
1299 # Valid effect
1300 # 0x00 - Fade In
1301 # 0x01 - Gradual
1302 # 0x02 - Jump
1303 # 0x03 - Strobe
1304 if effect and not (0x00 <= effect <= 0x03):
1305 raise ValueError(
1306 "Mode must be one of (0x00 - Fade In, 0x01 - Gradual, 0x02 - Jump, 0x03 - Strobe)"
1307 )
1308 return [
1309 self.construct_message(bytearray([0x73, 0x01, sensitivity, 0x0F])),
1310 self.construct_message(bytearray([0x37, effect or 0x00, 0x00])),
1311 ]
1312
1313
1314 class ProtocolLEDENET9Byte(ProtocolLEDENET8Byte):
1315 """The newer LEDENET protocol with checksums that uses 9 bytes to set state."""
1316
1317 @property
1318 def name(self) -> str:
1319 """The name of the protocol."""
1320 return PROTOCOL_LEDENET_9BYTE
1321
1322 @property
1323 def timer_response_len(self) -> int:
1324 """Return the time response len."""
1325 return LEDENET_TIMERS_9BYTE_RESPONSE_LEN
1326
1327 @property
1328 def timer_len(self) -> int:
1329 """Return a single timer len."""
1330 return 15
1331
1332 def construct_levels_change(
1333 self,
1334 persist: int,
1335 red: int | None,
1336 green: int | None,
1337 blue: int | None,
1338 warm_white: int | None,
1339 cool_white: int | None,
1340 write_mode: LevelWriteMode | int,
1341 ) -> list[bytearray]:
1342 """The bytes to send for a level change request."""
1343 # sample message for 9-byte LEDENET protocol (w/ checksum at end)
1344 # 0 1 2 3 4 5 6 7
1345 # 31 bc c1 ff 00 00 f0 0f
1346 # | | | | | | | |
1347 # | | | | | | | terminator
1348 # | | | | | | write mode (f0 colors, 0f whites, 00 colors & whites)
1349 # | | | | | cold white
1350 # | | | | warm white
1351 # | | | blue
1352 # | | green
1353 # | red
1354 # persistence (31 for true / 41 for false)
1355 #
1356 return [
1357 self.construct_message(
1358 bytearray(
1359 [
1360 0x31 if persist else 0x41,
1361 red or 0x00,
1362 green or 0x00,
1363 blue or 0x00,
1364 warm_white or 0x00,
1365 cool_white or 0x00,
1366 write_mode.value
1367 if isinstance(write_mode, LevelWriteMode)
1368 else write_mode,
1369 0x0F,
1370 ]
1371 )
1372 )
1373 ]
1374
1375
1376 class ProtocolLEDENET9ByteAutoOn(ProtocolLEDENET9Byte):
1377 """Protocol that uses 9 bytes, and turns on by changing levels or effects."""
1378
1379 @property
1380 def requires_turn_on(self) -> bool:
1381 """If True the device must be turned on before setting level/patterns/modes."""
1382 return False
1383
1384 @property
1385 def name(self) -> str:
1386 """The name of the protocol."""
1387 return PROTOCOL_LEDENET_9BYTE_AUTO_ON
1388
1389
1390 # This protocol also supports Candle mode but its not currently implemented here
1391 class ProtocolLEDENET9ByteDimmableEffects(ProtocolLEDENET9ByteAutoOn):
1392 """The newer LEDENET protocol with checksums that uses 9 bytes to set state."""
1393
1394 @property
1395 def dimmable_effects(self) -> bool:
1396 """Protocol supports dimmable effects."""
1397 return True
1398
1399 @property
1400 def power_push_updates(self) -> bool:
1401 """If True the protocol pushes power state updates when controlled via ir/rf/app."""
1402 return True
1403
1404 @property
1405 def state_push_updates(self) -> bool:
1406 """If True the protocol pushes state updates when controlled via ir/rf/app."""
1407 return True
1408
1409 @property
1410 def name(self) -> str:
1411 """The name of the protocol."""
1412 return PROTOCOL_LEDENET_9BYTE_DIMMABLE_EFFECTS
1413
1414 def construct_preset_pattern(
1415 self, pattern: int, speed: int, brightness: int
1416 ) -> bytearray:
1417 """The bytes to send for a preset pattern."""
1418 delay = utils.speedToDelay(speed)
1419 return self.construct_message(bytearray([0x38, pattern, delay, brightness]))
1420
1421
1422 class ProtocolLEDENET25Byte(ProtocolLEDENET9Byte):
1423 """25 byte protocol"""
1424
1425 level_write_modes = LevelWriteModeData(ALL=0x00, COLORS=0xA1, WHITES=0xB1)
1426
1427 @property
1428 def speed_is_delay(self) -> bool:
1429 """If True the speed is a delay."""
1430 return False
1431
1432 @property
1433 def name(self) -> str:
1434 """The name of the protocol."""
1435 return PROTOCOL_LEDENET_25BYTE
1436
1437 def is_valid_extended_state_response(self, raw_state: bytes) -> bool:
1438 """Check if a state response is valid."""
1439 return raw_state[0] == 0xEA and raw_state[1] == 0x81 and len(raw_state) >= 20
1440
1441 def extended_state_to_state(self, raw_state: bytes) -> bytes:
1442 """Convert an extended state response to a state response."""
1443 # pos 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1444 # EA 81 01 10 35 0A 23 61 01 50 0F 3C 64 64 00 64 00 00 00 00
1445 # | | | | | | | | | | | | | | | | | | | |
1446 # | | | | | | | | | | | | | | | | | | | ??
1447 # | | | | | | | | | | | | | | | | | | ??
1448 # | | | | | | | | | | | | | | | | | ??
1449 # | | | | | | | | | | | | | | | | ??
1450 # | | | | | | | | | | | | | | | White brightness
1451 # | | | | | | | | | | | | | | White temperature
1452 # | | | | | | | | | | | | | Value
1453 # | | | | | | | | | | | | Saturation
1454 # | | | | | | | | | | | Hue / 2 (0-180)
1455 # | | | | | | | | | | 0f white / f0 rgb
1456 # | | | | | | | | | Speed?
1457 # | | | | | | | | w 01 / rgb 00
1458 # | | | | | | | ??
1459 # | | | | | | Power state (0x23 = ON, 0x24 = OFF)
1460 # | | | | | ??
1461 # | | | | Version number
1462 # | | | Model number
1463 # | | Unknown / reserved
1464 # | Unknown / reserved
1465 # Extended message header (ea 81)
1466
1467 if len(raw_state) < 20:
1468 return b""
1469
1470 model_num = raw_state[4]
1471 version_number = raw_state[5]
1472 power_state = raw_state[6]
1473 preset_pattern = raw_state[7]
1474 speed = raw_state[9]
1475
1476 hue = raw_state[11]
1477 saturation = raw_state[12]
1478 value = raw_state[13]
1479
1480 white_temp = raw_state[14]
1481 white_brightness = raw_state[15]
1482 levels = scaled_color_temp_to_white_levels(white_temp, white_brightness)
1483
1484 cool_white = levels.cool_white
1485 warm_white = levels.warm_white
1486
1487 # Convert HSV to RGB
1488 h = (hue * 2) / 360
1489 s = saturation / 100
1490 v = value / 100
1491 r_f, g_f, b_f = colorsys.hsv_to_rgb(h, s, v)
1492 red = min(int(max(0, r_f) * 255), 255)
1493 green = min(int(max(0, g_f) * 255), 255)
1494 blue = min(int(max(0, b_f) * 255), 255)
1495
1496 # Fill standard state structure
1497 mode = 0
1498 color_mode = 0
1499 check_sum = 0 # Set to 0; not critical
1500
1501 return bytes(
1502 (
1503 raw_state[1], # Head (second byte of EA 81)
1504 model_num,
1505 power_state,
1506 preset_pattern,
1507 mode,
1508 speed,
1509 red,
1510 green,
1511 blue,
1512 warm_white,
1513 version_number,
1514 cool_white,
1515 color_mode,
1516 check_sum,
1517 )
1518 )
1519
1520 def construct_levels_change(
1521 self,
1522 persist: int,
1523 red: int | None, # 0-255
1524 green: int | None, # 0-255
1525 blue: int | None, # 0-255
1526 warm_white: int | None, # 0-255
1527 cool_white: int | None, # 0-255
1528 write_mode: LevelWriteMode | int,
1529 ) -> list[bytearray]:
1530 """The bytes to send for a level change request."""
1531 # sample message for 25-byte LEDENET protocol (w/ checksum at end)
1532 # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
1533 # b0 b1 b2 b3 00 01 02 79 00 0e e0 01 00 a1 78 64 64 00 00 00 00 14 00 00
1534 # | | | | | | | | | |
1535 # | | | | | | | | | footer
1536 # | | | | | | | | white brightness
1537 # | | | | | | | white temperature (00 warm - 64 cool)
1538 # | | | | | | value
1539 # | | | | | saturation
1540 # | | | | hue / 2
1541 # | | | write mode (a1 color, b1 white)
1542 # | | unknown filler
1543 # | increment counter
1544 # header
1545
1546 if red is not None and green is not None and blue is not None:
1547 h, s, v = colorsys.rgb_to_hsv(red / 255, green / 255, blue / 255)
1548 h = int((h * 360) / 2) # Hue needs to be halved
1549 s = int(s * 100)
1550 v = int(v * 100)
1551 else:
1552 h = s = v = 0x00
1553
1554 if (
1555 cool_white is None
1556 or warm_white is None
1557 or (cool_white == 0 and warm_white == 0)
1558 ):
1559 white_temp = white_brightness = 0
1560 else:
1561 total = warm_white + cool_white
1562 # temperature: ratio of cool to total, scaled to 0-100
1563 white_temp = round((cool_white / float(total)) * 100)
1564 # brightness: clamp sum at 255, then scale to 0-100
1565 clamped_sum = min(total, 255)
1566 white_brightness = round((clamped_sum / 255.0) * 100)
1567
1568 return [
1569 self.construct_wrapped_message(
1570 bytearray(
1571 [
1572 0xE0,
1573 0x01,
1574 0x00,
1575 write_mode.value
1576 if isinstance(write_mode, LevelWriteMode)
1577 else write_mode,
1578 h,
1579 s,
1580 v,
1581 white_temp,
1582 white_brightness,
1583 0x00,
1584 0x00,
1585 0x14,
1586 0x00,
1587 0x00,
1588 ]
1589 ),
1590 inner_pre_constructed=True,
1591 version=0x02,
1592 )
1593 ]
1594
1595
1596 class ProtocolLEDENETAddressableBase(ProtocolLEDENET9Byte):
1597 """Base class for addressable protocols."""
1598
1599 @property
1600 def timer_response_len(self) -> int:
1601 """Return the time response len."""
1602 return LEDENET_TIMERS_8BYTE_RESPONSE_LEN
1603
1604 @property
1605 def timer_len(self) -> int:
1606 """Return a single timer len."""
1607 return 14
1608
1609 @property
1610 def speed_is_delay(self) -> bool:
1611 """If True the speed is a delay."""
1612 return False
1613
1614
1615 class ProtocolLEDENETAddressableA1(ProtocolLEDENETAddressableBase):
1616 def construct_request_strip_setting(self) -> bytearray:
1617 return bytearray([0x63, 0x12, 0x21, 0x36])
1618
1619 @property
1620 def name(self) -> str:
1621 """The name of the protocol."""
1622 return PROTOCOL_LEDENET_ADDRESSABLE_A1
1623
1624 def is_valid_device_config_response(self, data: bytes) -> bool:
1625 """Check if a message is a valid ic state response."""
1626 return (
1627 len(data) == LEDENET_A1_DEVICE_CONFIG_RESPONSE_LEN
1628 and _message_type_from_start_of_msg(data) == MSG_A1_DEVICE_CONFIG
1629 and self.is_checksum_correct(data)
1630 )
1631
1632 @property
1633 def power_push_updates(self) -> bool:
1634 """If True the protocol pushes power state updates when controlled via ir/rf/app."""
1635 return True
1636
1637 @property
1638 def dimmable_effects(self) -> bool:
1639 """Protocol supports dimmable effects."""
1640 return False
1641
1642 @property
1643 def requires_turn_on(self) -> bool:
1644 """If True the device must be turned on before setting level/patterns/modes."""
1645 return False
1646
1647 def construct_preset_pattern(
1648 self, pattern: int, speed: int, brightness: int
1649 ) -> bytearray:
1650 """The bytes to send for a preset pattern."""
1651 effect = pattern + 99
1652 return self.construct_message(
1653 bytearray([0x61, effect >> 8, effect & 0xFF, speed, 0x0F])
1654 )
1655
1656 def parse_strip_setting(self, msg: bytes) -> LEDENETAddressableDeviceConfiguration:
1657 """Parse a strip settings message."""
1658 # pos 0 1 2 3 4 5 6 7 8 9 10 11
1659 # 63 00 32 05 00 00 00 00 00 00 02 9c
1660 # | | | | | | | | | | | |
1661 # | | | | | | | | | | | checksum
1662 # | | | | | | | | | | wiring type (0 indexed, RGB or RGBW)
1663 # | | | | | | | | | ?? always 00
1664 # | | | | | | | | ?? always 00
1665 # | | | | | | | n?? always 00
1666 # | | | | | | ?? always 00
1667 # | | | | | ?? always 00
1668 # | | | | ?? always 00
1669 # | | | ic type (01=UCS1903, 02=SM16703, 03=WS2811, 04=WS2812B, 05=SK6812, 06=INK1003, 07=WS2801, 08=LB1914)
1670 # | | num pixels (16 bit, low byte)
1671 # | num pixels (16 bit, high byte)
1672 # msg head
1673 #
1674 high_byte = msg[1]
1675 low_byte = msg[2]
1676 pixels_per_segment = (high_byte << 8) + low_byte
1677 _LOGGER.debug(
1678 "Pixel count (high: %s, low: %s) is: %s",
1679 hex(high_byte),
1680 hex(low_byte),
1681 pixels_per_segment,
1682 )
1683 return LEDENETAddressableDeviceConfiguration(
1684 pixels_per_segment=pixels_per_segment,
1685 segments=None,
1686 music_pixels_per_segment=None,
1687 music_segments=None,
1688 wirings=list(ADDRESSABLE_RGB_WIRING_TO_NUM),
1689 wiring_num=msg[10],
1690 wiring=ADDRESSABLE_RGB_NUM_TO_WIRING.get(msg[10]),
1691 num_to_wiring=ADDRESSABLE_RGB_NUM_TO_WIRING,
1692 wiring_to_num=ADDRESSABLE_RGB_WIRING_TO_NUM,
1693 ic_type=A1_NUM_TO_PROTOCOL.get(msg[3]),
1694 ic_type_num=msg[3],
1695 operating_mode=A1_NUM_TO_OPERATING_MODE.get(msg[3]),
1696 )
1697
1698 def construct_device_config(
1699 self,
1700 operating_mode: int | None,
1701 wiring: int | None,
1702 ic_type: int | None, # ic type
1703 pixels_per_segment: int | None, # pixels per segment
1704 segments: int | None, # number of segments
1705 music_pixels_per_segment: int | None, # music pixels per segment
1706 music_segments: int | None, # number of music segments
1707 ) -> bytearray:
1708 """
1709 The bytes to send to change device config.
1710 pos 0 1 2 3 4 5 6 7 8 9 10 11 12
1711 62 04 00 04 00 00 00 00 00 00 02 f0 5c <- checksum
1712 | | | | | | | | | | | |
1713 | | | | | | | | | | | always 0xf0
1714 | | | | | | | | | | wiring type (0 indexed, RGB or RGBW)
1715 | | | | | | | | | ?? always 00
1716 | | | | | | | | ?? always 00
1717 | | | | | | | n?? always 00
1718 | | | | | | ?? always 00
1719 | | | | | ?? always 00
1720 | | | | ?? always 00
1721 | | | ic type (01=UCS1903, 02=SM16703, 03=WS2811, 04=WS2812B, 05=SK6812, 06=INK1003, 07=WS2801, 08=LB1914)
1722 | | num pixels (16 bit, low byte)
1723 | num pixels (16 bit, high byte)
1724 msg head
1725
1726 """
1727 assert ic_type is not None
1728 assert pixels_per_segment is not None
1729 assert wiring is not None
1730 return self.construct_message(
1731 bytearray(
1732 [
1733 0x62,
1734 pixels_per_segment >> 8,
1735 pixels_per_segment & 0xFF,
1736 ic_type,
1737 0x00,
1738 0x00,
1739 0x00,
1740 0x00,
1741 0x00,
1742 0x00,
1743 wiring,
1744 0xF0,
1745 ]
1746 )
1747 )
1748
1749
1750 class ProtocolLEDENETAddressableA2(ProtocolLEDENETAddressableBase):
1751 # ic response
1752 # 0x96 0x63 0x00 0x32 0x00 0x01 0x01 0x04 0x32 0x01 0x64 (11)
1753 def construct_request_strip_setting(self) -> bytearray:
1754 return self.construct_message(bytearray([0x63, 0x12, 0x21, 0x0F]))
1755
1756 @property
1757 def name(self) -> str:
1758 """The name of the protocol."""
1759 return PROTOCOL_LEDENET_ADDRESSABLE_A2
1760
1761 @property
1762 def power_push_updates(self) -> bool:
1763 """If True the protocol pushes power state updates when controlled via ir/rf/app."""
1764 # This is likely due to buggy firmware
1765 return False
1766
1767 @property
1768 def dimmable_effects(self) -> bool:
1769 """Protocol supports dimmable effects."""
1770 return True
1771
1772 @property
1773 def requires_turn_on(self) -> bool:
1774 """If True the device must be turned on before setting level/patterns/modes."""
1775 return False
1776
1777 def is_valid_device_config_response(self, data: bytes) -> bool:
1778 """Check if a message is a valid ic state response."""
1779 return (
1780 len(data) == LEDENET_DEVICE_CONFIG_RESPONSE_LEN
1781 and _message_type_from_start_of_msg(data) == MSG_DEVICE_CONFIG
1782 and self.is_checksum_correct(data)
1783 )
1784
1785 def construct_preset_pattern(
1786 self, pattern: int, speed: int, brightness: int
1787 ) -> bytearray:
1788 """The bytes to send for a preset pattern."""
1789 return self.construct_message(bytearray([0x42, pattern, speed, brightness]))
1790
1791 def construct_levels_change(
1792 self,
1793 persist: int,
1794 red: int | None,
1795 green: int | None,
1796 blue: int | None,
1797 warm_white: int | None,
1798 cool_white: int | None,
1799 write_mode: LevelWriteMode | int,
1800 ) -> list[bytearray]:
1801 """
1802 The bytes to send for a level change request.
1803
1804 white 41 01 ff ff ff 00 00 00 60 ff 00 00 9e
1805 """
1806 preset_number = 0x01 # aka fixed color
1807 msgs = []
1808 if red is not None or green is not None or blue is not None:
1809 msgs.append(
1810 self.construct_message(
1811 bytearray(
1812 [
1813 0x41,
1814 preset_number,
1815 red or 0x00,
1816 green or 0x00,
1817 blue or 0x00,
1818 0x00,
1819 0x00,
1820 0x00,
1821 0x60,
1822 0xFF,
1823 0x00,
1824 0x00,
1825 ]
1826 )
1827 )
1828 )
1829 if warm_white is not None:
1830 msgs.append(self.construct_message(bytearray([0x47, warm_white or 0x00])))
1831 return msgs
1832
1833 def construct_music_mode(
1834 self,
1835 sensitivity: int,
1836 brightness: int,
1837 mode: int | None,
1838 effect: int | None,
1839 foreground_color: tuple[int, int, int] | None = None,
1840 background_color: tuple[int, int, int] | None = None,
1841 ) -> list[bytearray]:
1842 """
1843 The bytes to send for music mode.
1844
1845 Known messages
1846 73 01 27 01 00 00 00 00 ff ff 64 64 62 - lowest brightness music
1847 73 01 27 01 ff ff ff 00 ff ff 64 64 5f - highest brightness music
1848 ^R ^G ^B <-- failling color
1849 73 01 27 01 ff 00 00 00 ff ff 64 64 61
1850 ^R ^G ^B <-- failling color
1851
1852
1853 73 01 27 01 ff ff ff 00 ff ff 00 64 fb - lowest sensitivity
1854 73 01 27 01 ff ff ff 00 ff ff 64 64 5f - highest sensitivity
1855 ^ sensitivity
1856
1857
1858 73 01 27 13 00 ff 19 ff 00 00 64 64 8d
1859 ^R ^G ^B <-- failling color (light screen mode)
1860 73 01 27 13 00 ff 19 ff 00 00 64 64 8d
1861 ^R ^G ^B <-- column color (light screen mode)
1862
1863 73 01 27 14 00 ff 19 ff 00 00 64 64 8e
1864 ^ effect
1865 73 01 27 15 00 ff 19 ff 00 00 64 64 8f
1866 ^ effect
1867
1868 73 01 27 15 00 ff 19 ff 00 00 64 64 8f
1869 ^ mode - light screen mode
1870 73 01 26 01 00 00 00 00 ff ff 64 64 61
1871 ^ mode - led strip mode
1872
1873 73 01 26 0e 00 00 00 ff 00 00 64 64 6f
1874 ^R ^G ^B <-- led strip mode color
1875
1876 73 01 26 0e 00 00 00 ff 00 00 64 06 11
1877 ^brightness <-- led strip mode color
1878
1879 """
1880 if foreground_color is None:
1881 foreground_color = (0xFF, 0xFF, 0xFF)
1882 if background_color is None:
1883 background_color = (0x00, 0x00, 0x00)
1884 if effect and not (1 <= effect <= 16):
1885 raise ValueError("Effect must be between 1 and 16")
1886 if mode and not (0x26 <= mode <= 0x27):
1887 raise ValueError("Mode must be between 0x26 and 0x27")
1888
1889 return [
1890 self.construct_message(
1891 bytearray(
1892 [
1893 0x73,
1894 0x01,
1895 mode
1896 or MusicMode.STRIP.value, # strip mode 0x26, light screen mode 0x27
1897 effect or 0x01,
1898 *foreground_color,
1899 *background_color,
1900 sensitivity,
1901 brightness,
1902 ]
1903 )
1904 )
1905 ]
1906
1907 def parse_strip_setting(self, msg: bytes) -> LEDENETAddressableDeviceConfiguration:
1908 """Parse a strip settings message."""
1909 # pos 0 1 2 3 4 5 6 7 8 9 10
1910 # 00 63 01 2c 00 01 07 08 96 01 45
1911 # | | | | | | | | | | |
1912 # | | | | | | | | | | checksum
1913 # | | | | | | | | | |
1914 # | | | | | | | | | segments (music mode)
1915 # | | | | | | | | num pixels (music mode)
1916 # | | | | | | | wiring type (0 indexed, RGB or RGBW)
1917 # | | | | | | ic type (01=UCS1903, 02=SM16703, 03=WS2811, 04=WS2811B, 05=SK6812, 06=INK1003, 07=WS2801, 08=WS2815, 09=APA102, 10=TM1914, 11=UCS2904B)
1918 # | | | | | segments
1919 # | | | | ?? (always 0x00)
1920 # | | | num pixels (16 bit, low byte)
1921 # | | num pixels (16 bit, high byte)
1922 # | msg head
1923 # msg head
1924 #
1925 high_byte = msg[2]
1926 low_byte = msg[3]
1927 pixels_per_segment = (high_byte << 8) + low_byte
1928 _LOGGER.debug("bytes: %s", msg)
1929 _LOGGER.debug(
1930 "Pixel count (high: %s, low: %s) is: %s",
1931 hex(high_byte),
1932 hex(low_byte),
1933 pixels_per_segment,
1934 )
1935 segments = msg[5]
1936 _LOGGER.debug(
1937 "Segment count (%s) is: %s",
1938 hex(segments),
1939 segments,
1940 )
1941 return LEDENETAddressableDeviceConfiguration(
1942 pixels_per_segment=pixels_per_segment,
1943 segments=segments,
1944 music_pixels_per_segment=msg[8],
1945 music_segments=msg[9],
1946 wirings=list(ADDRESSABLE_RGB_NUM_TO_WIRING.values()),
1947 wiring_num=msg[7],
1948 wiring=ADDRESSABLE_RGB_NUM_TO_WIRING.get(msg[7]),
1949 num_to_wiring=ADDRESSABLE_RGB_NUM_TO_WIRING,
1950 wiring_to_num=ADDRESSABLE_RGB_WIRING_TO_NUM,
1951 ic_type=A2_NUM_TO_PROTOCOL.get(msg[6]),
1952 ic_type_num=msg[6],
1953 operating_mode=A2_NUM_TO_OPERATING_MODE.get(msg[6]),
1954 )
1955
1956 def construct_device_config(
1957 self,
1958 operating_mode: int | None,
1959 wiring: int | None,
1960 ic_type: int | None, # ic type
1961 pixels_per_segment: int | None, # pixels per segment
1962 segments: int | None, # number of segments
1963 music_pixels_per_segment: int | None, # music pixels per segment
1964 music_segments: int | None, # number of music segments
1965 ) -> bytearray:
1966 """
1967 The bytes to send to change device config.
1968 pos 0 1 2 3 4 5 6 7 8 9 10
1969 62 01 2c 00 06 01 04 32 01 0f dc
1970 | | | | | | | | | | |
1971 | | | | | | | | | | |
1972 | | | | | | | | | | checksum
1973 | | | | | | | | | ?? always 0x0f
1974 | | | | | | | | segments (music mode)
1975 | | | | | | | num pixels (music mode)
1976 | | | | | | wiring type (0 indexed, RGB or RGBW)
1977 | | | | | ic type (01=WS2812B, 02=SM16703, 03=SM16704, 04=WS2811, 05=UCS1903, 06=SK6812, 07=SK6812RGBW, 08=INK1003, 09=UCS2904B)
1978 | | | | segments
1979 | | | ?? always 00
1980 | | num pixels (16 bit, low byte)
1981 | num pixels (16 bit, high byte)
1982 msg head
1983
1984
1985 """
1986 assert ic_type is not None
1987 assert pixels_per_segment is not None
1988 assert segments is not None
1989 assert music_pixels_per_segment is not None
1990 assert music_segments is not None
1991 assert wiring is not None
1992 pixels_per_segment = max(1, min(pixels_per_segment, PIXELS_PER_SEGMENT_MAX))
1993 segments = max(1, min(segments, SEGMENTS_MAX))
1994 if pixels_per_segment * segments > PIXELS_MAX:
1995 segments = int(PIXELS_MAX / pixels_per_segment)
1996 music_pixels_per_segment = max(
1997 1, min(music_pixels_per_segment, MUSIC_PIXELS_PER_SEGMENT_MAX)
1998 )
1999 music_segments = max(1, min(music_segments, MUSIC_SEGMENTS_MAX))
2000 if music_pixels_per_segment * music_segments > MUSIC_PIXELS_MAX:
2001 music_segments = int(MUSIC_PIXELS_MAX / music_pixels_per_segment)
2002 if (
2003 pixels_per_segment <= MUSIC_PIXELS_PER_SEGMENT_MAX
2004 and segments <= MUSIC_SEGMENTS_MAX
2005 and pixels_per_segment * segments <= MUSIC_PIXELS_MAX
2006 ):
2007 # If the pixels_per_segment and segments can accomate music
2008 # mode then we sync them
2009 music_pixels_per_segment = pixels_per_segment
2010 music_segments = segments
2011 return self.construct_message(
2012 bytearray(
2013 [
2014 0x62,
2015 pixels_per_segment >> 8,
2016 pixels_per_segment & 0xFF,
2017 0x00,
2018 segments,
2019 ic_type,
2020 wiring,
2021 music_pixels_per_segment,
2022 music_segments,
2023 0xF0,
2024 ]
2025 )
2026 )
2027
2028
2029 class ProtocolLEDENETAddressableA3(ProtocolLEDENETAddressableA2):
2030 def construct_request_strip_setting(self) -> bytearray:
2031 return self.construct_wrapped_message(
2032 super().construct_request_strip_setting(),
2033 inner_pre_constructed=True,
2034 )
2035
2036 def construct_state_query(self) -> bytearray:
2037 """The bytes to send for a query request."""
2038 return self.construct_wrapped_message(
2039 super().construct_state_query(),
2040 inner_pre_constructed=True,
2041 )
2042
2043 def construct_state_change(self, turn_on: int) -> bytearray:
2044 """The bytes to send for a state change request."""
2045 return self.construct_wrapped_message(
2046 super().construct_state_change(turn_on),
2047 inner_pre_constructed=True,
2048 )
2049
2050 # ic response
2051 # 0x00 0x63 0x00 0x32 0x00 0x01 0x04 0x03 0x32 0x01 0xD0 (11)
2052 # b0 b1 b2 b3 00 01 01 37 00 0b 00 63 00 32 00 01 04 03 32 01 d0 aa
2053 @property
2054 def power_push_updates(self) -> bool:
2055 """If True the protocol pushes power state updates when controlled via ir/rf/app."""
2056 return True
2057
2058 @property
2059 def state_push_updates(self) -> bool:
2060 """If True the protocol pushes state updates when controlled via ir/rf/app."""
2061 return True
2062
2063 @property
2064 def zones(self) -> bool:
2065 """If the protocol supports zones."""
2066 return True
2067
2068 @property
2069 def name(self) -> str:
2070 """The name of the protocol."""
2071 return PROTOCOL_LEDENET_ADDRESSABLE_A3
2072
2073 @property
2074 def dimmable_effects(self) -> bool:
2075 """Protocol supports dimmable effects."""
2076 return True
2077
2078 @property
2079 def requires_turn_on(self) -> bool:
2080 """If True the device must be turned on before setting level/patterns/modes."""
2081 return False
2082
2083 def construct_preset_pattern(
2084 self, pattern: int, speed: int, brightness: int
2085 ) -> bytearray:
2086 """The bytes to send for a preset pattern."""
2087 return self.construct_wrapped_message(
2088 super().construct_preset_pattern(pattern, speed, brightness),
2089 inner_pre_constructed=True,
2090 )
2091
2092 def parse_strip_setting(self, msg: bytes) -> LEDENETAddressableDeviceConfiguration:
2093 """Parse a strip settings message."""
2094 # pos 0 1 2 3 4 5 6 7 8 9 10
2095 # 00 63 01 2c 00 01 07 08 96 01 45
2096 # | | | | | | | | | | |
2097 # | | | | | | | | | | checksum
2098 # | | | | | | | | | |
2099 # | | | | | | | | | segments (music mode)
2100 # | | | | | | | | num pixels (music mode)
2101 # | | | | | | | wiring type (0 indexed, RGB or RGBW)
2102 # | | | | | | ic type (01=WS2812B, 02=SM16703, 03=SM16704, 04=WS2811, 05=UCS1903, 06=SK6812, 07=SK6812RGBW, 08=INK1003, 09=UCS2904B)
2103 # | | | | | segments
2104 # | | | | ?? (always 0x00)
2105 # | | | num pixels (16 bit, low byte)
2106 # | | num pixels (16 bit, high byte)
2107 # | msg head
2108 # msg head
2109 #
2110 high_byte = msg[2]
2111 low_byte = msg[3]
2112 pixels_per_segment = (high_byte << 8) + low_byte
2113 _LOGGER.debug("bytes: %s", msg)
2114 _LOGGER.debug(
2115 "Pixel count (high: %s, low: %s) is: %s",
2116 hex(high_byte),
2117 hex(low_byte),
2118 pixels_per_segment,
2119 )
2120 segments = msg[5]
2121 _LOGGER.debug(
2122 "Segment count (%s) is: %s",
2123 hex(segments),
2124 segments,
2125 )
2126 if NEW_ADDRESSABLE_NUM_TO_OPERATING_MODE.get(msg[6]) == COLOR_MODE_RGBW:
2127 wirings = ADDRESSABLE_RGBW_NUM_TO_WIRING
2128 num_to_wiring = ADDRESSABLE_RGBW_NUM_TO_WIRING
2129 wiring_to_num = ADDRESSABLE_RGBW_WIRING_TO_NUM
2130 else:
2131 wirings = ADDRESSABLE_RGB_NUM_TO_WIRING
2132 num_to_wiring = ADDRESSABLE_RGB_NUM_TO_WIRING
2133 wiring_to_num = ADDRESSABLE_RGB_WIRING_TO_NUM
2134 return LEDENETAddressableDeviceConfiguration(
2135 pixels_per_segment=pixels_per_segment,
2136 segments=segments,
2137 music_pixels_per_segment=msg[8],
2138 music_segments=msg[9],
2139 wirings=list(wirings.values()),
2140 wiring_num=msg[7],
2141 wiring=wirings.get(msg[7]),
2142 ic_type=NEW_ADDRESSABLE_NUM_TO_PROTOCOL.get(msg[6]),
2143 num_to_wiring=num_to_wiring,
2144 wiring_to_num=wiring_to_num,
2145 ic_type_num=msg[6],
2146 operating_mode=NEW_ADDRESSABLE_NUM_TO_OPERATING_MODE.get(msg[6]),
2147 )
2148
2149 # To query music mode
2150 # Send -> b0 b1 b2 b3 00 01 01 1c 00 03 72 00 72 cb
2151 # Responds <- b0 b1 b2 b3 00 01 01 1c 00 0d 72 01 26 01 00 00 00 00 00 00 64 64 62 b5
2152
2153 def construct_music_mode(
2154 self,
2155 sensitivity: int,
2156 brightness: int,
2157 mode: int | None,
2158 effect: int | None,
2159 foreground_color: tuple[int, int, int] | None = None,
2160 background_color: tuple[int, int, int] | None = None,
2161 ) -> list[bytearray]:
2162 """
2163 The bytes to send for music mode.
2164
2165 Known messages
2166 b0 b1 b2 b3 00 01 01 1f 00 0d 73 01 27 01 ff 00 00 ff 00 00 64 64 62 b8 - Music mode
2167 b0 b1 b2 b3 00 01 01 20 00 0d 73 01 27 01 00 ff 44 ff 00 00 64 64 a6 41 - Music mode
2168 b0 b1 b2 b3 00 01 01 21 00 0d 73 01 27 01 ff a6 00 ff 00 00 64 64 08 06 - Music mode
2169 b0 b1 b2 b3 00 01 01 22 00 0d 73 01 27 01 ff a6 00 ff 00 00 2e 64 d2 9b - Music mode
2170
2171 b0 b1 b2 b3 00 01 01 2d 00 0d 73 01 27 01 ff a6 00 ff 00 00 4e 64 f2 e6 - Music mode (various sensitivity)
2172 b0 b1 b2 b3 00 01 01 2e 00 0d 73 01 27 01 ff a6 00 ff 00 00 5f 64 03 09 - Music mode (various sensitivity)
2173 b0 b1 b2 b3 00 01 01 2f 00 0d 73 01 27 01 ff a6 00 ff 00 00 64 64 08 14 - Music mode (various sensitivity)
2174 b0 b1 b2 b3 00 01 01 30 00 0d 73 01 27 01 ff a6 00 ff 00 00 37 64 db bb - Music mode (various sensitivity)
2175 ^^
2176 Likely sensitivity from 0-100 (0x64)
2177 b0 b1 b2 b3 00 01 01 60 00 0d 73 01 27 01 ff a6 00 ff 00 00 64 64 08 45 - Music mode (various sensitivity)
2178 b0 b1 b2 b3 00 01 01 5f 00 0d 73 01 27 01 ff a6 00 ff 00 00 64 64 08 44 - Music mode (various sensitivity)
2179 b0 b1 b2 b3 00 01 01 69 00 0d 73 01 26 01 ff 00 00 ff 00 00 00 64 fd 38 - Music mode (various sensitivity)
2180 b0 b1 b2 b3 00 01 01 68 00 0d 73 01 26 01 ff 00 00 ff 00 00 64 64 61 ff - Music mode (various sensitivity)
2181
2182
2183 b0 b1 b2 b3 00 01 01 08 00 0d 73 01 26 02 00 00 00 00 ff ff 64 60 5e 99 -- red lines
2184 b0 b1 b2 b3 00 01 01 16 00 0d 73 01 26 02 00 00 00 00 ff ff 64 64 62 af -- red lines
2185 b0 b1 b2 b3 00 01 01 17 00 0d 73 01 26 01 00 00 00 00 ff ff 64 64 61 ae -- rainbow lines
2186 ^^
2187 Likely brightness from 0-100 (0x64)
2188 """
2189 return [
2190 self.construct_wrapped_message(msg, inner_pre_constructed=True)
2191 for msg in super().construct_music_mode(
2192 sensitivity,
2193 brightness,
2194 mode,
2195 effect,
2196 foreground_color,
2197 background_color,
2198 )
2199 ]
2200
2201 def construct_levels_change(
2202 self,
2203 persist: int,
2204 red: int | None,
2205 green: int | None,
2206 blue: int | None,
2207 warm_white: int | None,
2208 cool_white: int | None,
2209 write_mode: LevelWriteMode | int,
2210 ) -> list[bytearray]:
2211 """
2212 The bytes to send for a level change request.
2213
2214 b0 [unknown static?] b1 [unknown static?] b2 [unknown static?] b3 [unknown static?] 00 [unknown static?] 01 [unknown static?] 01 [unknown static?] 6a [incrementing sequence number] 00 [unknown static?] 0d [unknown, sometimes 0c] 41 [unknown static?] 02 [preset number] ff [foreground r] 00 [foreground g] 00 [foreground b] 00 [background red] ff [background green] 00 [background blue] 06 [speed or direction?] 00 [unknown static?] 00 [unknown static?] 00 [unknown static?] 47 [speed or direction?] cd [check sum]
2215
2216 Known messages
2217
2218
2219 b0 b1 b2 b3 00 01 01 01 00 0c 10 14 15 0a 0b 0e 12 06 01 00 0f 84 dd - preset 1
2220 b0 b1 b2 b3 00 01 01 03 00 0d 41 02 00 ff ff 00 00 00 06 00 00 00 47 66 - preset 2
2221 b0 b1 b2 b3 00 01 01 04 00 0d 41 03 00 ff ff 00 00 00 06 00 00 00 48 69 - preset 3
2222 b0 b1 b2 b3 00 01 01 02 00 0d 41 01 00 ff ff 00 00 00 06 ff 00 00 45 61 - preset 4
2223 b0 b1 b2 b3 00 01 01 1f 00 0d 41 01 ff 00 00 00 00 00 06 ff 00 00 46 80 - preset 1 red or green
2224 b0 b1 b2 b3 00 01 01 27 00 0d 41 01 00 ff 00 00 00 00 06 ff 00 00 46 88 - preset 1 red or green
2225 b0 b1 b2 b3 00 01 01 2e 00 0d 41 01 ff 00 00 00 00 00 06 ff 00 00 46 8f - preset 1 red (foreground)
2226 b0 b1 b2 b3 00 01 01 27 00 0d 41 01 00 ff 00 00 00 00 06 ff 00 00 46 88 - preset 1 green (foreground)
2227 b0 b1 b2 b3 00 01 01 3e 00 0d 41 01 00 00 ff 00 00 00 06 ff 00 00 46 9f - preset 1 blue (foreground)
2228 b0 b1 b2 b3 00 01 01 54 00 0d 41 02 00 ff 00 00 00 00 06 00 00 00 48 b9 - preset 2 green (foreground)
2229 b0 b1 b2 b3 00 01 01 55 00 0d 41 02 ff 00 00 00 00 00 06 00 00 00 48 ba - preset 2 red (foreground)
2230 b0 b1 b2 b3 00 01 01 67 00 0d 41 02 ff 00 00 ff 00 00 06 00 00 00 47 ca - preset 2 red (foreground), red (background)
2231 b0 b1 b2 b3 00 01 01 67 00 0d 41 02 ff 00 00 ff 00 00 06 00 00 00 47 ca - preset 2 red (foreground), red (background)
2232 b0 b1 b2 b3 00 01 01 69 00 0d 41 02 ff 00 00 ff 00 00 06 00 00 00 47 cc - preset 2 red (foreground), red (background)
2233 b0 b1 b2 b3 00 01 01 6a 00 0d 41 02 ff 00 00 00 ff 00 06 00 00 00 47 cd - preset 2 red (foreground), green (background)
2234 b0 b1 b2 b3 00 01 01 77 00 0d 41 02 ff 00 00 00 ff 00 06 00 00 00 47 da - preset 2 red (foreground), green (background) - direction RTL
2235 b0 b1 b2 b3 00 01 01 7d 00 0d 41 02 ff 00 00 00 ff 00 06 00 00 00 47 e0 - preset 2 red (foreground), green (background) - direction RTL
2236 b0 b1 b2 b3 00 01 01 7d 00 0d 41 02 ff 00 00 00 ff 00 06 00 00 00 47 e0 - preset 2 red (foreground), green (background) - direction RTL
2237 b0 b1 b2 b3 00 01 01 7c 00 0d 41 02 ff 00 00 00 ff 00 06 01 00 00 48 e1 - preset 2 red (foreground), green (background) - direction LTR
2238 b0 b1 b2 b3 00 01 01 89 00 0d 41 02 ff 00 00 00 ff 00 00 00 00 00 41 e0 - preset 2 red (foreground), green (background) - direction LTR - speed 0
2239 b0 b1 b2 b3 00 01 01 8a 00 0d 41 02 ff 00 00 00 ff 00 64 00 00 00 a5 a9 - preset 2 red (foreground), green (background) - direction LTR - speed 64
2240 b0 b1 b2 b3 00 01 01 8b 00 0d 41 02 ff 00 00 00 ff 00 00 00 00 00 41 e2 - preset 2 red (foreground), green (background) - direction LTR - speed 0?
2241 b0 b1 b2 b3 00 01 01 8c 00 0d 41 02 ff 00 00 00 ff 00 64 00 00 00 a5 ab - preset 2 red (foreground), green (background) - direction LTR - speed 64?
2242
2243 Set Blue
2244 b0b1b2b30001010b0034a0000600010000ff0000ff0002ffff000000ff00030000ff0000ff0004ffff000000ff00050000ff0000ff0006ffff000000ffac5f
2245
2246 Query
2247 b0b1b2b30001010c0004818a8b9604
2248 b0b1b2b30001010c000e811a23280000640f000001000660a2
2249
2250 Set Red
2251 b0b1b2b30001010d0034a0000600010000ff0000ff0002ff00000000ff00030000ff0000ff0004ff00000000ff00050000ff0000ff0006ff00000000ffaf67
2252 """
2253 return [
2254 self.construct_wrapped_message(msg, inner_pre_constructed=True)
2255 for msg in super().construct_levels_change(
2256 persist, red, green, blue, warm_white, cool_white, write_mode
2257 )
2258 ]
2259
2260 def construct_zone_change(
2261 self,
2262 points: int, # the number of points on the strip
2263 rgb_list: list[tuple[int, int, int]],
2264 speed: int,
2265 effect: MultiColorEffects,
2266 ) -> bytearray:
2267 """
2268 The bytes to send for multiple zones.
2269
2270 Blue/Green - Static
2271 590063ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff00000000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff001e04640024
2272
2273 Red/Blue - Jump
2274 5900630000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff00ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff00001e01640021
2275
2276 White/Green - Static
2277 590063ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff00001e01640003
2278 11111 22222 33333 44444 55555 66666 77777 88888 99999 00000 11111 22222 33333 44444 55555
2279
2280 White - Static
2281 590063ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff001e016400e5
2282
2283 White - Running Water - Full speed
2284 590063ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff001e026400e6
2285
2286 White - Running Water - 50% speed
2287 590063ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff001e023200b4
2288
2289 Red - Blue - Gradient
2290 590063ff0000f60008ed0011e4001adb0023d3002bca0034c1003db80046af004fa700579e00609500698c007283007b7b008372008c69009560009e5700a74f00af4600b83d00c13400ca2b00d32300db1a00e41100ed0800f60000ff001e01640005
2291
2292 Red - Brething
2293 590063ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000001e05640025
2294 """
2295 sent_zones = len(rgb_list)
2296 if sent_zones > points:
2297 raise ValueError(f"Device supports a maximum of {points} zones")
2298 pixel_bits = 9 + (points * 3)
2299 pixels = bytearray([pixel_bits >> 8, pixel_bits & 0xFF])
2300 msg = bytearray([0x59])
2301 msg.extend(pixels)
2302 zone_size = points // sent_zones
2303 remaining = points
2304 for rgb in rgb_list:
2305 for _ in range(zone_size):
2306 remaining -= 1
2307 msg.extend(bytearray([*rgb]))
2308 while remaining:
2309 remaining -= 1
2310 msg.extend(bytearray([*rgb]))
2311 msg.extend(bytearray([0x00, 0x1E]))
2312 msg.extend(bytearray([effect.value, speed]))
2313 msg.append(0x00)
2314 return self.construct_wrapped_message(msg)
2315
2316 def construct_device_config(
2317 self,
2318 operating_mode: int | None,
2319 wiring: int | None,
2320 ic_type: int | None, # ic type
2321 pixels_per_segment: int | None, # pixels per segment
2322 segments: int | None, # number of segments
2323 music_pixels_per_segment: int | None, # music pixels per segment
2324 music_segments: int | None, # number of music segments
2325 ) -> bytearray:
2326 """The bytes to send to change device config."""
2327 return self.construct_wrapped_message(
2328 super().construct_device_config(
2329 operating_mode,
2330 wiring,
2331 ic_type,
2332 pixels_per_segment,
2333 segments,
2334 music_pixels_per_segment,
2335 segments,
2336 ),
2337 inner_pre_constructed=True,
2338 )
2339
2340
2341 class ProtocolLEDENETSocket(ProtocolLEDENET8Byte):
2342 @property
2343 def power_push_updates(self) -> bool:
2344 """If True the protocol pushes power state updates when controlled via ir/rf/app."""
2345 return True
2346
2347 @property
2348 def state_push_updates(self) -> bool:
2349 """If True the protocol pushes state updates when controlled via ir/rf/app."""
2350 return True
2351
2352 @property
2353 def name(self) -> str:
2354 """The name of the protocol."""
2355 return PROTOCOL_LEDENET_SOCKET
2356
2357 @property
2358 def timer_response_len(self) -> int:
2359 """Return the time response len."""
2360 return LEDENET_TIMERS_SOCKET_RESPONSE_LEN
2361
2362 @property
2363 def timer_len(self) -> int:
2364 """Return a single timer len."""
2365 return 12
2366
2367 @property
2368 def timer_count(self) -> int:
2369 """Return the number of timers."""
2370 return 8
2371
2372
2373 class ProtocolLEDENETCCT(ProtocolLEDENET9Byte):
2374 MIN_BRIGHTNESS = 2
2375
2376 @property
2377 def timer_response_len(self) -> int:
2378 """Return the time response len."""
2379 return LEDENET_TIMERS_8BYTE_RESPONSE_LEN
2380
2381 @property
2382 def timer_len(self) -> int:
2383 """Return a single timer len."""
2384 return 14
2385
2386 @property
2387 def dimmable_effects(self) -> bool:
2388 """Protocol supports dimmable effects."""
2389 return False
2390
2391 @property
2392 def name(self) -> str:
2393 """The name of the protocol."""
2394 return PROTOCOL_LEDENET_CCT
2395
2396 @property
2397 def power_push_updates(self) -> bool:
2398 """If True the protocol pushes power state updates when controlled via ir/rf/app."""
2399 return True
2400
2401 def construct_levels_change(
2402 self,
2403 persist: int,
2404 red: int | None,
2405 green: int | None,
2406 blue: int | None,
2407 warm_white: int | None,
2408 cool_white: int | None,
2409 write_mode: LevelWriteMode | int,
2410 ) -> list[bytearray]:
2411 """
2412 The bytes to send for a level change request.
2413
2414 b0 b1 b2 b3 00 01 01 52 00 09 35 b1 00 64 00 00 00 03 4d bd - 100% warm
2415 b0 b1 b2 b3 00 01 01 72 00 09 35 b1 64 64 00 00 00 03 b1 a5 - 100% cool
2416 b0 b1 b2 b3 00 01 01 9f 00 09 35 b1 64 32 00 00 00 03 7f 6e - 100% cool - dim 50%
2417 """
2418 assert warm_white is not None, "CCT devices must set a warm white value"
2419 assert cool_white is not None, "CCT devices must set a cool white value"
2420 scaled_temp, brightness = white_levels_to_scaled_color_temp(
2421 warm_white, cool_white
2422 )
2423 return [
2424 self.construct_message(
2425 bytearray(
2426 [
2427 0x35,
2428 0xB1,
2429 scaled_temp,
2430 # If the brightness goes below the precision the device
2431 # will flip from cold to warm
2432 max(self.MIN_BRIGHTNESS, brightness),
2433 0x00,
2434 0x00,
2435 0x00,
2436 0x03,
2437 ]
2438 )
2439 )
2440 ]
2441
2442
2443 class ProtocolLEDENETCCTWrapped(ProtocolLEDENETCCT):
2444 @property
2445 def name(self) -> str:
2446 """The name of the protocol."""
2447 return PROTOCOL_LEDENET_CCT_WRAPPED
2448
2449 @property
2450 def state_push_updates(self) -> bool:
2451 """If True the protocol pushes state updates when controlled via ir/rf/app."""
2452 return True
2453
2454 @property
2455 def requires_turn_on(self) -> bool:
2456 """If True the device must be turned on before setting level/patterns/modes."""
2457 return False
2458
2459 def construct_state_query(self) -> bytearray:
2460 """The bytes to send for a query request."""
2461 return self.construct_wrapped_message(
2462 super().construct_state_query(),
2463 inner_pre_constructed=True,
2464 )
2465
2466 def construct_state_change(self, turn_on: int) -> bytearray:
2467 """The bytes to send for a state change request."""
2468 return self.construct_wrapped_message(
2469 super().construct_state_change(turn_on),
2470 inner_pre_constructed=True,
2471 )
2472
2473 def construct_levels_change(
2474 self,
2475 persist: int,
2476 red: int | None,
2477 green: int | None,
2478 blue: int | None,
2479 warm_white: int | None,
2480 cool_white: int | None,
2481 write_mode: LevelWriteMode | int,
2482 ) -> list[bytearray]:
2483 """
2484 The bytes to send for a level change request.
2485
2486 b0 b1 b2 b3 00 01 01 52 00 09 35 b1 00 64 00 00 00 03 4d bd - 100% warm
2487 b0 b1 b2 b3 00 01 01 72 00 09 35 b1 64 64 00 00 00 03 b1 a5 - 100% cool
2488 b0 b1 b2 b3 00 01 01 9f 00 09 35 b1 64 32 00 00 00 03 7f 6e - 100% cool - dim 50%
2489 """
2490 return [
2491 self.construct_wrapped_message(
2492 super().construct_levels_change(
2493 persist, red, green, blue, warm_white, cool_white, write_mode
2494 )[0],
2495 inner_pre_constructed=True,
2496 )
2497 ]
2498
2499
2500 class ProtocolLEDENETAddressableChristmas(ProtocolLEDENETAddressableBase):
2501 @property
2502 def speed_is_delay(self) -> bool:
2503 """If True the speed is a delay in ms."""
2504 return True
2505
2506 def construct_state_query(self) -> bytearray:
2507 """The bytes to send for a query request."""
2508 return self.construct_wrapped_message(
2509 super().construct_state_query(),
2510 inner_pre_constructed=True,
2511 )
2512
2513 def construct_state_change(self, turn_on: int) -> bytearray:
2514 """The bytes to send for a state change request."""
2515 return self.construct_wrapped_message(
2516 super().construct_state_change(turn_on),
2517 inner_pre_constructed=True,
2518 )
2519
2520 @property
2521 def name(self) -> str:
2522 """The name of the protocol."""
2523 return PROTOCOL_LEDENET_ADDRESSABLE_CHRISTMAS
2524
2525 @property
2526 def zones(self) -> bool:
2527 """If the protocol supports zones."""
2528 return True
2529
2530 @property
2531 def power_push_updates(self) -> bool:
2532 """If True the protocol pushes power state updates when controlled via ir/rf/app."""
2533 return True
2534
2535 @property
2536 def state_push_updates(self) -> bool:
2537 """If True the protocol pushes state updates when controlled via ir/rf/app."""
2538 return True
2539
2540 @property
2541 def dimmable_effects(self) -> bool:
2542 """Protocol supports dimmable effects."""
2543 return False
2544
2545 @property
2546 def requires_turn_on(self) -> bool:
2547 """If True the device must be turned on before setting level/patterns/modes."""
2548 return False
2549
2550 def construct_preset_pattern(
2551 self, pattern: int, speed: int, brightness: int
2552 ) -> bytearray:
2553 """
2554 The bytes to send for a preset pattern.
2555 0xB0 0xB1 0xB2 0xB3 0x00 0x01 0x01 0x2A 0x00 0x04 0x38 0x01 0x10 0x00 0x3F (15)
2556 0xB0 0xB1 0xB2 0xB3 0x00 0x01 0x01 0x2B 0x00 0x04 0x38 0x02 0x10 0x00 0x41 (15)
2557 0xB0 0xB1 0xB2 0xB3 0x00 0x01 0x01 0x2C 0x00 0x04 0x38 0x03 0x10 0x00 0x43 (15)
2558 0xB0 0xB1 0xB2 0xB3 0x00 0x01 0x01 0x2D 0x00 0x04 0x38 0x04 0x10 0x00 0x45 (15)
2559 0xB0 0xB1 0xB2 0xB3 0x00 0x01 0x01 0x2E 0x00 0x04 0x38 0x05 0x10 0x00 0x47 (15)
2560 0xB0 0xB1 0xB2 0xB3 0x00 0x01 0x01 0x2F 0x00 0x04 0x38 0x06 0x10 0x00 0x49 (15)
2561 """
2562 return self.construct_wrapped_message(
2563 bytearray(
2564 [
2565 0x38,
2566 pattern,
2567 utils.speedToDelay(speed),
2568 ]
2569 )
2570 )
2571
2572 def construct_levels_change(
2573 self,
2574 persist: int,
2575 red: int | None,
2576 green: int | None,
2577 blue: int | None,
2578 warm_white: int | None,
2579 cool_white: int | None,
2580 write_mode: LevelWriteMode | int,
2581 ) -> list[bytearray]:
2582 """
2583 The bytes to send for a level change request.
2584
2585 Green 100%:
2586 b0b1b2b300010180000d3ba100646400000000000000a49d
2587
2588 Blue 50%
2589 b0b1b2b300010110000d3ba176e4320000000000000068b5
2590
2591 Red & green 255 and 25% bright
2592 b0b1b2b300010133000d3ba11e64190000000000000077f6
2593
2594 Red & Blue 255 and 40%
2595 b0b1b2b30001014e000d3ba196642800000000000000fe1f
2596
2597 Inner messages
2598
2599 Single - Green - Brightness 100%
2600 3b a1 3c 64 64 00 00 00 00 00 00 00 e0
2601
2602 Single - Green - Brightness 50%
2603 3b a1 3c 64 32 00 00 00 00 00 00 00 ae
2604
2605 Single - Blue - Brightness 100%
2606 3b a1 78 64 64 00 00 00 00 00 00 00 1c
2607
2608 Single - Red - Brightness 100%
2609 3b a1 00 64 64 00 00 00 00 00 00 00 a4
2610
2611 Single - Pink (100% Red, 100% Blue) - Brightness 100%
2612 3b a1 96 64 64 00 00 00 00 00 00 00 3a
2613
2614 Single - White (100% Red, 100% Green, 100% Blue) - Brightness 100%
2615 3b a1 00 00 64 00 00 00 00 00 00 00 40
2616
2617 Single - Yellow (100% Red, 100% Green) - Brightness 100%
2618 3b a1 1e 64 64 00 00 00 00 00 00 00 c2
2619
2620 Single - Light Blue (100% Blue, 100% Green) - Brightness 100%
2621 3b a1 5a 64 64 00 00 00 00 00 00 00 fe
2622
2623 Single - Red - Brightness 0%
2624 3b a1 00 64 00 00 00 00 00 00 00 00 40
2625
2626 Single - Red - Brightness 50%
2627 3b a1 00 64 32 00 00 00 00 00 00 00 72
2628
2629 Single - Blue - Brightness 50%
2630 3b a1 78 64 32 00 00 00 00 00 00 00 ea
2631 """
2632 assert red is not None
2633 assert green is not None
2634 assert blue is not None
2635 h, s, v = colorsys.rgb_to_hsv(red / 255, green / 255, blue / 255)
2636 return [
2637 self.construct_wrapped_message(
2638 bytearray(
2639 [
2640 0x3B,
2641 0xA1,
2642 int(h * 180),
2643 int(s * 100),
2644 int(v * 100),
2645 0x00,
2646 0x00,
2647 0x00,
2648 0x00,
2649 0x00,
2650 0x00,
2651 0x00,
2652 ]
2653 )
2654 )
2655 ]
2656
2657 def construct_zone_change(
2658 self,
2659 points: int, # the number of points on the strip
2660 rgb_list: list[tuple[int, int, int]],
2661 speed: int,
2662 effect: MultiColorEffects,
2663 ) -> bytearray:
2664 """
2665 The bytes to send for multiple zones.
2666
2667 6 Zone All red
2668 a000060001ff00000000ff0002ff00000000ff0003ff00000000ff0004ff00000000ff0005ff00000000ff0006ff00000000ffaf
2669 6 Zone All Yellow
2670 a000060001ffff000000ff0002ffff000000ff0003ffff000000ff0004ffff000000ff0005ffff000000ff0006ffff000000ffa9
2671 6 Zone All Green
2672 a00006000100ff000000ff000200ff000000ff000300ff000000ff000400ff000000ff000500ff000000ff000600ff000000ffaf
2673 6 Zone All Green
2674 a00006000100ff000000ff000200ff000000ff000300ff000000ff000400ff000000ff000500ff000000ff000600ff000000ffaf
2675 6 Zone All Cyan
2676 a00006000100ffff0000ff000200ffff0000ff000300ffff0000ff000400ffff0000ff000500ffff0000ff000600ffff0000ffa9
2677 6 Zone All White
2678 a000060001ffffff0000ff0002ffffff0000ff0003ffffff0000ff0004ffffff0000ff0005ffffff0000ff0006ffffff0000ffa3
2679 """
2680 sent_zones = len(rgb_list)
2681 if sent_zones > points:
2682 raise ValueError(f"Device supports a maximum of {points} zones")
2683 msg = bytearray([0xA0, 0x00, 0x06])
2684 zone_size = points // sent_zones
2685 remaining = points
2686 for rgb in rgb_list:
2687 for _ in range(zone_size):
2688 remaining -= 1
2689 msg.extend(
2690 bytearray([0x00, points - remaining, *rgb, 0x00, 0x00, 0xFF])
2691 )
2692 while remaining:
2693 remaining -= 1
2694 msg.extend(
2695 bytearray([0x00, points - remaining, *rgb_list[-1], 0x00, 0x00, 0xFF])
2696 )
2697 return self.construct_wrapped_message(msg)
2698
2699 def parse_strip_setting(self, msg: bytes) -> LEDENETAddressableDeviceConfiguration:
2700 """Parse a strip settings message."""
2701 return LEDENETAddressableDeviceConfiguration(
2702 pixels_per_segment=6,
2703 segments=None,
2704 music_pixels_per_segment=None,
2705 music_segments=None,
2706 wirings=[],
2707 wiring_num=None,
2708 wiring=None,
2709 num_to_wiring={},
2710 wiring_to_num={},
2711 ic_type=None,
2712 ic_type_num=None,
2713 operating_mode=COLOR_MODE_RGB,
2714 )