Migrate blebox lights to use Kelvin (#132787)

This commit is contained in:
epenet 2024-12-10 08:43:07 +01:00 committed by GitHub
parent cd420aee88
commit a11bf5cce1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -11,7 +11,7 @@ from blebox_uniapi.light import BleboxColorMode
from homeassistant.components.light import ( from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP, ATTR_COLOR_TEMP_KELVIN,
ATTR_EFFECT, ATTR_EFFECT,
ATTR_RGB_COLOR, ATTR_RGB_COLOR,
ATTR_RGBW_COLOR, ATTR_RGBW_COLOR,
@ -22,6 +22,7 @@ from homeassistant.components.light import (
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import color as color_util
from . import BleBoxConfigEntry from . import BleBoxConfigEntry
from .entity import BleBoxEntity from .entity import BleBoxEntity
@ -58,8 +59,8 @@ COLOR_MODE_MAP = {
class BleBoxLightEntity(BleBoxEntity[blebox_uniapi.light.Light], LightEntity): class BleBoxLightEntity(BleBoxEntity[blebox_uniapi.light.Light], LightEntity):
"""Representation of BleBox lights.""" """Representation of BleBox lights."""
_attr_max_mireds = 370 # 1,000,000 divided by 2700 Kelvin = 370 Mireds _attr_min_color_temp_kelvin = 2700 # 370 Mireds
_attr_min_mireds = 154 # 1,000,000 divided by 6500 Kelvin = 154 Mireds _attr_max_color_temp_kelvin = 6500 # 154 Mireds
def __init__(self, feature: blebox_uniapi.light.Light) -> None: def __init__(self, feature: blebox_uniapi.light.Light) -> None:
"""Initialize a BleBox light.""" """Initialize a BleBox light."""
@ -78,9 +79,9 @@ class BleBoxLightEntity(BleBoxEntity[blebox_uniapi.light.Light], LightEntity):
return self._feature.brightness return self._feature.brightness
@property @property
def color_temp(self): def color_temp_kelvin(self) -> int:
"""Return color temperature.""" """Return the color temperature value in Kelvin."""
return self._feature.color_temp return color_util.color_temperature_mired_to_kelvin(self._feature.color_temp)
@property @property
def color_mode(self): def color_mode(self):
@ -136,7 +137,7 @@ class BleBoxLightEntity(BleBoxEntity[blebox_uniapi.light.Light], LightEntity):
rgbw = kwargs.get(ATTR_RGBW_COLOR) rgbw = kwargs.get(ATTR_RGBW_COLOR)
brightness = kwargs.get(ATTR_BRIGHTNESS) brightness = kwargs.get(ATTR_BRIGHTNESS)
effect = kwargs.get(ATTR_EFFECT) effect = kwargs.get(ATTR_EFFECT)
color_temp = kwargs.get(ATTR_COLOR_TEMP) color_temp_kelvin = kwargs.get(ATTR_COLOR_TEMP_KELVIN)
rgbww = kwargs.get(ATTR_RGBWW_COLOR) rgbww = kwargs.get(ATTR_RGBWW_COLOR)
feature = self._feature feature = self._feature
value = feature.sensible_on_value value = feature.sensible_on_value
@ -144,9 +145,10 @@ class BleBoxLightEntity(BleBoxEntity[blebox_uniapi.light.Light], LightEntity):
if rgbw is not None: if rgbw is not None:
value = list(rgbw) value = list(rgbw)
if color_temp is not None: if color_temp_kelvin is not None:
value = feature.return_color_temp_with_brightness( value = feature.return_color_temp_with_brightness(
int(color_temp), self.brightness int(color_util.color_temperature_kelvin_to_mired(color_temp_kelvin)),
self.brightness,
) )
if rgbww is not None: if rgbww is not None:
@ -158,9 +160,12 @@ class BleBoxLightEntity(BleBoxEntity[blebox_uniapi.light.Light], LightEntity):
value = list(rgb) value = list(rgb)
if brightness is not None: if brightness is not None:
if self.color_mode == ATTR_COLOR_TEMP: if self.color_mode == ColorMode.COLOR_TEMP:
value = feature.return_color_temp_with_brightness( value = feature.return_color_temp_with_brightness(
self.color_temp, brightness color_util.color_temperature_kelvin_to_mired(
self.color_temp_kelvin
),
brightness,
) )
else: else:
value = feature.apply_brightness(value, brightness) value = feature.apply_brightness(value, brightness)