| 1 |
from enum import IntEnum |
| 2 |
from .feature import Feature |
| 3 |
from typing import TYPE_CHECKING, Optional, Dict, Any, Union, Sequence |
| 4 |
|
| 5 |
if TYPE_CHECKING: |
| 6 |
from .box import Box |
| 7 |
|
| 8 |
|
| 9 |
ctx2_v3 = {"cct1": lambda x: f"{x}------", "cct2": lambda x: f"----{x}--"} |
| 10 |
ctx2 = {"cct1": lambda x: f"{x}----", "cct2": lambda x: f"----{x}"} |
| 11 |
|
| 12 |
mono = { |
| 13 |
"mono1": lambda x: f"{x}------", |
| 14 |
"mono2": lambda x: f"--{x}----", |
| 15 |
"mono3": lambda x: f"----{x}--", |
| 16 |
"mono4": lambda x: f"------{x}", |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
class BleboxColorMode(IntEnum): |
| 21 |
RGBW = 1 |
| 22 |
RGB = 2 |
| 23 |
MONO = 3 |
| 24 |
RGBorW = 4 |
| 25 |
CT = 5 |
| 26 |
CTx2 = 6 |
| 27 |
RGBWW = 7 |
| 28 |
|
| 29 |
@classmethod |
| 30 |
def invert(cls): |
| 31 |
return {item.value: item.name for item in cls} |
| 32 |
|
| 33 |
|
| 34 |
BLEBOX_COLOR_MODES = BleboxColorMode.invert() |
| 35 |
|
| 36 |
|
| 37 |
class Light(Feature): |
| 38 |
|
| 39 |
CURRENT_CONF = dict() |
| 40 |
CONFIG = { |
| 41 |
"wLightBox": { |
| 42 |
"default": "FFFFFFFF", |
| 43 |
"off": "00000000", |
| 44 |
"white?": True, |
| 45 |
"color?": True, |
| 46 |
"to_value": lambda int_value: f"{int_value:02x}", |
| 47 |
"validator": lambda product, alias, raw: product.expect_rgbw(alias, raw), |
| 48 |
}, |
| 49 |
"wLightBoxS": { |
| 50 |
"default": "FF", |
| 51 |
"off": "00", |
| 52 |
"white?": False, |
| 53 |
"color?": False, |
| 54 |
"to_value": lambda int_value: f"{int_value:02x}", |
| 55 |
"validator": lambda product, alias, raw: product.expect_hex_str( |
| 56 |
alias, raw, 255, 0 |
| 57 |
), |
| 58 |
}, |
| 59 |
"dimmerBox": { |
| 60 |
"default": 0xFF, |
| 61 |
"off": 0x0, |
| 62 |
"white?": False, |
| 63 |
"color?": False, |
| 64 |
"to_value": lambda int_value: int_value, |
| 65 |
"validator": lambda product, alias, raw: product.expect_int( |
| 66 |
alias, raw, 255, 0 |
| 67 |
), |
| 68 |
}, |
| 69 |
} |
| 70 |
|
| 71 |
COLOR_MODE_CONFIG = { |
| 72 |
"CT": { |
| 73 |
"default": "FFFFFFFF", |
| 74 |
"off": "0000", |
| 75 |
"white?": False, |
| 76 |
"color?": False, |
| 77 |
"to_value": lambda int_value: f"{int_value:02x}", |
| 78 |
"validator": lambda product, alias, raw: product.expect_hex_str( |
| 79 |
alias, raw, 255, 0 |
| 80 |
), |
| 81 |
}, |
| 82 |
"CTx2": { |
| 83 |
"default": "FFFFFFFF", |
| 84 |
"off": "0000", |
| 85 |
"white?": False, |
| 86 |
"color?": False, |
| 87 |
"to_value": lambda int_value: f"{int_value:02x}", |
| 88 |
"validator": lambda product, alias, raw: product.expect_hex_str( |
| 89 |
alias, raw, 255, 0 |
| 90 |
), |
| 91 |
}, |
| 92 |
"RGBWW": { |
| 93 |
"default": "FFFFFFFFFF", |
| 94 |
"off": "0000000000", |
| 95 |
"white?": True, |
| 96 |
"color?": True, |
| 97 |
"to_value": lambda int_value: f"{int_value:02x}", |
| 98 |
"validator": lambda product, alias, raw: product.expect_hex_str( |
| 99 |
alias, raw, 255, 0 |
| 100 |
), |
| 101 |
}, |
| 102 |
} |
| 103 |
|
| 104 |
def __init__( |
| 105 |
self, |
| 106 |
product: "Box", |
| 107 |
alias: str, |
| 108 |
methods: dict, |
| 109 |
extended_state: Optional[Dict], |
| 110 |
mask: Any, |
| 111 |
desired_color, |
| 112 |
color_mode, |
| 113 |
effect_list, |
| 114 |
current_effect, |
| 115 |
index: Optional[int] = None, |
| 116 |
) -> None: |
| 117 |
super().__init__(product, alias, methods) |
| 118 |
self._index = index |
| 119 |
config = self.CONFIG[product.type] |
| 120 |
self.mask = mask |
| 121 |
self.desired_color = desired_color |
| 122 |
self._color_mode = color_mode |
| 123 |
self._effect_list = effect_list |
| 124 |
if self._effect_list is not None: |
| 125 |
self._effect = current_effect |
| 126 |
|
| 127 |
if extended_state not in [None, {}]: |
| 128 |
self.extended_state = extended_state |
| 129 |
self.device_colorMode = color_mode |
| 130 |
if self.device_colorMode in [6, 7]: |
| 131 |
config = self.COLOR_MODE_CONFIG[ |
| 132 |
BLEBOX_COLOR_MODES[self.device_colorMode] |
| 133 |
] |
| 134 |
else: |
| 135 |
if product.type == "dimmerBox": |
| 136 |
self.device_colorMode = BleboxColorMode.MONO |
| 137 |
self.CURRENT_CONF = config |
| 138 |
|
| 139 |
self._off_value = self.evaluate_off_value(config, desired_color) |
| 140 |
self._last_on_state = self._default_on_value = config["default"] |
| 141 |
|
| 142 |
@classmethod |
| 143 |
def many_from_config( |
| 144 |
cls, product, box_type_config, extended_state |
| 145 |
) -> list["Light"]: |
| 146 |
if isinstance(extended_state, dict) and extended_state is not None: |
| 147 |
desired_color = extended_state.get("rgbw", {}).get("desiredColor") |
| 148 |
color_mode = extended_state.get("rgbw", {}).get("colorMode") |
| 149 |
current_effect = extended_state.get("rgbw", {}).get("effectID") |
| 150 |
effect_list = extended_state.get("rgbw", {}).get("effectsNames") |
| 151 |
else: |
| 152 |
desired_color = None |
| 153 |
color_mode = None |
| 154 |
current_effect = None |
| 155 |
effect_list = None |
| 156 |
alias, methods = box_type_config[0] |
| 157 |
const_kwargs = dict( |
| 158 |
methods=methods, |
| 159 |
extended_state=extended_state, |
| 160 |
desired_color=desired_color, |
| 161 |
color_mode=color_mode, |
| 162 |
current_effect=current_effect, |
| 163 |
effect_list=effect_list, |
| 164 |
) |
| 165 |
|
| 166 |
if extended_state is not None and color_mode is not None: |
| 167 |
if BleboxColorMode(color_mode).name == "RGBW": |
| 168 |
if len(desired_color) == 10: |
| 169 |
|
| 170 |
def generate_mask(x): |
| 171 |
return f"{x}--" |
| 172 |
|
| 173 |
mask = generate_mask |
| 174 |
else: |
| 175 |
mask = None |
| 176 |
return [cls(product, alias=alias + "_RGBW", mask=mask, **const_kwargs)] |
| 177 |
|
| 178 |
if BleboxColorMode(color_mode).name == "RGB": |
| 179 |
return [cls(product, alias=alias + "_RGB", mask=None, **const_kwargs)] |
| 180 |
|
| 181 |
if BleboxColorMode(color_mode).name == "MONO": |
| 182 |
if len(desired_color) % 2 == 0: |
| 183 |
object_list = [] |
| 184 |
mono_item = list(mono.items()) |
| 185 |
for i in range(0, int(len(desired_color) / 2)): |
| 186 |
indicator, mask = mono_item[i] |
| 187 |
object_list.append( |
| 188 |
cls( |
| 189 |
product, |
| 190 |
alias=alias + "_" + indicator, |
| 191 |
mask=mask, |
| 192 |
index=i, |
| 193 |
**const_kwargs, |
| 194 |
) |
| 195 |
) |
| 196 |
return object_list |
| 197 |
|
| 198 |
if BleboxColorMode(color_mode).name == "RGBorW": |
| 199 |
return [ |
| 200 |
cls(product, alias=alias + "_RGBorW", mask=None, **const_kwargs) |
| 201 |
] |
| 202 |
|
| 203 |
if BleboxColorMode(color_mode).name == "CT": |
| 204 |
mask = ctx2["cct1"] |
| 205 |
if len(desired_color) > 8: |
| 206 |
mask = ctx2_v3["cct1"] |
| 207 |
return [cls(product, alias=alias + "_cct", mask=mask, **const_kwargs)] |
| 208 |
|
| 209 |
if BleboxColorMode(color_mode).name == "CTx2": |
| 210 |
object_list = [] |
| 211 |
ct = ctx2 |
| 212 |
if len(desired_color) > 8: |
| 213 |
ct = ctx2_v3 |
| 214 |
for i, (indicator, mask) in enumerate(ct.items()): |
| 215 |
object_list.append( |
| 216 |
cls( |
| 217 |
product, |
| 218 |
alias=alias + "_" + indicator, |
| 219 |
mask=mask, |
| 220 |
index=i, |
| 221 |
**const_kwargs, |
| 222 |
) |
| 223 |
) |
| 224 |
return object_list |
| 225 |
|
| 226 |
if BleboxColorMode(color_mode).name == "RGBWW": |
| 227 |
return [ |
| 228 |
cls(product, alias=alias + "_RGBCCT", mask=None, **const_kwargs) |
| 229 |
] |
| 230 |
|
| 231 |
if len(box_type_config) > 0: |
| 232 |
del const_kwargs["methods"] |
| 233 |
if "Brightness" in box_type_config[0][1].get("desired"): |
| 234 |
const_kwargs["color_mode"] = BleboxColorMode.MONO |
| 235 |
return [ |
| 236 |
cls(product, *args, mask=None, **const_kwargs) |
| 237 |
for args in box_type_config |
| 238 |
] |
| 239 |
else: |
| 240 |
return [] |
| 241 |
|
| 242 |
@property |
| 243 |
def brightness(self) -> Optional[int]: |
| 244 |
if self.color_mode in [6, 5]: |
| 245 |
_, bgt = self.color_temp_brightness_int_from_hex(self._desired) |
| 246 |
return bgt |
| 247 |
elif self.color_mode is not None and ( |
| 248 |
rgb_list := self.rgb_hex_to_rgb_list(self.rgb_hex) |
| 249 |
): |
| 250 |
return self.evaluate_brightness_from_rgb(rgb_list) |
| 251 |
else: |
| 252 |
return None |
| 253 |
|
| 254 |
@property |
| 255 |
def index(self) -> Optional[int]: |
| 256 |
return self._index |
| 257 |
|
| 258 |
@property |
| 259 |
def effect_list(self): |
| 260 |
if isinstance(self._effect_list, dict): |
| 261 |
return list(self._effect_list.values()) |
| 262 |
else: |
| 263 |
return [] |
| 264 |
|
| 265 |
@property |
| 266 |
def color_temp(self): |
| 267 |
ct, _ = self.color_temp_brightness_int_from_hex(self._desired) |
| 268 |
return ct |
| 269 |
|
| 270 |
@staticmethod |
| 271 |
def evaluate_brightness_from_rgb(iterable: Sequence[int]) -> int: |
| 272 |
"return brightness from 0 to 255 evaluated basing rgb" |
| 273 |
if max(iterable) > 255: |
| 274 |
raise ValueError( |
| 275 |
f"evaluate_brightness_from_rgb values out of range, max is {max(iterable)}." |
| 276 |
) |
| 277 |
elif min(iterable) < 0: |
| 278 |
raise ValueError( |
| 279 |
f"evaluate_brightness_from_rgb values out of range, min is {min(iterable)}." |
| 280 |
) |
| 281 |
return int(max(iterable)) |
| 282 |
|
| 283 |
@staticmethod |
| 284 |
def swap_ww_cw(value: list) -> list: |
| 285 |
"""Swap WW and CW channels in RGBWW mode for API compatibility.""" |
| 286 |
if len(value) == 5: |
| 287 |
value = list(value) |
| 288 |
value[3], value[4] = value[4], value[3] |
| 289 |
return value |
| 290 |
|
| 291 |
@staticmethod |
| 292 |
def scale_rgbww_brightness(value: list, brightness: int) -> list: |
| 293 |
"""Scale RGBWW value preserving WW:CW ratio.""" |
| 294 |
current_max = max(value) |
| 295 |
if current_max > 0: |
| 296 |
return [round(x * brightness / current_max) for x in value] |
| 297 |
else: |
| 298 |
return [0, 0, 0, 0, brightness] |
| 299 |
|
| 300 |
def apply_brightness(self, value: int, brightness: int) -> Any: |
| 301 |
"""Return list of values with applied brightness.""" |
| 302 |
if not isinstance(brightness, int): |
| 303 |
raise ValueError( |
| 304 |
f"adjust_brightness called with bad parameter ({brightness} is {type(brightness)} instead of int)" |
| 305 |
) |
| 306 |
|
| 307 |
if brightness > 255: |
| 308 |
raise ValueError( |
| 309 |
f"adjust_brightness called with bad parameter ({brightness} is greater than 255)" |
| 310 |
) |
| 311 |
|
| 312 |
if self.product.type == "dimmerBox" or self.color_mode == BleboxColorMode.MONO: |
| 313 |
return [brightness] |
| 314 |
if brightness == 0: |
| 315 |
return [value] |
| 316 |
|
| 317 |
if self.color_mode in ( |
| 318 |
BleboxColorMode.RGB, |
| 319 |
BleboxColorMode.RGBW, |
| 320 |
BleboxColorMode.RGBorW, |
| 321 |
): |
| 322 |
value = self.normalise_elements_of_rgb(list(value)) |
| 323 |
res = list(map(lambda x: round(x * (brightness / 255)), value)) |
| 324 |
elif self.color_mode == BleboxColorMode.RGBWW: |
| 325 |
res = self.scale_rgbww_brightness(value, brightness) |
| 326 |
else: |
| 327 |
res = list(map(lambda x: round(x * (brightness / 255)), value)) |
| 328 |
return res |
| 329 |
|
| 330 |
def evaluate_off_value(self, config: dict, raw_hex: str) -> str: |
| 331 |
""" |
| 332 |
Return hex representing off state value without mask formatting for necessary channels if mask is applied. |
| 333 |
If no mask applied than return default from config |
| 334 |
|
| 335 |
:param config: |
| 336 |
:param raw_hex: |
| 337 |
:return: str |
| 338 |
""" |
| 339 |
if self.mask: |
| 340 |
placeholder = self._get_placeholder_for_color_mode() |
| 341 |
mask_result = self.mask(placeholder) |
| 342 |
x_count = mask_result.count("x") |
| 343 |
return "0" * x_count |
| 344 |
elif raw_hex is not None: |
| 345 |
if len(raw_hex) < len(config["off"]): |
| 346 |
return config["off"][: len(raw_hex)] |
| 347 |
return config["off"] |
| 348 |
|
| 349 |
@property |
| 350 |
def supports_white(self) -> Any: |
| 351 |
return self.CURRENT_CONF["white?"] |
| 352 |
|
| 353 |
@property |
| 354 |
def white_value(self) -> Optional[int]: |
| 355 |
return self._white_value |
| 356 |
|
| 357 |
def apply_white(self, value: str, white: int) -> Union[int, str]: |
| 358 |
if white is None: |
| 359 |
return value |
| 360 |
|
| 361 |
if not self.supports_white: |
| 362 |
return value |
| 363 |
|
| 364 |
rgbhex = value[0:6] |
| 365 |
white_raw = f"{white:02x}" |
| 366 |
return f"{rgbhex}{white_raw}" |
| 367 |
|
| 368 |
@property |
| 369 |
def supports_color(self) -> Any: |
| 370 |
return self.CURRENT_CONF["color?"] |
| 371 |
|
| 372 |
@property |
| 373 |
def color_mode(self) -> int: |
| 374 |
return self._color_mode |
| 375 |
|
| 376 |
def apply_color(self, value: str, rgb_hex: str) -> Union[int, str]: |
| 377 |
if rgb_hex is None: |
| 378 |
return value |
| 379 |
|
| 380 |
if not self.supports_color: |
| 381 |
return value |
| 382 |
|
| 383 |
white_hex = value[6:8] |
| 384 |
return f"{rgb_hex}{white_hex}" |
| 385 |
|
| 386 |
def _get_placeholder_for_color_mode(self) -> str: |
| 387 |
if self._color_mode == BleboxColorMode.MONO: |
| 388 |
return "xx" |
| 389 |
elif self._color_mode in (BleboxColorMode.CT, BleboxColorMode.CTx2): |
| 390 |
return "xxxx" |
| 391 |
elif self._color_mode == BleboxColorMode.RGB: |
| 392 |
return "xxxxxx" |
| 393 |
elif self._color_mode in (BleboxColorMode.RGBW, BleboxColorMode.RGBorW): |
| 394 |
return "xxxxxxxx" |
| 395 |
else: |
| 396 |
return "x" |
| 397 |
|
| 398 |
def return_color_temp_with_brightness( |
| 399 |
self, value, brightness: Any |
| 400 |
) -> Optional[str]: |
| 401 |
"""Method returns value which will be send to""" |
| 402 |
if value < 128: |
| 403 |
warm = min(255, value * 2) |
| 404 |
cold = 255 |
| 405 |
else: |
| 406 |
warm = 255 |
| 407 |
cold = max(0, min(255, (255 - value) * 2)) |
| 408 |
cold = cold * brightness / 255 |
| 409 |
warm = warm * brightness / 255 |
| 410 |
cold = f"{int(round(cold)):02x}" |
| 411 |
warm = f"{int(round(warm)):02x}" |
| 412 |
|
| 413 |
return self.rgb_hex_to_rgb_list(warm + cold) |
| 414 |
|
| 415 |
def value_for_selected_channels_from_given_val(self, value: str): |
| 416 |
placeholder = self._get_placeholder_for_color_mode() |
| 417 |
lambda_result = self.mask(placeholder) |
| 418 |
first_index = lambda_result.index("x") |
| 419 |
last_index = lambda_result.rindex("x") |
| 420 |
return value[first_index : last_index + 1] |
| 421 |
|
| 422 |
@staticmethod |
| 423 |
def color_temp_brightness_int_from_hex(val) -> (int, int): |
| 424 |
"""Assuming that hex is 2channels, 4characters. Return values for front end""" |
| 425 |
|
| 426 |
cold = int(val[2:], 16) |
| 427 |
warm = int(val[0:2], 16) |
| 428 |
|
| 429 |
if cold > warm: |
| 430 |
if warm == 0: |
| 431 |
return 0, cold |
| 432 |
else: |
| 433 |
return round(int((128 * (warm * 255 / cold) / 255)), 2), cold |
| 434 |
if cold < warm: |
| 435 |
if cold == 0: |
| 436 |
return 255, warm |
| 437 |
else: |
| 438 |
return round(int(255 - 128 * (cold * (255 / warm) / 255)), 2), warm |
| 439 |
else: |
| 440 |
return 128, max(cold, warm) |
| 441 |
|
| 442 |
@staticmethod |
| 443 |
def normalise_elements_of_rgb(elements): |
| 444 |
max_val = max(elements) |
| 445 |
min_val = min(elements) |
| 446 |
if 0 > max_val or max_val > 255: |
| 447 |
raise ValueError(f"Max value in normalisation was outside range {max_val}.") |
| 448 |
elif min_val < 0: |
| 449 |
raise ValueError(f"Min value in normalisation was outside range {min_val}.") |
| 450 |
elif max_val == 0: |
| 451 |
return [255] * len(elements) |
| 452 |
return list(map(lambda x: round(x * 255 / max_val), elements)) |
| 453 |
|
| 454 |
@property |
| 455 |
def is_on(self) -> Optional[bool]: |
| 456 |
return self._is_on |
| 457 |
|
| 458 |
@property |
| 459 |
def effect(self) -> Optional[str]: |
| 460 |
if isinstance(self._effect_list, dict): |
| 461 |
return self._effect_list.get(str(self._effect)) |
| 462 |
return self._effect |
| 463 |
|
| 464 |
def after_update(self) -> None: |
| 465 |
alias = self._alias |
| 466 |
product = self._product |
| 467 |
|
| 468 |
if product.last_data is None: |
| 469 |
self._desired_raw = None |
| 470 |
self._desired = None |
| 471 |
self._is_on = None |
| 472 |
self._effect = None |
| 473 |
if self.mask is None: |
| 474 |
self._white_value = None |
| 475 |
return |
| 476 |
|
| 477 |
self._effect = self.raw_value("currentEffect") |
| 478 |
raw = self._return_desired_value(alias, product) |
| 479 |
self._set_last_on_value(alias, product, raw) |
| 480 |
self._set_is_on() |
| 481 |
|
| 482 |
def _set_last_on_value(self, alias, product, raw): |
| 483 |
if raw == self._off_value: |
| 484 |
last_color = self.raw_value("last_color") |
| 485 |
if last_color is not None: |
| 486 |
raw = product.expect_rgbw(alias, last_color) |
| 487 |
if self.mask is not None: |
| 488 |
raw = self.value_for_selected_channels_from_given_val(raw) |
| 489 |
if raw == self._off_value: |
| 490 |
raw = self.value_for_selected_channels_from_given_val( |
| 491 |
"ffffffffff" |
| 492 |
) |
| 493 |
else: |
| 494 |
if raw == self._off_value: |
| 495 |
raw = "f" * len(raw) |
| 496 |
else: |
| 497 |
raw = self._default_on_value |
| 498 |
|
| 499 |
if raw in (self._off_value, None): |
| 500 |
raise ValueError(raw) |
| 501 |
|
| 502 |
self._last_on_state = raw |
| 503 |
|
| 504 |
def _set_is_on(self): |
| 505 |
self._is_on = (self._off_value != self._desired) or ( |
| 506 |
self._effect != 0 and self._effect is not None |
| 507 |
) |
| 508 |
if isinstance(self._desired, str): |
| 509 |
if int(self._desired, 16) == 0: |
| 510 |
self._is_on = False |
| 511 |
|
| 512 |
def _return_desired_value(self, alias, product) -> str: |
| 513 |
""" |
| 514 |
Return value representing desired device state, set desired fields |
| 515 |
:param alias: |
| 516 |
:param product: |
| 517 |
:return desired value including mask: |
| 518 |
""" |
| 519 |
|
| 520 |
response_desired_val = self.raw_value("desired") |
| 521 |
if self.mask is not None: |
| 522 |
raw = self.value_for_selected_channels_from_given_val(response_desired_val) |
| 523 |
self._desired = self.CONFIG[self._product.type]["validator"]( |
| 524 |
product, alias, raw |
| 525 |
) |
| 526 |
if self.color_mode in [1, 4]: |
| 527 |
self._white_value = int(raw[6:8], 16) |
| 528 |
else: |
| 529 |
raw = response_desired_val |
| 530 |
self._desired_raw = raw |
| 531 |
self._desired = self.CONFIG[self._product.type]["validator"]( |
| 532 |
product, alias, raw |
| 533 |
) |
| 534 |
if self.color_mode in [1, 4]: |
| 535 |
self._white_value = int(raw[6:8], 16) |
| 536 |
return raw |
| 537 |
|
| 538 |
@property |
| 539 |
def sensible_on_value(self) -> Any: |
| 540 |
"""Return sensible on value in hass format.""" |
| 541 |
if self.mask is not None: |
| 542 |
if int(self._last_on_state, 16) == 0: |
| 543 |
if self.color_mode in (BleboxColorMode.RGBW, BleboxColorMode.RGBorW): |
| 544 |
return 255, 255, 255, 255 |
| 545 |
if self.color_mode == BleboxColorMode.MONO: |
| 546 |
return [255] |
| 547 |
if self.color_mode in (BleboxColorMode.CT, BleboxColorMode.CTx2): |
| 548 |
return 255, 255 |
| 549 |
else: |
| 550 |
if self.color_mode in ( |
| 551 |
BleboxColorMode.MONO, |
| 552 |
BleboxColorMode.CT, |
| 553 |
BleboxColorMode.CTx2, |
| 554 |
): |
| 555 |
return self.rgb_hex_to_rgb_list(self._last_on_state) |
| 556 |
return self.normalise_elements_of_rgb( |
| 557 |
self.rgb_hex_to_rgb_list(self._last_on_state) |
| 558 |
) |
| 559 |
else: |
| 560 |
if isinstance(self._last_on_state, str): |
| 561 |
if int(self._last_on_state, 16) == 0: |
| 562 |
return [255] * len(self.rgb_hex_to_rgb_list(self._last_on_state)) |
| 563 |
else: |
| 564 |
if self.color_mode == BleboxColorMode.RGB: |
| 565 |
return self.rgb_hex_to_rgb_list(self._last_on_state[:6]) |
| 566 |
elif self.color_mode == BleboxColorMode.MONO: |
| 567 |
return self._last_on_state |
| 568 |
elif self.color_mode == BleboxColorMode.RGBWW: |
| 569 |
result = self.rgb_hex_to_rgb_list(self._last_on_state) |
| 570 |
return self.swap_ww_cw(result) |
| 571 |
else: |
| 572 |
return self.rgb_hex_to_rgb_list(self._last_on_state) |
| 573 |
else: |
| 574 |
return self._last_on_state |
| 575 |
|
| 576 |
@property |
| 577 |
def rgb_hex(self) -> Any: |
| 578 |
"""Return hex str representing rgb.""" |
| 579 |
if isinstance(self._desired, int): |
| 580 |
return f"{self._desired:02x}" |
| 581 |
else: |
| 582 |
return self._desired |
| 583 |
|
| 584 |
@property |
| 585 |
def rgbw_hex(self) -> Any: |
| 586 |
return self._desired |
| 587 |
|
| 588 |
@property |
| 589 |
def rgbww_hex(self) -> Any: |
| 590 |
if len(self._desired) < 10: |
| 591 |
return None |
| 592 |
else: |
| 593 |
hex_str = self._desired |
| 594 |
hex_str_warm_cold = hex_str[6:] |
| 595 |
output_str = hex_str[0:6] + "".join( |
| 596 |
[ |
| 597 |
hex_str_warm_cold[i - 2 : i] |
| 598 |
for i in range(len(hex_str_warm_cold), 0, -2) |
| 599 |
] |
| 600 |
) |
| 601 |
return output_str |
| 602 |
|
| 603 |
@staticmethod |
| 604 |
def rgb_hex_to_rgb_list(hex_str) -> list[int]: |
| 605 |
"""Return an RGB color value list from a hex color string.""" |
| 606 |
if hex_str is not None: |
| 607 |
return [int(hex_str[i : i + 2], 16) for i in range(0, len(hex_str), 2)] |
| 608 |
return [] |
| 609 |
|
| 610 |
@staticmethod |
| 611 |
def rgb_list_to_rgb_hex_list(rgb_list) -> hex: |
| 612 |
return [f"{i:02x}" for i in rgb_list] |
| 613 |
|
| 614 |
async def async_on(self, value: Any) -> None: |
| 615 |
if isinstance(value, (list, tuple)): |
| 616 |
if self.color_mode == BleboxColorMode.RGBWW: |
| 617 |
value.insert(3, value.pop()) |
| 618 |
value = "".join(self.rgb_list_to_rgb_hex_list(value)) |
| 619 |
if self.product.type == "dimmerBox": |
| 620 |
if not isinstance(value, int): |
| 621 |
value = int(value, 16) |
| 622 |
if not isinstance(value, type(self._off_value)): |
| 623 |
raise ValueError( |
| 624 |
f"turn_on called with bad parameter ({value} is {type(value)}, compared to {self._off_value} which is " |
| 625 |
f"{type(self._off_value)})" |
| 626 |
) |
| 627 |
|
| 628 |
if value == self._off_value: |
| 629 |
raise ValueError(f"turn_on called with invalid value ({value})") |
| 630 |
|
| 631 |
if self.mask is not None: |
| 632 |
value = self.mask(value) |
| 633 |
|
| 634 |
await self.async_api_command("set", value) |
| 635 |
|
| 636 |
async def async_off(self) -> None: |
| 637 |
if self.raw_value("colorMode") in [5, 6]: |
| 638 |
await self.async_api_command("set", self.mask("0000")) |
| 639 |
elif self.raw_value("colorMode") == 3 and self.product.type != "dimmerBox": |
| 640 |
await self.async_api_command("set", self.mask("00")) |
| 641 |
else: |
| 642 |
await self.async_api_command("set", self._off_value) |