Add Option For Kelvin Unit To Color Temperature Selector (#103799)

This commit is contained in:
schelv 2023-11-29 12:25:06 +01:00 committed by GitHub
parent d9c0acc1d2
commit 31cab5803c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 10 deletions

View File

@ -252,8 +252,9 @@ turn_on:
- light.ColorMode.RGBWW - light.ColorMode.RGBWW
selector: selector:
color_temp: color_temp:
min_mireds: 153 unit: "mired"
max_mireds: 500 min: 153
max: 500
kelvin: kelvin:
filter: filter:
attribute: attribute:
@ -266,11 +267,10 @@ turn_on:
- light.ColorMode.RGBWW - light.ColorMode.RGBWW
advanced: true advanced: true
selector: selector:
number: color_temp:
unit: "kelvin"
min: 2000 min: 2000
max: 6500 max: 6500
step: 100
unit_of_measurement: K
brightness: brightness:
filter: filter:
attribute: attribute:
@ -637,11 +637,10 @@ toggle:
- light.ColorMode.RGBWW - light.ColorMode.RGBWW
advanced: true advanced: true
selector: selector:
number: color_temp:
unit: "kelvin"
min: 2000 min: 2000
max: 6500 max: 6500
step: 100
unit_of_measurement: K
brightness: brightness:
filter: filter:
attribute: attribute:

View File

@ -425,10 +425,20 @@ class ColorRGBSelector(Selector[ColorRGBSelectorConfig]):
class ColorTempSelectorConfig(TypedDict, total=False): class ColorTempSelectorConfig(TypedDict, total=False):
"""Class to represent a color temp selector config.""" """Class to represent a color temp selector config."""
unit: ColorTempSelectorUnit
min: int
max: int
max_mireds: int max_mireds: int
min_mireds: int min_mireds: int
class ColorTempSelectorUnit(StrEnum):
"""Possible units for a color temperature selector."""
KELVIN = "kelvin"
MIRED = "mired"
@SELECTORS.register("color_temp") @SELECTORS.register("color_temp")
class ColorTempSelector(Selector[ColorTempSelectorConfig]): class ColorTempSelector(Selector[ColorTempSelectorConfig]):
"""Selector of an color temperature.""" """Selector of an color temperature."""
@ -437,6 +447,11 @@ class ColorTempSelector(Selector[ColorTempSelectorConfig]):
CONFIG_SCHEMA = vol.Schema( CONFIG_SCHEMA = vol.Schema(
{ {
vol.Optional("unit", default=ColorTempSelectorUnit.MIRED): vol.All(
vol.Coerce(ColorTempSelectorUnit), lambda val: val.value
),
vol.Optional("min"): vol.Coerce(int),
vol.Optional("max"): vol.Coerce(int),
vol.Optional("max_mireds"): vol.Coerce(int), vol.Optional("max_mireds"): vol.Coerce(int),
vol.Optional("min_mireds"): vol.Coerce(int), vol.Optional("min_mireds"): vol.Coerce(int),
} }
@ -448,11 +463,20 @@ class ColorTempSelector(Selector[ColorTempSelectorConfig]):
def __call__(self, data: Any) -> int: def __call__(self, data: Any) -> int:
"""Validate the passed selection.""" """Validate the passed selection."""
range_min = self.config.get("min")
range_max = self.config.get("max")
if not range_min:
range_min = self.config.get("min_mireds")
if not range_max:
range_max = self.config.get("max_mireds")
value: int = vol.All( value: int = vol.All(
vol.Coerce(float), vol.Coerce(float),
vol.Range( vol.Range(
min=self.config.get("min_mireds"), min=range_min,
max=self.config.get("max_mireds"), max=range_max,
), ),
)(data) )(data)
return value return value

View File

@ -907,6 +907,16 @@ def test_rgb_color_selector_schema(
(100, 200), (100, 200),
(99, 201), (99, 201),
), ),
(
{"unit": "mired", "min": 100, "max": 200},
(100, 200),
(99, 201),
),
(
{"unit": "kelvin", "min": 1000, "max": 2000},
(1000, 2000),
(999, 2001),
),
), ),
) )
def test_color_tempselector_schema( def test_color_tempselector_schema(