| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import colorsys |
| 4 |
import logging |
| 5 |
import random |
| 6 |
import time |
| 7 |
from collections.abc import Iterable |
| 8 |
from dataclasses import asdict, is_dataclass |
| 9 |
from enum import Enum |
| 10 |
from typing import TYPE_CHECKING, Any, Union |
| 11 |
|
| 12 |
from .const import ( |
| 13 |
ADDRESSABLE_STATE_CHANGE_LATENCY, |
| 14 |
ATTR_MODEL, |
| 15 |
ATTR_MODEL_DESCRIPTION, |
| 16 |
ATTR_MODEL_INFO, |
| 17 |
CHANNEL_STATES, |
| 18 |
COLOR_MODE_CCT, |
| 19 |
COLOR_MODE_DIM, |
| 20 |
COLOR_MODE_RGB, |
| 21 |
COLOR_MODE_RGBW, |
| 22 |
COLOR_MODE_RGBWW, |
| 23 |
COLOR_MODES_RGB, |
| 24 |
COLOR_MODES_RGB_CCT, |
| 25 |
COLOR_MODES_RGB_W, |
| 26 |
DEFAULT_MODE, |
| 27 |
DEFAULT_WHITE_CHANNEL_TYPE, |
| 28 |
EFFECT_MUSIC, |
| 29 |
EFFECT_RANDOM, |
| 30 |
MAX_TEMP, |
| 31 |
MODE_COLOR, |
| 32 |
MODE_CUSTOM, |
| 33 |
MODE_MUSIC, |
| 34 |
MODE_PRESET, |
| 35 |
MODE_SWITCH, |
| 36 |
MODE_WW, |
| 37 |
MODEL_NUMS_SWITCHS, |
| 38 |
NEVER_TIME, |
| 39 |
POWER_STATE_CHANGE_LATENCY, |
| 40 |
PRESET_MUSIC_MODE, |
| 41 |
PRESET_MUSIC_MODE_LEGACY, |
| 42 |
PRESET_MUSIC_MODES, |
| 43 |
PRESET_PATTERN_CHANGE_LATENCY, |
| 44 |
STATE_BLUE, |
| 45 |
STATE_CHANGE_LATENCY, |
| 46 |
STATE_COOL_WHITE, |
| 47 |
STATE_GREEN, |
| 48 |
STATE_POWER_STATE, |
| 49 |
STATE_RED, |
| 50 |
STATE_WARM_WHITE, |
| 51 |
STATIC_MODES, |
| 52 |
WhiteChannelType, |
| 53 |
) |
| 54 |
from .models_db import ( |
| 55 |
BASE_MODE_MAP, |
| 56 |
HARDWARE_MAP, |
| 57 |
LEDENETHardware, |
| 58 |
LEDENETModel, |
| 59 |
get_model, |
| 60 |
is_known_model, |
| 61 |
) |
| 62 |
from .pattern import ( |
| 63 |
ADDRESSABLE_EFFECT_ID_NAME, |
| 64 |
ADDRESSABLE_EFFECT_NAME_ID, |
| 65 |
ASSESSABLE_MULTI_COLOR_ID_NAME, |
| 66 |
CHRISTMAS_ADDRESSABLE_EFFECT_ID_NAME, |
| 67 |
CHRISTMAS_ADDRESSABLE_EFFECT_NAME_ID, |
| 68 |
EFFECT_CUSTOM, |
| 69 |
EFFECT_CUSTOM_CODE, |
| 70 |
EFFECT_ID_NAME, |
| 71 |
EFFECT_ID_NAME_LEGACY_CCT, |
| 72 |
EFFECT_LIST, |
| 73 |
EFFECT_LIST_DIMMABLE, |
| 74 |
EFFECT_LIST_LEGACY_CCT, |
| 75 |
ORIGINAL_ADDRESSABLE_EFFECT_ID_NAME, |
| 76 |
ORIGINAL_ADDRESSABLE_EFFECT_NAME_ID, |
| 77 |
PresetPattern, |
| 78 |
) |
| 79 |
from .protocol import ( |
| 80 |
PROTOCOL_LEDENET_8BYTE, |
| 81 |
PROTOCOL_LEDENET_8BYTE_AUTO_ON, |
| 82 |
PROTOCOL_LEDENET_8BYTE_DIMMABLE_EFFECTS, |
| 83 |
PROTOCOL_LEDENET_9BYTE, |
| 84 |
PROTOCOL_LEDENET_9BYTE_AUTO_ON, |
| 85 |
PROTOCOL_LEDENET_9BYTE_DIMMABLE_EFFECTS, |
| 86 |
PROTOCOL_LEDENET_25BYTE, |
| 87 |
PROTOCOL_LEDENET_ADDRESSABLE_A1, |
| 88 |
PROTOCOL_LEDENET_ADDRESSABLE_A2, |
| 89 |
PROTOCOL_LEDENET_ADDRESSABLE_A3, |
| 90 |
PROTOCOL_LEDENET_ADDRESSABLE_CHRISTMAS, |
| 91 |
PROTOCOL_LEDENET_CCT, |
| 92 |
PROTOCOL_LEDENET_CCT_WRAPPED, |
| 93 |
PROTOCOL_LEDENET_ORIGINAL, |
| 94 |
PROTOCOL_LEDENET_ORIGINAL_CCT, |
| 95 |
PROTOCOL_LEDENET_ORIGINAL_RGBW, |
| 96 |
PROTOCOL_LEDENET_SOCKET, |
| 97 |
LEDENETAddressableDeviceConfiguration, |
| 98 |
LEDENETOriginalRawState, |
| 99 |
LEDENETRawState, |
| 100 |
ProtocolLEDENET8Byte, |
| 101 |
ProtocolLEDENET8ByteAutoOn, |
| 102 |
ProtocolLEDENET8ByteDimmableEffects, |
| 103 |
ProtocolLEDENET9Byte, |
| 104 |
ProtocolLEDENET9ByteAutoOn, |
| 105 |
ProtocolLEDENET9ByteDimmableEffects, |
| 106 |
ProtocolLEDENET25Byte, |
| 107 |
ProtocolLEDENETAddressableA1, |
| 108 |
ProtocolLEDENETAddressableA2, |
| 109 |
ProtocolLEDENETAddressableA3, |
| 110 |
ProtocolLEDENETAddressableChristmas, |
| 111 |
ProtocolLEDENETCCT, |
| 112 |
ProtocolLEDENETCCTWrapped, |
| 113 |
ProtocolLEDENETOriginal, |
| 114 |
ProtocolLEDENETOriginalCCT, |
| 115 |
ProtocolLEDENETOriginalRGBW, |
| 116 |
ProtocolLEDENETSocket, |
| 117 |
RemoteConfig, |
| 118 |
) |
| 119 |
from .scanner import FluxLEDDiscovery, is_legacy_device |
| 120 |
from .timer import BuiltInTimer |
| 121 |
from .utils import scaled_color_temp_to_white_levels, utils, white_levels_to_color_temp |
| 122 |
|
| 123 |
_LOGGER = logging.getLogger(__name__) |
| 124 |
|
| 125 |
|
| 126 |
class DeviceUnavailableException(RuntimeError): |
| 127 |
"""Exception to indicate a device is not available.""" |
| 128 |
|
| 129 |
|
| 130 |
PROTOCOL_PROBES: tuple[type[ProtocolLEDENET8Byte], type[ProtocolLEDENETOriginal]] = ( |
| 131 |
ProtocolLEDENET8Byte, |
| 132 |
ProtocolLEDENETOriginal, |
| 133 |
) |
| 134 |
PROTOCOL_PROBES_LEGACY: tuple[ |
| 135 |
type[ProtocolLEDENETOriginal], type[ProtocolLEDENET8Byte] |
| 136 |
] = (ProtocolLEDENETOriginal, ProtocolLEDENET8Byte) |
| 137 |
|
| 138 |
PROTOCOL_TYPES = Union[ |
| 139 |
ProtocolLEDENET8Byte, |
| 140 |
ProtocolLEDENET8ByteAutoOn, |
| 141 |
ProtocolLEDENET8ByteDimmableEffects, |
| 142 |
ProtocolLEDENET9Byte, |
| 143 |
ProtocolLEDENET9ByteAutoOn, |
| 144 |
ProtocolLEDENET9ByteDimmableEffects, |
| 145 |
ProtocolLEDENET25Byte, |
| 146 |
ProtocolLEDENETAddressableA1, |
| 147 |
ProtocolLEDENETAddressableA2, |
| 148 |
ProtocolLEDENETAddressableA3, |
| 149 |
ProtocolLEDENETOriginal, |
| 150 |
ProtocolLEDENETOriginalCCT, |
| 151 |
ProtocolLEDENETOriginalRGBW, |
| 152 |
ProtocolLEDENETCCT, |
| 153 |
ProtocolLEDENETCCTWrapped, |
| 154 |
ProtocolLEDENETSocket, |
| 155 |
ProtocolLEDENETAddressableChristmas, |
| 156 |
] |
| 157 |
|
| 158 |
ADDRESSABLE_PROTOCOLS = { |
| 159 |
PROTOCOL_LEDENET_ADDRESSABLE_A1, |
| 160 |
PROTOCOL_LEDENET_ADDRESSABLE_A2, |
| 161 |
PROTOCOL_LEDENET_ADDRESSABLE_A3, |
| 162 |
} |
| 163 |
ALL_ADDRESSABLE_PROTOCOLS = ( |
| 164 |
ProtocolLEDENETAddressableA1, |
| 165 |
ProtocolLEDENETAddressableA2, |
| 166 |
ProtocolLEDENETAddressableA3, |
| 167 |
) |
| 168 |
ALL_IC_PROTOCOLS = (ProtocolLEDENETAddressableChristmas, *ALL_ADDRESSABLE_PROTOCOLS) |
| 169 |
CHRISTMAS_EFFECTS_PROTOCOLS = {PROTOCOL_LEDENET_ADDRESSABLE_CHRISTMAS} |
| 170 |
OLD_EFFECTS_PROTOCOLS = {PROTOCOL_LEDENET_ADDRESSABLE_A1} |
| 171 |
NEW_EFFECTS_PROTOCOLS = { |
| 172 |
PROTOCOL_LEDENET_ADDRESSABLE_A2, |
| 173 |
PROTOCOL_LEDENET_ADDRESSABLE_A3, |
| 174 |
} |
| 175 |
SPEED_ADJUST_WILL_TURN_ON = { |
| 176 |
PROTOCOL_LEDENET_ADDRESSABLE_A1, |
| 177 |
PROTOCOL_LEDENET_ADDRESSABLE_A2, |
| 178 |
} |
| 179 |
PROTOCOL_NAME_TO_CLS = { |
| 180 |
PROTOCOL_LEDENET_ORIGINAL: ProtocolLEDENETOriginal, |
| 181 |
PROTOCOL_LEDENET_ORIGINAL_CCT: ProtocolLEDENETOriginalCCT, |
| 182 |
PROTOCOL_LEDENET_ORIGINAL_RGBW: ProtocolLEDENETOriginalRGBW, |
| 183 |
PROTOCOL_LEDENET_8BYTE: ProtocolLEDENET8Byte, |
| 184 |
PROTOCOL_LEDENET_8BYTE_AUTO_ON: ProtocolLEDENET8ByteAutoOn, |
| 185 |
PROTOCOL_LEDENET_8BYTE_DIMMABLE_EFFECTS: ProtocolLEDENET8ByteDimmableEffects, |
| 186 |
PROTOCOL_LEDENET_9BYTE: ProtocolLEDENET9Byte, |
| 187 |
PROTOCOL_LEDENET_9BYTE_AUTO_ON: ProtocolLEDENET9ByteAutoOn, |
| 188 |
PROTOCOL_LEDENET_9BYTE_DIMMABLE_EFFECTS: ProtocolLEDENET9ByteDimmableEffects, |
| 189 |
PROTOCOL_LEDENET_25BYTE: ProtocolLEDENET25Byte, |
| 190 |
PROTOCOL_LEDENET_ADDRESSABLE_A3: ProtocolLEDENETAddressableA3, |
| 191 |
PROTOCOL_LEDENET_ADDRESSABLE_A2: ProtocolLEDENETAddressableA2, |
| 192 |
PROTOCOL_LEDENET_ADDRESSABLE_A1: ProtocolLEDENETAddressableA1, |
| 193 |
PROTOCOL_LEDENET_CCT: ProtocolLEDENETCCT, |
| 194 |
PROTOCOL_LEDENET_CCT_WRAPPED: ProtocolLEDENETCCTWrapped, |
| 195 |
PROTOCOL_LEDENET_SOCKET: ProtocolLEDENETSocket, |
| 196 |
PROTOCOL_LEDENET_ADDRESSABLE_CHRISTMAS: ProtocolLEDENETAddressableChristmas, |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
PATTERN_CODE_TO_EFFECT = { |
| 201 |
PRESET_MUSIC_MODE: MODE_MUSIC, |
| 202 |
PRESET_MUSIC_MODE_LEGACY: MODE_MUSIC, |
| 203 |
EFFECT_CUSTOM_CODE: EFFECT_CUSTOM, |
| 204 |
} |
| 205 |
|
| 206 |
SERIALIZABLE_TYPES = (str, bool, dict, int, float, list, tuple, set) |
| 207 |
|
| 208 |
|
| 209 |
DEFAULT_PORT = 5577 |
| 210 |
ARMACOST_PORT = 34001 |
| 211 |
|
| 212 |
|
| 213 |
class DeviceType(Enum): |
| 214 |
Bulb = 0 |
| 215 |
Switch = 1 |
| 216 |
|
| 217 |
|
| 218 |
class LEDENETDevice: |
| 219 |
"""An LEDENET Device.""" |
| 220 |
|
| 221 |
def __init__( |
| 222 |
self, |
| 223 |
ipaddr: str, |
| 224 |
port: int = 0, |
| 225 |
timeout: float = 5, |
| 226 |
discovery: FluxLEDDiscovery | None = None, |
| 227 |
) -> None: |
| 228 |
"""Init the LEDENEt Device.""" |
| 229 |
self.ipaddr: str = ipaddr |
| 230 |
self._port: int = port |
| 231 |
self.timeout: float = timeout |
| 232 |
self.raw_state: LEDENETOriginalRawState | LEDENETRawState | None = None |
| 233 |
self.available: bool | None = None |
| 234 |
self._model_num: int | None = None |
| 235 |
self._model_data: LEDENETModel | None = None |
| 236 |
self._paired_remotes: int | None = None |
| 237 |
self._remote_config: RemoteConfig | None = None |
| 238 |
self._white_channel_channel_type: WhiteChannelType = DEFAULT_WHITE_CHANNEL_TYPE |
| 239 |
self._discovery = discovery |
| 240 |
self._protocol: PROTOCOL_TYPES | None = None |
| 241 |
self._mode: str | None = None |
| 242 |
self._transition_complete_time: float = 0 |
| 243 |
self._preset_pattern_transition_complete_time: float = 0 |
| 244 |
self._power_state_transition_complete_time: float = 0 |
| 245 |
self._last_effect_brightness: int = 100 |
| 246 |
self._device_config: LEDENETAddressableDeviceConfiguration | None = None |
| 247 |
self._last_message: dict[str, bytes] = {} |
| 248 |
self._unavailable_reason: str | None = None |
| 249 |
|
| 250 |
def _protocol_probes( |
| 251 |
self, |
| 252 |
) -> ( |
| 253 |
tuple[type[ProtocolLEDENETOriginal], type[ProtocolLEDENET8Byte]] |
| 254 |
| tuple[type[ProtocolLEDENET8Byte], type[ProtocolLEDENETOriginal]] |
| 255 |
): |
| 256 |
"""Determine the probe order based on device type.""" |
| 257 |
discovery = self.discovery |
| 258 |
return ( |
| 259 |
PROTOCOL_PROBES_LEGACY if is_legacy_device(discovery) else PROTOCOL_PROBES |
| 260 |
) |
| 261 |
|
| 262 |
@property |
| 263 |
def model_num(self) -> int: |
| 264 |
"""Return the model number.""" |
| 265 |
assert self._model_num is not None |
| 266 |
return self._model_num |
| 267 |
|
| 268 |
@property |
| 269 |
def model_data(self) -> LEDENETModel: |
| 270 |
"""Return the model data.""" |
| 271 |
assert self._model_data is not None |
| 272 |
return self._model_data |
| 273 |
|
| 274 |
@property |
| 275 |
def discovery(self) -> FluxLEDDiscovery | None: |
| 276 |
"""Return the discovery data.""" |
| 277 |
return self._discovery |
| 278 |
|
| 279 |
@discovery.setter |
| 280 |
def discovery(self, value: FluxLEDDiscovery | None) -> None: |
| 281 |
"""Set the discovery data.""" |
| 282 |
self._discovery = value |
| 283 |
|
| 284 |
@property |
| 285 |
def white_channel_channel_type(self) -> WhiteChannelType: |
| 286 |
"""Return the type of the white channel.""" |
| 287 |
return self._white_channel_channel_type |
| 288 |
|
| 289 |
@white_channel_channel_type.setter |
| 290 |
def white_channel_channel_type(self, value: WhiteChannelType) -> None: |
| 291 |
"""Set the type of the white channel.""" |
| 292 |
self._white_channel_channel_type = value |
| 293 |
|
| 294 |
@property |
| 295 |
def hardware(self) -> LEDENETHardware | None: |
| 296 |
"""Retrurn the hardware mapping for the device.""" |
| 297 |
if not self._discovery or ATTR_MODEL not in self._discovery: |
| 298 |
return None |
| 299 |
model = self._discovery.get(ATTR_MODEL) |
| 300 |
if model is None: |
| 301 |
return None |
| 302 |
assert isinstance(model, str) |
| 303 |
return HARDWARE_MAP.get(model) |
| 304 |
|
| 305 |
@property |
| 306 |
def paired_remotes(self) -> int | None: |
| 307 |
"""Return the number of paired remotes or None if not supported.""" |
| 308 |
return self._paired_remotes |
| 309 |
|
| 310 |
@property |
| 311 |
def remote_config(self) -> RemoteConfig | None: |
| 312 |
"""Return the number of remote config or None if not supported.""" |
| 313 |
return self._remote_config |
| 314 |
|
| 315 |
@property |
| 316 |
def speed_adjust_off(self) -> int: |
| 317 |
"""Return true if the speed of an effect can be adjusted while off.""" |
| 318 |
return self.protocol not in SPEED_ADJUST_WILL_TURN_ON |
| 319 |
|
| 320 |
@property |
| 321 |
def _whites_are_temp_brightness(self) -> bool: |
| 322 |
"""Return true if warm_white and cool_white are scaled temp values and not raw 0-255.""" |
| 323 |
return self.protocol in (PROTOCOL_LEDENET_CCT, PROTOCOL_LEDENET_CCT_WRAPPED) |
| 324 |
|
| 325 |
@property |
| 326 |
def model(self) -> str: |
| 327 |
"""Return the human readable model description.""" |
| 328 |
if self._discovery and self._discovery.get(ATTR_MODEL_DESCRIPTION): |
| 329 |
return f"{self._discovery[ATTR_MODEL_DESCRIPTION]} (0x{self.model_num:02X})" |
| 330 |
return f"{self.model_data.description} (0x{self.model_num:02X})" |
| 331 |
|
| 332 |
@property |
| 333 |
def version_num(self) -> int: |
| 334 |
"""Return the version number.""" |
| 335 |
assert self.raw_state is not None |
| 336 |
raw_state = self.raw_state |
| 337 |
if hasattr(raw_state, "version_number"): |
| 338 |
assert isinstance(raw_state, LEDENETRawState) |
| 339 |
return raw_state.version_number |
| 340 |
return 0 |
| 341 |
|
| 342 |
@property |
| 343 |
def preset_pattern_num(self) -> int: |
| 344 |
"""Return the preset pattern number.""" |
| 345 |
assert self.raw_state is not None |
| 346 |
return self.raw_state.preset_pattern |
| 347 |
|
| 348 |
@property |
| 349 |
def rgbwprotocol(self) -> bool: |
| 350 |
"""Devices that don't require a separate rgb/w bit.""" |
| 351 |
return self.rgbwcapable or self.model_data.always_writes_white_and_colors |
| 352 |
|
| 353 |
@property |
| 354 |
def microphone(self) -> bool: |
| 355 |
"""Devices that have a microphone built in.""" |
| 356 |
return self.model_data.microphone |
| 357 |
|
| 358 |
@property |
| 359 |
def rgbwcapable(self) -> bool: |
| 360 |
"""Devices that actually support rgbw.""" |
| 361 |
color_modes = self.color_modes |
| 362 |
return COLOR_MODE_RGBW in color_modes or COLOR_MODE_RGBWW in color_modes |
| 363 |
|
| 364 |
@property |
| 365 |
def device_type(self) -> DeviceType: |
| 366 |
"""Return the device type.""" |
| 367 |
is_switch = self.model_num in MODEL_NUMS_SWITCHS |
| 368 |
return DeviceType.Switch if is_switch else DeviceType.Bulb |
| 369 |
|
| 370 |
@property |
| 371 |
def color_temp(self) -> int: |
| 372 |
"""Return the current color temp in kelvin.""" |
| 373 |
return (self.getWhiteTemperature())[0] |
| 374 |
|
| 375 |
@property |
| 376 |
def min_temp(self) -> int: |
| 377 |
"""Returns the minimum color temp in kelvin.""" |
| 378 |
return int(self._white_channel_channel_type.value) |
| 379 |
|
| 380 |
@property |
| 381 |
def max_temp(self) -> int: |
| 382 |
"""Returns the maximum color temp in kelvin.""" |
| 383 |
return MAX_TEMP |
| 384 |
|
| 385 |
@property |
| 386 |
def _rgbwwprotocol(self) -> bool: |
| 387 |
"""Device that uses the 9-byte protocol.""" |
| 388 |
return self.protocol in ( |
| 389 |
PROTOCOL_LEDENET_9BYTE, |
| 390 |
PROTOCOL_LEDENET_9BYTE_DIMMABLE_EFFECTS, |
| 391 |
) |
| 392 |
|
| 393 |
@property |
| 394 |
def white_active(self) -> bool: |
| 395 |
"""Any white channel is active.""" |
| 396 |
assert self.raw_state is not None |
| 397 |
raw_state = self.raw_state |
| 398 |
return bool(raw_state.warm_white or raw_state.cool_white) |
| 399 |
|
| 400 |
@property |
| 401 |
def color_active(self) -> bool: |
| 402 |
"""Any color channel is active.""" |
| 403 |
assert self.raw_state is not None |
| 404 |
raw_state = self.raw_state |
| 405 |
return bool(raw_state.red or raw_state.green or raw_state.blue) |
| 406 |
|
| 407 |
def rgbw_color_temp_support(self, color_modes: set[str]) -> bool: |
| 408 |
"""RGBW color temp support.""" |
| 409 |
return COLOR_MODE_RGBW in color_modes and self.max_temp != self.min_temp |
| 410 |
|
| 411 |
@property |
| 412 |
def color_is_white_only(self) -> bool: |
| 413 |
"""Return if the curent color is active and white.""" |
| 414 |
assert self.raw_state is not None |
| 415 |
raw_state = self.raw_state |
| 416 |
return bool( |
| 417 |
|
| 418 |
(raw_state.red or raw_state.green or raw_state.blue or raw_state.warm_white) |
| 419 |
|
| 420 |
and raw_state.red == raw_state.green == raw_state.blue |
| 421 |
) |
| 422 |
|
| 423 |
@property |
| 424 |
def multi_color_mode(self) -> bool: |
| 425 |
"""The device supports multiple color modes.""" |
| 426 |
return len(self.color_modes) > 1 |
| 427 |
|
| 428 |
@property |
| 429 |
def color_modes(self) -> set[str]: |
| 430 |
"""The available color modes.""" |
| 431 |
color_modes = self._internal_color_modes |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
if ( |
| 437 |
COLOR_MODE_CCT not in color_modes and COLOR_MODE_RGBWW in color_modes |
| 438 |
) or self.rgbw_color_temp_support(color_modes): |
| 439 |
return {COLOR_MODE_CCT, *color_modes} |
| 440 |
return color_modes |
| 441 |
|
| 442 |
@property |
| 443 |
def _internal_color_modes(self) -> set[str]: |
| 444 |
"""The internal available color modes.""" |
| 445 |
assert self.raw_state is not None |
| 446 |
if ( |
| 447 |
self._device_config is not None |
| 448 |
|
| 449 |
and self._device_config.operating_mode == COLOR_MODE_RGBW |
| 450 |
): |
| 451 |
return {COLOR_MODE_RGBW} |
| 452 |
if not is_known_model(self.model_num): |
| 453 |
|
| 454 |
return BASE_MODE_MAP.get(self.raw_state.mode & 0x0F, {DEFAULT_MODE}) |
| 455 |
model_data = self.model_data |
| 456 |
return model_data.mode_to_color_mode.get( |
| 457 |
self.raw_state.mode, model_data.color_modes |
| 458 |
) |
| 459 |
|
| 460 |
@property |
| 461 |
def pixels_per_segment(self) -> int | None: |
| 462 |
"""Return the pixels per segment.""" |
| 463 |
if self._device_config is None: |
| 464 |
return None |
| 465 |
return self._device_config.pixels_per_segment |
| 466 |
|
| 467 |
@property |
| 468 |
def segments(self) -> int | None: |
| 469 |
"""Return the number of segments.""" |
| 470 |
if self._device_config is None: |
| 471 |
return None |
| 472 |
return self._device_config.segments |
| 473 |
|
| 474 |
@property |
| 475 |
def music_pixels_per_segment(self) -> int | None: |
| 476 |
"""Return the music pixels per segment.""" |
| 477 |
if self._device_config is None: |
| 478 |
return None |
| 479 |
return self._device_config.music_pixels_per_segment |
| 480 |
|
| 481 |
@property |
| 482 |
def music_segments(self) -> int | None: |
| 483 |
"""Return the number of music segments.""" |
| 484 |
if self._device_config is None: |
| 485 |
return None |
| 486 |
return self._device_config.music_segments |
| 487 |
|
| 488 |
@property |
| 489 |
def wiring(self) -> str | None: |
| 490 |
"""Return the sort order as a string.""" |
| 491 |
device_config = self.model_data.device_config |
| 492 |
if not device_config.wiring: |
| 493 |
return None |
| 494 |
if self._device_config: |
| 495 |
return self._device_config.wiring |
| 496 |
assert self.raw_state is not None |
| 497 |
return device_config.num_to_wiring.get(int((self.raw_state.mode & 0xF0) / 16)) |
| 498 |
|
| 499 |
@property |
| 500 |
def wiring_num(self) -> int | None: |
| 501 |
"""Return the wiring number.""" |
| 502 |
if not self.model_data.device_config.wiring: |
| 503 |
return None |
| 504 |
if self._device_config: |
| 505 |
return self._device_config.wiring_num |
| 506 |
assert self.raw_state is not None |
| 507 |
return int((self.raw_state.mode & 0xF0) / 16) |
| 508 |
|
| 509 |
@property |
| 510 |
def wirings(self) -> list[str] | None: |
| 511 |
"""Return available wirings for the device.""" |
| 512 |
device_config = self.model_data.device_config |
| 513 |
if not device_config.wiring: |
| 514 |
return None |
| 515 |
if self._device_config: |
| 516 |
return list(self._device_config.wirings) |
| 517 |
return list(device_config.wiring_to_num) |
| 518 |
|
| 519 |
@property |
| 520 |
def operating_mode(self) -> str | None: |
| 521 |
"""Return the strip mode as a string.""" |
| 522 |
device_config = self.model_data.device_config |
| 523 |
if not device_config.operating_modes: |
| 524 |
return None |
| 525 |
if self._device_config: |
| 526 |
return self._device_config.operating_mode |
| 527 |
assert self.raw_state is not None |
| 528 |
return device_config.num_to_operating_mode.get(self.raw_state.mode & 0x0F) |
| 529 |
|
| 530 |
@property |
| 531 |
def operating_mode_num(self) -> int | None: |
| 532 |
"""Return the strip mode as a string.""" |
| 533 |
if not self.model_data.device_config.operating_modes: |
| 534 |
return None |
| 535 |
assert self.raw_state is not None |
| 536 |
return self.raw_state.mode & 0x0F |
| 537 |
|
| 538 |
@property |
| 539 |
def operating_modes(self) -> list[str] | None: |
| 540 |
"""Return available operating modes for the device.""" |
| 541 |
if not self.model_data.device_config.operating_modes: |
| 542 |
return None |
| 543 |
return list(self.model_data.device_config.operating_mode_to_num) |
| 544 |
|
| 545 |
@property |
| 546 |
def ic_type(self) -> str | None: |
| 547 |
"""Return the strip ictype as a string.""" |
| 548 |
if not self.model_data.device_config.ic_type: |
| 549 |
return None |
| 550 |
assert self._device_config is not None |
| 551 |
return self._device_config.ic_type |
| 552 |
|
| 553 |
@property |
| 554 |
def ic_type_num(self) -> int | None: |
| 555 |
"""Return the strip ictype as an int.""" |
| 556 |
if not self.model_data.device_config.ic_type: |
| 557 |
return None |
| 558 |
assert self._device_config is not None |
| 559 |
return self._device_config.ic_type_num |
| 560 |
|
| 561 |
@property |
| 562 |
def ic_types(self) -> list[str] | None: |
| 563 |
"""Return the ic types.""" |
| 564 |
if not self.model_data.device_config.ic_type: |
| 565 |
return None |
| 566 |
return list(self.model_data.device_config.ic_type_to_num) |
| 567 |
|
| 568 |
@property |
| 569 |
def color_mode(self) -> str | None: |
| 570 |
"""The current color mode.""" |
| 571 |
color_modes = self._internal_color_modes |
| 572 |
if COLOR_MODE_RGBWW in color_modes: |
| 573 |
|
| 574 |
return COLOR_MODE_RGBWW if self.color_active else COLOR_MODE_CCT |
| 575 |
if self.rgbw_color_temp_support(color_modes): |
| 576 |
|
| 577 |
return COLOR_MODE_CCT if self.color_is_white_only else COLOR_MODE_RGBW |
| 578 |
if ( |
| 579 |
color_modes == COLOR_MODES_RGB_CCT |
| 580 |
): |
| 581 |
return COLOR_MODE_CCT if self.white_active else COLOR_MODE_RGB |
| 582 |
if color_modes == COLOR_MODES_RGB_W: |
| 583 |
return COLOR_MODE_DIM if self.white_active else COLOR_MODE_RGB |
| 584 |
if color_modes: |
| 585 |
return next(iter(color_modes)) |
| 586 |
return None |
| 587 |
|
| 588 |
@property |
| 589 |
def protocol(self) -> str | None: |
| 590 |
"""Returns the name of the protocol in use.""" |
| 591 |
if self._protocol is None: |
| 592 |
return None |
| 593 |
return self._protocol.name |
| 594 |
|
| 595 |
@property |
| 596 |
def dimmable_effects(self) -> bool: |
| 597 |
"""Return true of the device supports dimmable effects.""" |
| 598 |
assert self._protocol is not None |
| 599 |
return self._protocol.dimmable_effects |
| 600 |
|
| 601 |
@property |
| 602 |
def requires_turn_on(self) -> bool: |
| 603 |
"""Return true of the device requires a power on command before setting levels/effects.""" |
| 604 |
assert self._protocol is not None |
| 605 |
return self._protocol.requires_turn_on |
| 606 |
|
| 607 |
@property |
| 608 |
def is_on(self) -> bool: |
| 609 |
assert self.raw_state is not None |
| 610 |
assert self._protocol is not None |
| 611 |
return self.raw_state.power_state == self._protocol.on_byte |
| 612 |
|
| 613 |
@property |
| 614 |
def mode(self) -> str | None: |
| 615 |
return self._mode |
| 616 |
|
| 617 |
@property |
| 618 |
def warm_white(self) -> int: |
| 619 |
assert self.raw_state is not None |
| 620 |
return self.raw_state.warm_white if self._rgbwwprotocol else 0 |
| 621 |
|
| 622 |
@property |
| 623 |
def effect_list(self) -> list[str]: |
| 624 |
"""Return the list of available effects.""" |
| 625 |
effects: Iterable[str] = [] |
| 626 |
protocol = self.protocol |
| 627 |
if protocol in OLD_EFFECTS_PROTOCOLS: |
| 628 |
effects = ORIGINAL_ADDRESSABLE_EFFECT_ID_NAME.values() |
| 629 |
elif protocol in NEW_EFFECTS_PROTOCOLS: |
| 630 |
effects = ADDRESSABLE_EFFECT_ID_NAME.values() |
| 631 |
elif protocol in CHRISTMAS_EFFECTS_PROTOCOLS: |
| 632 |
effects = CHRISTMAS_ADDRESSABLE_EFFECT_ID_NAME.values() |
| 633 |
elif COLOR_MODES_RGB.intersection(self.color_modes): |
| 634 |
effects = EFFECT_LIST_DIMMABLE if self.dimmable_effects else EFFECT_LIST |
| 635 |
elif protocol == PROTOCOL_LEDENET_ORIGINAL_CCT: |
| 636 |
effects = EFFECT_LIST_LEGACY_CCT |
| 637 |
if self.microphone: |
| 638 |
return [*effects, EFFECT_RANDOM, EFFECT_MUSIC] |
| 639 |
return [*effects, EFFECT_RANDOM] |
| 640 |
|
| 641 |
@property |
| 642 |
def effect(self) -> str | None: |
| 643 |
"""Return the current effect.""" |
| 644 |
if self.protocol in CHRISTMAS_EFFECTS_PROTOCOLS: |
| 645 |
return self._named_effect |
| 646 |
return PATTERN_CODE_TO_EFFECT.get(self.preset_pattern_num, self._named_effect) |
| 647 |
|
| 648 |
@property |
| 649 |
def _named_effect(self) -> str | None: |
| 650 |
"""Returns the named effect.""" |
| 651 |
assert self.raw_state is not None |
| 652 |
mode = self.raw_state.mode |
| 653 |
pattern_code = self.preset_pattern_num |
| 654 |
protocol = self.protocol |
| 655 |
if protocol in OLD_EFFECTS_PROTOCOLS: |
| 656 |
effect_id = (pattern_code << 8) + mode - 99 |
| 657 |
return ORIGINAL_ADDRESSABLE_EFFECT_ID_NAME.get(effect_id) |
| 658 |
if protocol in NEW_EFFECTS_PROTOCOLS: |
| 659 |
if pattern_code == 0x25: |
| 660 |
return ADDRESSABLE_EFFECT_ID_NAME.get(mode) |
| 661 |
if pattern_code == 0x24: |
| 662 |
return ASSESSABLE_MULTI_COLOR_ID_NAME.get(mode) |
| 663 |
return None |
| 664 |
if protocol in CHRISTMAS_EFFECTS_PROTOCOLS: |
| 665 |
if pattern_code == 0x25: |
| 666 |
return CHRISTMAS_ADDRESSABLE_EFFECT_ID_NAME.get(mode) |
| 667 |
return None |
| 668 |
if protocol == PROTOCOL_LEDENET_ORIGINAL_CCT: |
| 669 |
return EFFECT_ID_NAME_LEGACY_CCT.get(pattern_code) |
| 670 |
return EFFECT_ID_NAME.get(pattern_code) |
| 671 |
|
| 672 |
@property |
| 673 |
def cool_white(self) -> int: |
| 674 |
assert self.raw_state is not None |
| 675 |
if self._rgbwwprotocol: |
| 676 |
return self.raw_state.cool_white |
| 677 |
return 0 |
| 678 |
|
| 679 |
|
| 680 |
@property |
| 681 |
def cold_white(self) -> int: |
| 682 |
return self.cool_white |
| 683 |
|
| 684 |
@property |
| 685 |
def brightness(self) -> int: |
| 686 |
"""Return current brightness 0-255. |
| 687 |
|
| 688 |
For warm white return current led level. For RGB |
| 689 |
calculate the HSV and return the 'value'. |
| 690 |
for CCT calculate the brightness. |
| 691 |
for ww send led level |
| 692 |
""" |
| 693 |
color_mode = self.color_mode |
| 694 |
raw_state = self.raw_state |
| 695 |
|
| 696 |
assert raw_state is not None |
| 697 |
|
| 698 |
if self._named_effect: |
| 699 |
if self.dimmable_effects: |
| 700 |
if ( |
| 701 |
self.protocol in NEW_EFFECTS_PROTOCOLS |
| 702 |
and time.monotonic() > self._transition_complete_time |
| 703 |
): |
| 704 |
|
| 705 |
return min(255, round(raw_state.red * 255 / 100)) |
| 706 |
return round(self._last_effect_brightness * 255 / 100) |
| 707 |
return 255 |
| 708 |
if raw_state.preset_pattern in PRESET_MUSIC_MODES and not self.dimmable_effects: |
| 709 |
return 255 |
| 710 |
if color_mode == COLOR_MODE_DIM: |
| 711 |
return int(raw_state.warm_white) |
| 712 |
if color_mode == COLOR_MODE_CCT: |
| 713 |
_, b = self.getWhiteTemperature() |
| 714 |
return b |
| 715 |
|
| 716 |
r, g, b = self.getRgb() |
| 717 |
_, _, v = colorsys.rgb_to_hsv(r / 255, g / 255, b / 255) |
| 718 |
v_255 = v * 255 |
| 719 |
if color_mode == COLOR_MODE_RGBW: |
| 720 |
return round((v_255 + raw_state.warm_white) / 2) |
| 721 |
if color_mode == COLOR_MODE_RGBWW: |
| 722 |
return round((v_255 + raw_state.warm_white + raw_state.cool_white) / 3) |
| 723 |
|
| 724 |
|
| 725 |
return int(v_255) |
| 726 |
|
| 727 |
def _determineMode(self) -> str | None: |
| 728 |
assert self.raw_state is not None |
| 729 |
pattern_code = self.raw_state.preset_pattern |
| 730 |
if self.device_type == DeviceType.Switch: |
| 731 |
return MODE_SWITCH |
| 732 |
if pattern_code in (0x41, 0x61): |
| 733 |
if self.color_mode in {COLOR_MODE_DIM, COLOR_MODE_CCT}: |
| 734 |
return MODE_WW |
| 735 |
return MODE_COLOR |
| 736 |
if pattern_code == EFFECT_CUSTOM_CODE: |
| 737 |
return ( |
| 738 |
MODE_PRESET |
| 739 |
if self.protocol in CHRISTMAS_EFFECTS_PROTOCOLS |
| 740 |
else MODE_CUSTOM |
| 741 |
) |
| 742 |
if pattern_code in (PRESET_MUSIC_MODE, PRESET_MUSIC_MODE_LEGACY): |
| 743 |
return MODE_MUSIC |
| 744 |
if PresetPattern.valid(pattern_code): |
| 745 |
return MODE_PRESET |
| 746 |
if BuiltInTimer.valid(pattern_code): |
| 747 |
return BuiltInTimer.valtostr(pattern_code) |
| 748 |
if self.protocol in ADDRESSABLE_PROTOCOLS: |
| 749 |
return MODE_PRESET |
| 750 |
return None |
| 751 |
|
| 752 |
@property |
| 753 |
def port(self) -> int: |
| 754 |
"""Return the discovered port.""" |
| 755 |
if self._port: |
| 756 |
return self._port |
| 757 |
if ( |
| 758 |
self._discovery |
| 759 |
and "armacost" in (self._discovery.get(ATTR_MODEL_INFO) or "").lower() |
| 760 |
): |
| 761 |
return ARMACOST_PORT |
| 762 |
return DEFAULT_PORT |
| 763 |
|
| 764 |
def set_unavailable(self, reason: str) -> None: |
| 765 |
_LOGGER.debug("%s: set_unavailable: %s", self.ipaddr, reason) |
| 766 |
self._unavailable_reason = reason |
| 767 |
self.available = False |
| 768 |
|
| 769 |
def set_available(self, reason: str) -> None: |
| 770 |
_LOGGER.debug("%s: set_available: %s", self.ipaddr, reason) |
| 771 |
self._unavailable_reason = None |
| 772 |
self.available = True |
| 773 |
|
| 774 |
def process_device_config_response(self, msg: bytes) -> None: |
| 775 |
"""Process an IC (strip config) response.""" |
| 776 |
assert isinstance(self._protocol, ALL_IC_PROTOCOLS) |
| 777 |
self._device_config = self._protocol.parse_strip_setting(msg) |
| 778 |
_LOGGER.debug("%s: device_config: %s", self.ipaddr, self._device_config) |
| 779 |
|
| 780 |
def process_state_response(self, rx: bytes) -> bool: |
| 781 |
"""Process a state change message.""" |
| 782 |
assert self._protocol is not None |
| 783 |
if not self._protocol.is_valid_state_response(rx): |
| 784 |
_LOGGER.warning( |
| 785 |
"%s: Invalid response: %s", |
| 786 |
self.ipaddr, |
| 787 |
utils.raw_state_to_dec(rx), |
| 788 |
) |
| 789 |
return False |
| 790 |
return self._process_valid_state_response(rx) |
| 791 |
|
| 792 |
def _process_valid_state_response(self, rx: bytes) -> bool: |
| 793 |
assert self._protocol is not None |
| 794 |
raw_state: LEDENETOriginalRawState | LEDENETRawState = ( |
| 795 |
self._protocol.named_raw_state(rx) |
| 796 |
) |
| 797 |
_LOGGER.debug("%s: State: %s", self.ipaddr, raw_state) |
| 798 |
|
| 799 |
if raw_state != self.raw_state: |
| 800 |
_LOGGER.debug( |
| 801 |
"%s: unmapped raw state: %s", |
| 802 |
self.ipaddr, |
| 803 |
utils.raw_state_to_dec(raw_state), |
| 804 |
) |
| 805 |
|
| 806 |
now_time = time.monotonic() |
| 807 |
transition_states = set() |
| 808 |
if now_time < self._power_state_transition_complete_time: |
| 809 |
transition_states.add(STATE_POWER_STATE) |
| 810 |
if now_time < self._transition_complete_time: |
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
transition_states |= CHANNEL_STATES |
| 816 |
if now_time < self._preset_pattern_transition_complete_time: |
| 817 |
transition_states.add("preset_pattern") |
| 818 |
|
| 819 |
if transition_states: |
| 820 |
self._replace_raw_state( |
| 821 |
{ |
| 822 |
name: value |
| 823 |
for name, value in raw_state._asdict().items() |
| 824 |
if name not in transition_states |
| 825 |
} |
| 826 |
) |
| 827 |
else: |
| 828 |
self._set_raw_state(raw_state) |
| 829 |
|
| 830 |
_LOGGER.debug("%s: Mapped State: %s", self.ipaddr, self.raw_state) |
| 831 |
|
| 832 |
mode = self._determineMode() |
| 833 |
|
| 834 |
if mode is None: |
| 835 |
_LOGGER.debug( |
| 836 |
"%s: Unable to determine mode from raw state: %s", |
| 837 |
self.ipaddr, |
| 838 |
utils.raw_state_to_dec(rx), |
| 839 |
) |
| 840 |
return False |
| 841 |
|
| 842 |
self._mode = mode |
| 843 |
return True |
| 844 |
|
| 845 |
def process_power_state_response(self, msg: bytes) -> bool: |
| 846 |
"""Process a power state change message.""" |
| 847 |
assert self._protocol is not None |
| 848 |
if not self._protocol.is_valid_power_state_response(msg): |
| 849 |
_LOGGER.warning( |
| 850 |
"%s: Recieved invalid power state response: %s", |
| 851 |
self.ipaddr, |
| 852 |
utils.raw_state_to_dec(msg), |
| 853 |
) |
| 854 |
return False |
| 855 |
_LOGGER.debug("%s: Setting power state to: %s", self.ipaddr, f"0x{msg[2]:02X}") |
| 856 |
self._set_power_state(msg[2]) |
| 857 |
return True |
| 858 |
|
| 859 |
def process_extended_state_response(self, msg: bytes) -> bool: |
| 860 |
"""Process and extended state response.""" |
| 861 |
assert self._protocol is not None |
| 862 |
self._process_valid_state_response(self._protocol.extended_state_to_state(msg)) |
| 863 |
return True |
| 864 |
|
| 865 |
def _set_raw_state( |
| 866 |
self, |
| 867 |
raw_state: LEDENETOriginalRawState | LEDENETRawState, |
| 868 |
updated: set[str] | None = None, |
| 869 |
) -> None: |
| 870 |
"""Set the raw state remapping channels as needed. |
| 871 |
|
| 872 |
The goal is to normalize the data so the raw state |
| 873 |
is always in the same format reguardless of the protocol |
| 874 |
|
| 875 |
Some devices need to have channels remapped |
| 876 |
|
| 877 |
Other devices uses color_temp/brightness format |
| 878 |
which needs to be converted back to 0-255 values for |
| 879 |
warm_white and cool_white |
| 880 |
""" |
| 881 |
channel_map = self.model_data.channel_map |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
if self._whites_are_temp_brightness: |
| 889 |
assert isinstance(raw_state, LEDENETRawState) |
| 890 |
|
| 891 |
if updated is not None: |
| 892 |
self.raw_state = raw_state |
| 893 |
return |
| 894 |
|
| 895 |
temp = raw_state.warm_white |
| 896 |
|
| 897 |
brightness = raw_state.cool_white |
| 898 |
warm_white, cool_white = scaled_color_temp_to_white_levels(temp, brightness) |
| 899 |
self.raw_state = raw_state._replace( |
| 900 |
warm_white=warm_white, cool_white=cool_white |
| 901 |
) |
| 902 |
return |
| 903 |
|
| 904 |
if channel_map: |
| 905 |
if updated is None: |
| 906 |
updated = set(channel_map.keys()) |
| 907 |
self.raw_state = raw_state._replace( |
| 908 |
**{ |
| 909 |
name: getattr(raw_state, source) |
| 910 |
if source in updated |
| 911 |
else getattr(raw_state, name) |
| 912 |
for name, source in channel_map.items() |
| 913 |
} |
| 914 |
) |
| 915 |
return |
| 916 |
|
| 917 |
if isinstance(self._protocol, ProtocolLEDENETAddressableA3): |
| 918 |
if updated is not None: |
| 919 |
self.raw_state = raw_state |
| 920 |
return |
| 921 |
|
| 922 |
self.raw_state = raw_state._replace( |
| 923 |
warm_white=utils.A3WarmWhiteToByte(raw_state.warm_white) |
| 924 |
) |
| 925 |
return |
| 926 |
|
| 927 |
self.raw_state = raw_state |
| 928 |
|
| 929 |
def __str__(self) -> str: |
| 930 |
assert self.raw_state is not None |
| 931 |
assert self._protocol is not None |
| 932 |
|
| 933 |
rx = self.raw_state |
| 934 |
if not rx: |
| 935 |
return "No state data" |
| 936 |
mode = self.mode |
| 937 |
color_mode = self.color_mode |
| 938 |
power_str = "Unknown power state" |
| 939 |
if rx.power_state == self._protocol.on_byte: |
| 940 |
power_str = "ON " |
| 941 |
elif rx.power_state == self._protocol.off_byte: |
| 942 |
power_str = "OFF " |
| 943 |
|
| 944 |
if mode in STATIC_MODES: |
| 945 |
if color_mode in COLOR_MODES_RGB: |
| 946 |
mode_str = f"Color: {(rx.red, rx.green, rx.blue)}" |
| 947 |
|
| 948 |
if self.rgbwcapable: |
| 949 |
mode_str += f" White: {rx.warm_white}" |
| 950 |
else: |
| 951 |
mode_str += f" Brightness: {round(self.brightness * 100 / 255)}%" |
| 952 |
elif color_mode == COLOR_MODE_DIM: |
| 953 |
mode_str = f"Warm White: {utils.byteToPercent(rx.warm_white)}%" |
| 954 |
elif color_mode == COLOR_MODE_CCT: |
| 955 |
cct_value = self.getWhiteTemperature() |
| 956 |
mode_str = f"CCT: {cct_value[0]}K Brightness: {round(cct_value[1] * 100 / 255)}%" |
| 957 |
elif mode == MODE_PRESET: |
| 958 |
mode_str = f"Pattern: {self.effect} (Speed {self.speed}%)" |
| 959 |
elif mode == MODE_CUSTOM: |
| 960 |
mode_str = f"Custom pattern (Speed {self.speed}%)" |
| 961 |
elif BuiltInTimer.valid(rx.preset_pattern): |
| 962 |
mode_str = BuiltInTimer.valtostr(rx.preset_pattern) |
| 963 |
elif mode == MODE_MUSIC: |
| 964 |
mode_str = "Music" |
| 965 |
elif mode == MODE_SWITCH: |
| 966 |
mode_str = "Switch" |
| 967 |
else: |
| 968 |
mode_str = f"Unknown mode 0x{rx.preset_pattern:x}" |
| 969 |
mode_str += " raw state: " |
| 970 |
mode_str += utils.raw_state_to_dec(rx) |
| 971 |
return f"{power_str} [{mode_str}]" |
| 972 |
|
| 973 |
def _set_power_state(self, new_power_state: int) -> None: |
| 974 |
"""Set the power state in the raw state.""" |
| 975 |
self._replace_raw_state({"power_state": new_power_state}) |
| 976 |
self._set_transition_complete_time() |
| 977 |
|
| 978 |
def _replace_raw_state(self, new_states: dict[str, int]) -> None: |
| 979 |
assert self.raw_state is not None |
| 980 |
_LOGGER.debug("%s: _replace_raw_state: %s", self.ipaddr, new_states) |
| 981 |
self._set_raw_state( |
| 982 |
self.raw_state._replace(**new_states), set(new_states.keys()) |
| 983 |
) |
| 984 |
|
| 985 |
def isOn(self) -> bool: |
| 986 |
return self.is_on |
| 987 |
|
| 988 |
def getWarmWhite255(self) -> int: |
| 989 |
if self.color_mode not in {COLOR_MODE_CCT, COLOR_MODE_DIM}: |
| 990 |
return 255 |
| 991 |
return self.brightness |
| 992 |
|
| 993 |
def getWhiteTemperature(self) -> tuple[int, int]: |
| 994 |
"""Returns the color temp and brightness""" |
| 995 |
|
| 996 |
|
| 997 |
assert self.raw_state is not None |
| 998 |
raw_state = self.raw_state |
| 999 |
warm_white = raw_state.warm_white |
| 1000 |
if self.rgbw_color_temp_support(self.color_modes): |
| 1001 |
cool_white = raw_state.red if self.color_is_white_only else 0 |
| 1002 |
else: |
| 1003 |
cool_white = raw_state.cool_white |
| 1004 |
temp, brightness = white_levels_to_color_temp( |
| 1005 |
warm_white, cool_white, self.min_temp, self.max_temp |
| 1006 |
) |
| 1007 |
return temp, brightness |
| 1008 |
|
| 1009 |
def getRgbw(self) -> tuple[int, int, int, int]: |
| 1010 |
"""Returns red,green,blue,white (usually warm).""" |
| 1011 |
if self.color_mode not in COLOR_MODES_RGB: |
| 1012 |
return (255, 255, 255, 255) |
| 1013 |
return self.rgbw |
| 1014 |
|
| 1015 |
@property |
| 1016 |
def rgbw(self) -> tuple[int, int, int, int]: |
| 1017 |
"""Returns red,green,blue,white (usually warm).""" |
| 1018 |
assert self.raw_state is not None |
| 1019 |
raw_state = self.raw_state |
| 1020 |
return ( |
| 1021 |
raw_state.red, |
| 1022 |
raw_state.green, |
| 1023 |
raw_state.blue, |
| 1024 |
raw_state.warm_white, |
| 1025 |
) |
| 1026 |
|
| 1027 |
def getRgbww(self) -> tuple[int, int, int, int, int]: |
| 1028 |
"""Returns red,green,blue,warm,cool.""" |
| 1029 |
if self.color_mode not in COLOR_MODES_RGB: |
| 1030 |
return (255, 255, 255, 255, 255) |
| 1031 |
return self.rgbww |
| 1032 |
|
| 1033 |
@property |
| 1034 |
def rgbww(self) -> tuple[int, int, int, int, int]: |
| 1035 |
"""Returns red,green,blue,warm,cool.""" |
| 1036 |
raw_state = self.raw_state |
| 1037 |
assert raw_state is not None |
| 1038 |
return ( |
| 1039 |
raw_state.red, |
| 1040 |
raw_state.green, |
| 1041 |
raw_state.blue, |
| 1042 |
raw_state.warm_white, |
| 1043 |
raw_state.cool_white, |
| 1044 |
) |
| 1045 |
|
| 1046 |
def getRgbcw(self) -> tuple[int, int, int, int, int]: |
| 1047 |
"""Returns red,green,blue,cool,warm.""" |
| 1048 |
if self.color_mode not in COLOR_MODES_RGB: |
| 1049 |
return (255, 255, 255, 255, 255) |
| 1050 |
return self.rgbcw |
| 1051 |
|
| 1052 |
@property |
| 1053 |
def rgbcw(self) -> tuple[int, int, int, int, int]: |
| 1054 |
"""Returns red,green,blue,cool,warm.""" |
| 1055 |
raw_state = self.raw_state |
| 1056 |
assert raw_state is not None |
| 1057 |
return ( |
| 1058 |
raw_state.red, |
| 1059 |
raw_state.green, |
| 1060 |
raw_state.blue, |
| 1061 |
raw_state.cool_white, |
| 1062 |
raw_state.warm_white, |
| 1063 |
) |
| 1064 |
|
| 1065 |
def getCCT(self) -> tuple[int, int]: |
| 1066 |
if self.color_mode != COLOR_MODE_CCT: |
| 1067 |
return (255, 255) |
| 1068 |
raw_state = self.raw_state |
| 1069 |
assert raw_state is not None |
| 1070 |
return (raw_state.warm_white, raw_state.cool_white) |
| 1071 |
|
| 1072 |
@property |
| 1073 |
def speed(self) -> int: |
| 1074 |
assert self.raw_state is not None |
| 1075 |
if self._protocol is not None and not self._protocol.speed_is_delay: |
| 1076 |
return self.raw_state.speed |
| 1077 |
if self.protocol in CHRISTMAS_EFFECTS_PROTOCOLS: |
| 1078 |
return utils.delayToSpeed(self.raw_state.green) |
| 1079 |
return utils.delayToSpeed(self.raw_state.speed) |
| 1080 |
|
| 1081 |
def getSpeed(self) -> int: |
| 1082 |
return self.speed |
| 1083 |
|
| 1084 |
def _generate_random_levels_change(self) -> tuple[list[bytearray], dict[str, int]]: |
| 1085 |
"""Generate a random levels change.""" |
| 1086 |
channels = {STATE_WARM_WHITE} |
| 1087 |
if COLOR_MODES_RGB.intersection(self.color_modes): |
| 1088 |
channels = {STATE_RED, STATE_GREEN, STATE_BLUE} |
| 1089 |
elif COLOR_MODE_CCT in self.color_modes: |
| 1090 |
channels = {STATE_WARM_WHITE, STATE_COOL_WHITE} |
| 1091 |
return self._generate_levels_change( |
| 1092 |
{ |
| 1093 |
channel: random.randint(0, 255) if channel in channels else None |
| 1094 |
for channel in CHANNEL_STATES |
| 1095 |
} |
| 1096 |
) |
| 1097 |
|
| 1098 |
def _generate_levels_change( |
| 1099 |
self, |
| 1100 |
channels: dict[str, int | None], |
| 1101 |
persist: bool = True, |
| 1102 |
brightness: int | None = None, |
| 1103 |
) -> tuple[list[bytearray], dict[str, int]]: |
| 1104 |
"""Generate the levels change request.""" |
| 1105 |
channel_map = self.model_data.channel_map |
| 1106 |
if channel_map: |
| 1107 |
mapped_channels = { |
| 1108 |
channel: channels[channel_map.get(channel, channel)] |
| 1109 |
for channel in channels |
| 1110 |
} |
| 1111 |
else: |
| 1112 |
mapped_channels = channels |
| 1113 |
|
| 1114 |
r = mapped_channels[STATE_RED] |
| 1115 |
g = mapped_channels[STATE_GREEN] |
| 1116 |
b = mapped_channels[STATE_BLUE] |
| 1117 |
w = mapped_channels[STATE_WARM_WHITE] |
| 1118 |
w2 = mapped_channels[STATE_COOL_WHITE] |
| 1119 |
|
| 1120 |
if (r or g or b) and (w or w2) and not self.rgbwcapable: |
| 1121 |
raise ValueError("RGB&CW command sent to non-RGB&CW device") |
| 1122 |
|
| 1123 |
if brightness is not None and r is not None and g is not None and b is not None: |
| 1124 |
(r, g, b) = self._calculateBrightness((r, g, b), brightness) |
| 1125 |
|
| 1126 |
r_value = None if r is None else int(r) |
| 1127 |
g_value = None if g is None else int(g) |
| 1128 |
b_value = None if b is None else int(b) |
| 1129 |
w_value = None if w is None else int(w) |
| 1130 |
|
| 1131 |
if w2 is None: |
| 1132 |
if w is not None and self.color_mode in {COLOR_MODE_CCT, COLOR_MODE_RGBWW}: |
| 1133 |
|
| 1134 |
w2_value: int | None = self.cold_white |
| 1135 |
else: |
| 1136 |
|
| 1137 |
w2_value = w_value |
| 1138 |
else: |
| 1139 |
w2_value = int(w2) |
| 1140 |
|
| 1141 |
|
| 1142 |
if TYPE_CHECKING: |
| 1143 |
assert self._protocol is not None, "Protocol should not be None" |
| 1144 |
level_write_mode = self._protocol.level_write_modes |
| 1145 |
|
| 1146 |
write_mode = level_write_mode.ALL |
| 1147 |
|
| 1148 |
if not self.rgbwprotocol: |
| 1149 |
if w is None and w2 is None: |
| 1150 |
write_mode = level_write_mode.COLORS |
| 1151 |
elif r is None and g is None and b is None: |
| 1152 |
write_mode = level_write_mode.WHITES |
| 1153 |
|
| 1154 |
assert self._protocol is not None |
| 1155 |
msgs = self._protocol.construct_levels_change( |
| 1156 |
persist, r_value, g_value, b_value, w_value, w2_value, write_mode |
| 1157 |
) |
| 1158 |
updates = {} |
| 1159 |
multi_mode = self.multi_color_mode |
| 1160 |
if multi_mode or write_mode in self._protocol.get_write_all_colors(): |
| 1161 |
updates.update( |
| 1162 |
{"red": r_value or 0, "green": g_value or 0, "blue": b_value or 0} |
| 1163 |
) |
| 1164 |
if multi_mode or write_mode in self._protocol.get_write_all_whites(): |
| 1165 |
updates.update({"warm_white": w_value or 0, "cool_white": w2_value or 0}) |
| 1166 |
return msgs, updates |
| 1167 |
|
| 1168 |
def _set_transition_complete_time(self) -> None: |
| 1169 |
"""Set the time we expect the transition will be completed. |
| 1170 |
|
| 1171 |
Devices fade to a specific state so we want to avoid |
| 1172 |
consuming state updates into self.raw_state while a transition |
| 1173 |
is in progress as this will provide unexpected results |
| 1174 |
and the brightness values will be wrong until |
| 1175 |
the transition completes. |
| 1176 |
""" |
| 1177 |
assert self.raw_state is not None |
| 1178 |
latency = STATE_CHANGE_LATENCY |
| 1179 |
if self.protocol in ADDRESSABLE_PROTOCOLS: |
| 1180 |
latency = ADDRESSABLE_STATE_CHANGE_LATENCY |
| 1181 |
transition_time = latency + utils.speedToDelay(self.raw_state.speed) / 100 |
| 1182 |
self._transition_complete_time = time.monotonic() + transition_time |
| 1183 |
_LOGGER.debug( |
| 1184 |
"%s: Transition time is %s, set _transition_complete_time to %s", |
| 1185 |
self.ipaddr, |
| 1186 |
transition_time, |
| 1187 |
self._transition_complete_time, |
| 1188 |
) |
| 1189 |
|
| 1190 |
self._preset_pattern_transition_complete_time = NEVER_TIME |
| 1191 |
|
| 1192 |
def _set_preset_pattern_transition_complete_time(self) -> None: |
| 1193 |
"""Set the time we expect the preset_pattern transition will be completed.""" |
| 1194 |
assert self.raw_state is not None |
| 1195 |
self._preset_pattern_transition_complete_time = ( |
| 1196 |
time.monotonic() + PRESET_PATTERN_CHANGE_LATENCY |
| 1197 |
) |
| 1198 |
_LOGGER.debug( |
| 1199 |
"%s: Mode transition time is %s, set _preset_pattern_transition_complete_time to %s", |
| 1200 |
self.ipaddr, |
| 1201 |
PRESET_PATTERN_CHANGE_LATENCY, |
| 1202 |
self._preset_pattern_transition_complete_time, |
| 1203 |
) |
| 1204 |
|
| 1205 |
def _set_power_transition_complete_time(self) -> None: |
| 1206 |
"""Set the time we expect the power transition will be completed.""" |
| 1207 |
assert self.raw_state is not None |
| 1208 |
self._power_state_transition_complete_time = ( |
| 1209 |
time.monotonic() + POWER_STATE_CHANGE_LATENCY |
| 1210 |
) |
| 1211 |
_LOGGER.debug( |
| 1212 |
"%s: Mode transition time is %s, set _power_state_transition_complete_time to %s", |
| 1213 |
self.ipaddr, |
| 1214 |
POWER_STATE_CHANGE_LATENCY, |
| 1215 |
self._power_state_transition_complete_time, |
| 1216 |
) |
| 1217 |
|
| 1218 |
def getRgb(self) -> tuple[int, int, int]: |
| 1219 |
if self.color_mode not in COLOR_MODES_RGB: |
| 1220 |
return (255, 255, 255) |
| 1221 |
return self.rgb |
| 1222 |
|
| 1223 |
@property |
| 1224 |
def rgb(self) -> tuple[int, int, int]: |
| 1225 |
assert self.raw_state is not None |
| 1226 |
raw_state = self.raw_state |
| 1227 |
return (raw_state.red, raw_state.green, raw_state.blue) |
| 1228 |
|
| 1229 |
@property |
| 1230 |
def rgb_unscaled(self) -> tuple[int, int, int]: |
| 1231 |
"""Return the unscaled RGB.""" |
| 1232 |
r, g, b = self.rgb |
| 1233 |
hsv = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0) |
| 1234 |
r_p, g_p, b_p = colorsys.hsv_to_rgb(hsv[0], hsv[1], 1) |
| 1235 |
return round(r_p * 255), round(g_p * 255), round(b_p * 255) |
| 1236 |
|
| 1237 |
def _calculateBrightness( |
| 1238 |
self, rgb: tuple[int, int, int], level: int |
| 1239 |
) -> tuple[int, int, int]: |
| 1240 |
hsv = colorsys.rgb_to_hsv(*rgb) |
| 1241 |
r, g, b = colorsys.hsv_to_rgb(hsv[0], hsv[1], level) |
| 1242 |
return int(r), int(g), int(b) |
| 1243 |
|
| 1244 |
def setProtocol(self, protocol: str) -> None: |
| 1245 |
cls = PROTOCOL_NAME_TO_CLS.get(protocol) |
| 1246 |
if cls is None: |
| 1247 |
raise ValueError(f"Invalid protocol: {protocol}") |
| 1248 |
self._protocol = cls() |
| 1249 |
|
| 1250 |
def _set_protocol_from_msg( |
| 1251 |
self, |
| 1252 |
full_msg: bytes, |
| 1253 |
fallback_protocol: str, |
| 1254 |
) -> None: |
| 1255 |
self._model_num = full_msg[1] |
| 1256 |
self._model_data = get_model(self._model_num, fallback_protocol) |
| 1257 |
version_num = full_msg[10] if len(full_msg) > 10 else 1 |
| 1258 |
self.setProtocol(self._model_data.protocol_for_version_num(version_num)) |
| 1259 |
|
| 1260 |
def _generate_preset_pattern( |
| 1261 |
self, pattern: int, speed: int, brightness: int |
| 1262 |
) -> bytearray: |
| 1263 |
"""Generate the preset pattern protocol bytes.""" |
| 1264 |
protocol = self.protocol |
| 1265 |
if protocol in OLD_EFFECTS_PROTOCOLS: |
| 1266 |
if pattern not in ORIGINAL_ADDRESSABLE_EFFECT_ID_NAME: |
| 1267 |
raise ValueError("Pattern must be between 1 and 302") |
| 1268 |
elif protocol in NEW_EFFECTS_PROTOCOLS: |
| 1269 |
if pattern not in ADDRESSABLE_EFFECT_ID_NAME: |
| 1270 |
raise ValueError("Pattern must be between 1 and 100") |
| 1271 |
elif protocol in CHRISTMAS_EFFECTS_PROTOCOLS: |
| 1272 |
if pattern not in CHRISTMAS_ADDRESSABLE_EFFECT_ID_NAME: |
| 1273 |
raise ValueError("Pattern must be between 1 and 100") |
| 1274 |
else: |
| 1275 |
PresetPattern.valid_or_raise(pattern) |
| 1276 |
if not (1 <= brightness <= 100): |
| 1277 |
raise ValueError("Brightness must be between 1 and 100") |
| 1278 |
self._last_effect_brightness = brightness |
| 1279 |
assert self._protocol is not None |
| 1280 |
return self._protocol.construct_preset_pattern(pattern, speed, brightness) |
| 1281 |
|
| 1282 |
def _generate_custom_patterm( |
| 1283 |
self, rgb_list: list[tuple[int, int, int]], speed: int, transition_type: str |
| 1284 |
) -> bytearray: |
| 1285 |
"""Generate the custom pattern protocol bytes.""" |
| 1286 |
|
| 1287 |
if len(rgb_list) > 16: |
| 1288 |
_LOGGER.warning( |
| 1289 |
"Too many colors in %s, truncating list to %s", len(rgb_list), 16 |
| 1290 |
) |
| 1291 |
del rgb_list[16:] |
| 1292 |
|
| 1293 |
if len(rgb_list) == 0: |
| 1294 |
raise ValueError("setCustomPattern requires at least one color tuples") |
| 1295 |
|
| 1296 |
assert self._protocol is not None |
| 1297 |
return self._protocol.construct_custom_effect(rgb_list, speed, transition_type) |
| 1298 |
|
| 1299 |
def _effect_to_pattern(self, effect: str) -> int: |
| 1300 |
"""Convert an effect to a pattern code.""" |
| 1301 |
protocol = self.protocol |
| 1302 |
if protocol in CHRISTMAS_EFFECTS_PROTOCOLS: |
| 1303 |
return CHRISTMAS_ADDRESSABLE_EFFECT_NAME_ID[effect] |
| 1304 |
if protocol in NEW_EFFECTS_PROTOCOLS: |
| 1305 |
return ADDRESSABLE_EFFECT_NAME_ID[effect] |
| 1306 |
if protocol in OLD_EFFECTS_PROTOCOLS: |
| 1307 |
return ORIGINAL_ADDRESSABLE_EFFECT_NAME_ID[effect] |
| 1308 |
return PresetPattern.str_to_val(effect) |
| 1309 |
|
| 1310 |
@property |
| 1311 |
def diagnostics(self) -> dict[str, Any]: |
| 1312 |
"""Return diagnostics for the device.""" |
| 1313 |
data: dict[str, Any] = {"device_state": {}, "last_messages": {}} |
| 1314 |
last_messages = data["last_messages"] |
| 1315 |
for name, msg in self._last_message.items(): |
| 1316 |
last_messages[name] = " ".join(f"0x{x:02X}" for x in msg) |
| 1317 |
device_state = data["device_state"] |
| 1318 |
for name in dir(self): |
| 1319 |
if name.startswith("_") or name == "diagnostics" or not hasattr(self, name): |
| 1320 |
continue |
| 1321 |
value: Any = getattr(self, name) |
| 1322 |
if is_dataclass(value): |
| 1323 |
value = asdict(value) |
| 1324 |
if hasattr(value, "value"): |
| 1325 |
value = value.value |
| 1326 |
if value is None or isinstance(value, SERIALIZABLE_TYPES): |
| 1327 |
device_state[name] = value |
| 1328 |
return data |