mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 11:33:11 -04:00
Use keyword parameters to parse the LIFX color changes (#195997)
This commit is contained in:
@@ -147,7 +147,7 @@ class LIFXManager:
|
||||
|
||||
@staticmethod
|
||||
async def hsbk_from_service_data(
|
||||
device: Light, service_data: Mapping[str, object]
|
||||
device: Light, service_data: Mapping[str, Any]
|
||||
) -> HSBK | None:
|
||||
"""Merge the requested color components onto a device's current color."""
|
||||
changes = parse_hsbk_changes(**service_data)
|
||||
|
||||
@@ -7,17 +7,7 @@ from typing import TYPE_CHECKING, Any, TypedDict
|
||||
|
||||
from lifx import HSBK, Colors, LifxError, Theme, ThemeLibrary
|
||||
|
||||
from homeassistant.components.light import (
|
||||
ATTR_BRIGHTNESS,
|
||||
ATTR_BRIGHTNESS_PCT,
|
||||
ATTR_BRIGHTNESS_STEP,
|
||||
ATTR_BRIGHTNESS_STEP_PCT,
|
||||
ATTR_COLOR_NAME,
|
||||
ATTR_COLOR_TEMP_KELVIN,
|
||||
ATTR_HS_COLOR,
|
||||
ATTR_RGB_COLOR,
|
||||
ATTR_XY_COLOR,
|
||||
)
|
||||
from homeassistant.components.light import LightServiceArgument
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
|
||||
from homeassistant.util import color as color_util
|
||||
@@ -123,14 +113,24 @@ class HSBKChanges(TypedDict):
|
||||
kelvin: int | None
|
||||
|
||||
|
||||
def parse_hsbk_changes(**kwargs: Any) -> HSBKChanges:
|
||||
def parse_hsbk_changes(
|
||||
*,
|
||||
color_name: str | None = None,
|
||||
hs_color: tuple[float, float] | None = None,
|
||||
rgb_color: tuple[int, int, int] | None = None,
|
||||
xy_color: tuple[float, float] | None = None,
|
||||
color_temp_kelvin: int | None = None,
|
||||
brightness: int | None = None,
|
||||
brightness_pct: float | None = None,
|
||||
**_: Any,
|
||||
) -> HSBKChanges:
|
||||
"""Return the public-unit color components a service call asks to change."""
|
||||
hue: float | None = None
|
||||
saturation: float | None = None
|
||||
brightness: float | None = None
|
||||
fraction: float | None = None
|
||||
kelvin: int | None = None
|
||||
|
||||
if (color_name := kwargs.get(ATTR_COLOR_NAME)) is not None:
|
||||
if color_name is not None:
|
||||
try:
|
||||
hue, saturation = color_util.color_RGB_to_hs(
|
||||
*color_util.color_name_to_rgb(color_name)
|
||||
@@ -141,29 +141,29 @@ def parse_hsbk_changes(**kwargs: Any) -> HSBKChanges:
|
||||
)
|
||||
hue, saturation = (0.0, 0.0)
|
||||
|
||||
if ATTR_HS_COLOR in kwargs:
|
||||
hue, saturation = kwargs[ATTR_HS_COLOR]
|
||||
elif ATTR_RGB_COLOR in kwargs:
|
||||
hue, saturation = color_util.color_RGB_to_hs(*kwargs[ATTR_RGB_COLOR])
|
||||
elif ATTR_XY_COLOR in kwargs:
|
||||
hue, saturation = color_util.color_xy_to_hs(*kwargs[ATTR_XY_COLOR])
|
||||
if hs_color is not None:
|
||||
hue, saturation = hs_color
|
||||
elif rgb_color is not None:
|
||||
hue, saturation = color_util.color_RGB_to_hs(*rgb_color)
|
||||
elif xy_color is not None:
|
||||
hue, saturation = color_util.color_xy_to_hs(*xy_color)
|
||||
|
||||
if hue is not None:
|
||||
assert saturation is not None
|
||||
saturation /= 100
|
||||
kelvin = Colors.WHITE_NEUTRAL.kelvin
|
||||
|
||||
if ATTR_COLOR_TEMP_KELVIN in kwargs:
|
||||
kelvin = kwargs[ATTR_COLOR_TEMP_KELVIN]
|
||||
if color_temp_kelvin is not None:
|
||||
kelvin = color_temp_kelvin
|
||||
saturation = 0.0
|
||||
|
||||
if ATTR_BRIGHTNESS in kwargs:
|
||||
brightness = kwargs[ATTR_BRIGHTNESS] / 255
|
||||
elif ATTR_BRIGHTNESS_PCT in kwargs:
|
||||
brightness = kwargs[ATTR_BRIGHTNESS_PCT] / 100
|
||||
if brightness is not None:
|
||||
fraction = brightness / 255
|
||||
elif brightness_pct is not None:
|
||||
fraction = brightness_pct / 100
|
||||
|
||||
return HSBKChanges(
|
||||
hue=hue, saturation=saturation, brightness=brightness, kelvin=kelvin
|
||||
hue=hue, saturation=saturation, brightness=fraction, kelvin=kelvin
|
||||
)
|
||||
|
||||
|
||||
@@ -225,13 +225,17 @@ def palette_fraction(value: float) -> float:
|
||||
|
||||
def resolve_brightness_step(kwargs: dict[str, Any], current_brightness: int) -> None:
|
||||
"""Turn a relative brightness step into the absolute brightness it asks for."""
|
||||
if ATTR_BRIGHTNESS_STEP in kwargs:
|
||||
brightness = current_brightness + kwargs.pop(ATTR_BRIGHTNESS_STEP)
|
||||
elif ATTR_BRIGHTNESS_STEP_PCT in kwargs:
|
||||
if LightServiceArgument.BRIGHTNESS_STEP in kwargs:
|
||||
brightness = current_brightness + kwargs.pop(
|
||||
LightServiceArgument.BRIGHTNESS_STEP
|
||||
)
|
||||
elif LightServiceArgument.BRIGHTNESS_STEP_PCT in kwargs:
|
||||
brightness_pct = round(current_brightness / 255 * 100)
|
||||
brightness = round(
|
||||
(brightness_pct + kwargs.pop(ATTR_BRIGHTNESS_STEP_PCT)) / 100 * 255
|
||||
(brightness_pct + kwargs.pop(LightServiceArgument.BRIGHTNESS_STEP_PCT))
|
||||
/ 100
|
||||
* 255
|
||||
)
|
||||
else:
|
||||
return
|
||||
kwargs[ATTR_BRIGHTNESS] = max(0, min(255, brightness))
|
||||
kwargs[LightServiceArgument.BRIGHTNESS] = max(0, min(255, brightness))
|
||||
|
||||
Reference in New Issue
Block a user