1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
|
"""
Module for managing a light via KNX.
It provides functionality for
* switching light 'on' and 'off'.
* setting the brightness.
* setting the color.
* setting the relative color temperature (tunable white).
* setting the absolute color temperature.
* reading the current state from KNX bus.
"""
from __future__ import annotations
import asyncio
from collections.abc import Iterator
from enum import Enum
from itertools import chain
import logging
from typing import TYPE_CHECKING, Any, cast
from xknx.dpt import RGBColor, RGBWColor, XYYColor
from xknx.remote_value import (
GroupAddressesType,
RemoteValue,
RemoteValueColorRGB,
RemoteValueColorRGBW,
RemoteValueColorXYY,
RemoteValueNumeric,
RemoteValueScaling,
RemoteValueSwitch,
)
from xknx.remote_value.remote_value import RVCallbackType
from .device import Device, DeviceCallbackType
if TYPE_CHECKING:
from xknx.telegram import Telegram
from xknx.xknx import XKNX
logger = logging.getLogger("xknx.log")
class ColorTemperatureType(Enum):
"""DPT used for absolute color temperature."""
UINT_2_BYTE = "color_temperature" # DPTColorTemperature 7.600
FLOAT_2_BYTE = "2byte_float" # DPT2ByteFloat generic 9
class _SwitchAndBrightness:
def __init__(
self,
xknx: XKNX,
name: str,
feature_name: str,
group_address_switch: GroupAddressesType = None,
group_address_switch_state: GroupAddressesType = None,
group_address_brightness: GroupAddressesType = None,
group_address_brightness_state: GroupAddressesType = None,
sync_state: bool | int | float | str = True,
after_update_cb: RVCallbackType[bool | int] | None = None,
) -> None:
self.switch = RemoteValueSwitch(
xknx,
group_address_switch,
group_address_switch_state,
sync_state=sync_state,
device_name=name,
feature_name=f"{feature_name}_state",
after_update_cb=after_update_cb,
)
self.brightness = RemoteValueScaling(
xknx,
group_address_brightness,
group_address_brightness_state,
sync_state=sync_state,
device_name=name,
feature_name=f"{feature_name}_brightness",
after_update_cb=after_update_cb,
range_from=0,
range_to=255,
)
@property
def is_on(self) -> bool | None:
"""Return if light is on."""
if self.switch.initialized:
return self.switch.value
if self.brightness.initialized and self.brightness.value is not None:
return bool(self.brightness.value)
return None
def set_on(self) -> None:
"""Switch light on."""
if self.switch.writable:
self.switch.on()
return
if self.brightness.writable:
self.brightness.set(self.brightness.range_to)
def set_off(self) -> None:
"""Switch light off."""
if self.switch.writable:
self.switch.off()
return
if self.brightness.writable:
self.brightness.set(0)
def __eq__(self, other: object) -> bool:
"""Compare for equality."""
return self.__dict__ == other.__dict__
class Light(Device):
"""Class for managing a light."""
DEBOUNCE_TIMEOUT = 0.2
DEFAULT_MIN_KELVIN = 2700 # 370 mireds
DEFAULT_MAX_KELVIN = 6000 # 166 mireds
def __init__(
self,
xknx: XKNX,
name: str,
group_address_switch: GroupAddressesType = None,
group_address_switch_state: GroupAddressesType = None,
group_address_brightness: GroupAddressesType = None,
group_address_brightness_state: GroupAddressesType = None,
group_address_color: GroupAddressesType = None,
group_address_color_state: GroupAddressesType = None,
group_address_rgbw: GroupAddressesType = None,
group_address_rgbw_state: GroupAddressesType = None,
group_address_hue: GroupAddressesType = None,
group_address_hue_state: GroupAddressesType = None,
group_address_saturation: GroupAddressesType = None,
group_address_saturation_state: GroupAddressesType = None,
group_address_xyy_color: GroupAddressesType = None,
group_address_xyy_color_state: GroupAddressesType = None,
group_address_tunable_white: GroupAddressesType = None,
group_address_tunable_white_state: GroupAddressesType = None,
group_address_color_temperature: GroupAddressesType = None,
group_address_color_temperature_state: GroupAddressesType = None,
group_address_switch_red: GroupAddressesType = None,
group_address_switch_red_state: GroupAddressesType = None,
group_address_brightness_red: GroupAddressesType = None,
group_address_brightness_red_state: GroupAddressesType = None,
group_address_switch_green: GroupAddressesType = None,
group_address_switch_green_state: GroupAddressesType = None,
group_address_brightness_green: GroupAddressesType = None,
group_address_brightness_green_state: GroupAddressesType = None,
group_address_switch_blue: GroupAddressesType = None,
group_address_switch_blue_state: GroupAddressesType = None,
group_address_brightness_blue: GroupAddressesType = None,
group_address_brightness_blue_state: GroupAddressesType = None,
group_address_switch_white: GroupAddressesType = None,
group_address_switch_white_state: GroupAddressesType = None,
group_address_brightness_white: GroupAddressesType = None,
group_address_brightness_white_state: GroupAddressesType = None,
color_temperature_type: ColorTemperatureType = ColorTemperatureType.UINT_2_BYTE,
sync_state: bool | int | float | str = True,
min_kelvin: int | None = None,
max_kelvin: int | None = None,
device_updated_cb: DeviceCallbackType[Light] | None = None,
) -> None:
"""Initialize Light class."""
super().__init__(xknx, name, device_updated_cb)
self.switch = RemoteValueSwitch(
xknx,
group_address_switch,
group_address_switch_state,
sync_state=sync_state,
device_name=self.name,
feature_name="State",
after_update_cb=self.after_update,
)
self.brightness = RemoteValueScaling(
xknx,
group_address_brightness,
group_address_brightness_state,
sync_state=sync_state,
device_name=self.name,
feature_name="Brightness",
after_update_cb=self.after_update,
range_from=0,
range_to=255,
)
self.color = RemoteValueColorRGB(
xknx,
group_address_color,
group_address_color_state,
sync_state=sync_state,
device_name=self.name,
feature_name="Color RGB",
after_update_cb=self.after_update,
)
self.rgbw = RemoteValueColorRGBW(
xknx,
group_address_rgbw,
group_address_rgbw_state,
sync_state=sync_state,
device_name=self.name,
feature_name="Color RGBW",
after_update_cb=self.after_update,
)
self.hue = RemoteValueNumeric(
xknx,
group_address_hue,
group_address_hue_state,
sync_state=sync_state,
value_type="angle",
device_name=self.name,
feature_name="Hue",
after_update_cb=self.after_update,
)
self.saturation = RemoteValueNumeric(
xknx,
group_address_saturation,
group_address_saturation_state,
sync_state=sync_state,
value_type="percent",
device_name=self.name,
feature_name="Saturation",
after_update_cb=self.after_update,
)
self._xyy_color_valid: XYYColor | None = None
self.xyy_color = RemoteValueColorXYY(
xknx,
group_address_xyy_color,
group_address_xyy_color_state,
sync_state=sync_state,
device_name=self.name,
feature_name="Color XYY",
after_update_cb=self._xyy_color_from_rv,
)
self.tunable_white = RemoteValueScaling(
xknx,
group_address_tunable_white,
group_address_tunable_white_state,
sync_state=sync_state,
device_name=self.name,
feature_name="Tunable white",
after_update_cb=self.after_update,
range_from=0,
range_to=255,
)
self.color_temperature = RemoteValueNumeric(
xknx,
group_address_color_temperature,
group_address_color_temperature_state,
sync_state=sync_state,
value_type=color_temperature_type.value,
device_name=self.name,
feature_name="Color temperature",
after_update_cb=self.after_update,
)
self.red = _SwitchAndBrightness(
xknx,
self.name,
"red",
group_address_switch_red,
group_address_switch_red_state,
group_address_brightness_red,
group_address_brightness_red_state,
sync_state=sync_state,
after_update_cb=self._individual_color_callback_debounce,
)
self.green = _SwitchAndBrightness(
xknx,
self.name,
"green",
group_address_switch_green,
group_address_switch_green_state,
group_address_brightness_green,
group_address_brightness_green_state,
sync_state=sync_state,
after_update_cb=self._individual_color_callback_debounce,
)
self.blue = _SwitchAndBrightness(
xknx,
self.name,
"blue",
group_address_switch_blue,
group_address_switch_blue_state,
group_address_brightness_blue,
group_address_brightness_blue_state,
sync_state=sync_state,
after_update_cb=self._individual_color_callback_debounce,
)
self.white = _SwitchAndBrightness(
xknx,
self.name,
"white",
group_address_switch_white,
group_address_switch_white_state,
group_address_brightness_white,
group_address_brightness_white_state,
sync_state=sync_state,
after_update_cb=self._individual_color_callback_debounce,
)
self.min_kelvin = min_kelvin
self.max_kelvin = max_kelvin
self._individual_color_debounce_task_name = (
f"{id(self)}_individual_color_debounce"
)
self._individual_color_debounce_telegram_counter: int
self._reset_individual_color_debounce_telegrams()
def _iter_remote_values(self) -> Iterator[RemoteValue[Any]]:
"""Iterate the devices RemoteValue classes."""
return chain(
self._iter_instant_remote_values(),
self._iter_debounce_remote_values(),
)
def _iter_instant_remote_values(self) -> Iterator[RemoteValue[Any]]:
"""Iterate the devices RemoteValue classes calling after_update_cb immediately."""
yield self.switch
yield self.brightness
yield self.color
yield self.rgbw
yield self.hue
yield self.saturation
yield self.xyy_color
yield self.tunable_white
yield self.color_temperature
def _iter_debounce_remote_values(self) -> Iterator[RemoteValue[Any]]:
"""Iterate the devices RemoteValue classes debouncing after_update_cb."""
for color in self._iter_individual_colors():
yield color.switch
yield color.brightness
def _iter_individual_colors(self) -> Iterator[_SwitchAndBrightness]:
"""Iterate the devices individual colors."""
yield from (self.red, self.green, self.blue, self.white)
def _reset_individual_color_debounce_telegrams(self) -> None:
"""Reset individual color debounce telegram counter."""
self._individual_color_debounce_telegram_counter = sum(
(
self.red.switch.initialized or self.red.brightness.initialized,
self.green.switch.initialized or self.green.brightness.initialized,
self.blue.switch.initialized or self.blue.brightness.initialized,
self.white.switch.initialized or self.white.brightness.initialized,
)
)
def _individual_color_callback_debounce(self, *args: Any) -> None:
"""Run callback after all individual colors were updated or timeout passed."""
async def debouncer() -> None:
await asyncio.sleep(Light.DEBOUNCE_TIMEOUT)
self._reset_individual_color_debounce_telegrams()
self.after_update()
self._individual_color_debounce_telegram_counter -= 1
if self._individual_color_debounce_telegram_counter > 0:
# task registry cancels existing task
self.xknx.task_registry.register(
name=self._individual_color_debounce_task_name,
async_func=debouncer,
).start()
return
self.xknx.task_registry.unregister(self._individual_color_debounce_task_name)
self._reset_individual_color_debounce_telegrams()
self.after_update()
@property
def supports_brightness(self) -> bool:
"""Return if light supports brightness."""
return self.brightness.initialized
@property
def supports_color(self) -> bool:
"""Return if light supports color."""
return self.color.initialized or all(
c.brightness.initialized for c in (self.red, self.green, self.blue)
)
@property
def supports_rgbw(self) -> bool:
"""Return if light supports RGBW."""
return self.rgbw.initialized or all(
c.brightness.initialized for c in self._iter_individual_colors()
)
@property
def supports_hs_color(self) -> bool:
"""Return if light supports HS-color."""
return self.hue.initialized and self.saturation.initialized
@property
def supports_xyy_color(self) -> bool:
"""Return if light supports xyY-color."""
return self.xyy_color.initialized
@property
def supports_tunable_white(self) -> bool:
"""Return if light supports tunable white / relative color temperature."""
return self.tunable_white.initialized
@property
def supports_color_temperature(self) -> bool:
"""Return if light supports absolute color temperature."""
return self.color_temperature.initialized
@property
def state(self) -> bool | None:
"""Return the current switch state of the device."""
if self.switch.value is not None:
return self.switch.value
if any(c.is_on is not None for c in self._iter_individual_colors()):
return any(c.is_on for c in self._iter_individual_colors())
return None
async def set_on(self) -> None:
"""Switch light on."""
if self.switch.writable:
self.switch.on()
return
for color in self._iter_individual_colors():
color.set_on()
async def set_off(self) -> None:
"""Switch light off."""
if self.switch.writable:
self.switch.off()
return
for color in self._iter_individual_colors():
color.set_off()
@property
def current_brightness(self) -> int | None:
"""Return current brightness of light between 0..255."""
return self.brightness.value
async def set_brightness(self, brightness: int) -> None:
"""Set brightness of light."""
if not self.supports_brightness:
logger.warning("Dimming not supported for device %s", self.get_name())
return
self.brightness.set(brightness)
@property
def current_color(self) -> tuple[tuple[int, int, int] | None, int | None]:
"""
Return current color of light.
If the device supports RGBW, get the current RGB+White values instead.
"""
if self.supports_rgbw and self.rgbw.initialized:
if not self.rgbw.value:
return None, None
if (
self.rgbw.value.red is not None
and self.rgbw.value.green is not None
and self.rgbw.value.blue is not None
):
return (
(self.rgbw.value.red, self.rgbw.value.green, self.rgbw.value.blue),
self.rgbw.value.white,
)
return None, self.rgbw.value.white
if self.color.initialized:
if self.color.value is None:
return None, None
return (
self.color.value.red,
self.color.value.green,
self.color.value.blue,
), None
# individual RGB addresses - white will return None when it is not initialized
colors = (
self.red.brightness.value,
self.green.brightness.value,
self.blue.brightness.value,
)
if None in colors:
return None, self.white.brightness.value
return cast(tuple[int, int, int], colors), self.white.brightness.value
async def set_color(
self, color: tuple[int, int, int], white: int | None = None
) -> None:
"""
Set color of a light device.
If also the white value is given and the device supports RGBW,
set all four values.
"""
if white is not None:
if self.supports_rgbw:
if self.rgbw.initialized:
self.rgbw.set(RGBWColor(*color, white))
return
if all(
c.brightness.initialized for c in self._iter_individual_colors()
):
self.red.brightness.set(color[0])
self.green.brightness.set(color[1])
self.blue.brightness.set(color[2])
self.white.brightness.set(white)
return
logger.warning("RGBW not supported for device %s", self.get_name())
else:
if self.supports_color:
if self.color.initialized:
self.color.set(RGBColor(*color))
return
if all(
c.brightness.initialized for c in (self.red, self.green, self.blue)
):
self.red.brightness.set(color[0])
self.green.brightness.set(color[1])
self.blue.brightness.set(color[2])
return
logger.warning("Colors not supported for device %s", self.get_name())
@property
def current_hs_color(self) -> tuple[float, float] | None:
"""
Return current HS-color of the light.
Hue is scaled 0-360 (265 possible values from KNX)
Sat is scaled 0-100
"""
if (hue := self.hue.value) is not None and (
(saturation := self.saturation.value) is not None
):
return (hue, saturation)
return None
async def set_hs_color(self, hs_color: tuple[float, float]) -> None:
"""Set HS-color of the light."""
if not self.supports_hs_color:
logger.warning("HS-color not supported for device %s", self.get_name())
return
value_sent = False
if (hue := hs_color[0]) != self.hue.value:
self.hue.set(hue)
value_sent = True
if (saturation := hs_color[1]) != self.saturation.value:
self.saturation.set(saturation)
value_sent = True
if not value_sent:
# at least one value shall be sent to enable turn-on by hs_color
self.hue.set(hue)
self.saturation.set(saturation)
def _xyy_color_from_rv(self, xyy_color: XYYColor) -> None:
"""Update the current xyY-color from RemoteValue (Callback)."""
if self._xyy_color_valid is not None:
self._xyy_color_valid = self._xyy_color_valid | xyy_color
else:
self._xyy_color_valid = xyy_color
self.after_update()
@property
def current_xyy_color(self) -> XYYColor | None:
"""Return current xyY-color of the light."""
return self._xyy_color_valid
async def set_xyy_color(self, xyy: XYYColor) -> None:
"""Set xyY-color of the light."""
if not self.supports_xyy_color:
logger.warning("XYY-color not supported for device %s", self.get_name())
return
self.xyy_color.set(xyy)
@property
def current_tunable_white(self) -> int | None:
"""Return current relative color temperature of light."""
return self.tunable_white.value
async def set_tunable_white(self, tunable_white: int) -> None:
"""Set relative color temperature of light."""
if not self.supports_tunable_white:
logger.warning("Tunable white not supported for device %s", self.get_name())
return
self.tunable_white.set(tunable_white)
@property
def current_color_temperature(self) -> int | float | None:
"""Return current absolute color temperature of light."""
return self.color_temperature.value
async def set_color_temperature(self, color_temperature: int) -> None:
"""Set absolute color temperature of light."""
if not self.supports_color_temperature:
logger.warning(
"Absolute Color Temperature not supported for device %s",
self.get_name(),
)
return
self.color_temperature.set(color_temperature)
def process_group_write(self, telegram: Telegram) -> None:
"""Process incoming and outgoing GROUP WRITE telegram."""
for remote_value in self._iter_instant_remote_values():
remote_value.process(telegram)
for remote_value in self._iter_debounce_remote_values():
remote_value.process(telegram, always_callback=True)
def __str__(self) -> str:
"""Return object as readable string."""
str_brightness = (
f" brightness={self.brightness.group_addr_str()}"
if self.supports_brightness
else ""
)
str_color = (
f" color={self.color.group_addr_str()}" if self.supports_color else ""
)
str_rgbw = f" rgbw={self.rgbw.group_addr_str()}" if self.supports_rgbw else ""
str_hue = (
f" brightness={self.hue.group_addr_str()}" if self.hue.initialized else ""
)
str_saturation = (
f" brightness={self.saturation.group_addr_str()}"
if self.saturation.initialized
else ""
)
str_xyy_color = (
f" xyy_color={self.xyy_color.group_addr_str()}"
if self.supports_xyy_color
else ""
)
str_tunable_white = (
f" tunable_white={self.tunable_white.group_addr_str()}"
if self.supports_tunable_white
else ""
)
str_color_temperature = (
f" color_temperature={self.color_temperature.group_addr_str()}"
if self.supports_color_temperature
else ""
)
str_red_state = (
f" red_state={self.red.switch.group_addr_str()}"
if self.red.switch.initialized
else ""
)
str_red_brightness = (
f" red_brightness={self.red.brightness.group_addr_str()}"
if self.red.brightness.initialized
else ""
)
str_green_state = (
f" green_state={self.green.switch.group_addr_str()}"
if self.green.switch.initialized
else ""
)
str_green_brightness = (
f" green_brightness={self.green.brightness.group_addr_str()}"
if self.green.brightness.initialized
else ""
)
str_blue_state = (
f" blue_state={self.blue.switch.group_addr_str()}"
if self.blue.switch.initialized
else ""
)
str_blue_brightness = (
f" blue_brightness={self.blue.brightness.group_addr_str()}"
if self.blue.brightness.initialized
else ""
)
str_white_state = (
f" white_state={self.white.switch.group_addr_str()}"
if self.white.switch.initialized
else ""
)
str_white_brightness = (
f" white_brightness={self.white.brightness.group_addr_str()}"
if self.white.brightness.initialized
else ""
)
return (
f'<Light name="{self.name}" '
f"switch={self.switch.group_addr_str()}"
f"{str_brightness}"
f"{str_color}"
f"{str_rgbw}"
f"{str_hue}"
f"{str_saturation}"
f"{str_xyy_color}"
f"{str_tunable_white}"
f"{str_color_temperature}"
f"{str_red_state}"
f"{str_red_brightness}"
f"{str_green_state}"
f"{str_green_brightness}"
f"{str_blue_state}"
f"{str_blue_brightness}"
f"{str_white_state}"
f"{str_white_brightness}"
" />"
)
|