From d724488376b868a39670d53ef0a77ef03d41f448 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 10 Dec 2024 10:29:32 +0100 Subject: [PATCH] Migrate yeelight lights to use Kelvin (#132814) --- homeassistant/components/yeelight/light.py | 37 +++---- tests/components/yeelight/test_light.py | 111 ++++++++------------- 2 files changed, 58 insertions(+), 90 deletions(-) diff --git a/homeassistant/components/yeelight/light.py b/homeassistant/components/yeelight/light.py index 7f705da68d1..8cc3f2600e5 100644 --- a/homeassistant/components/yeelight/light.py +++ b/homeassistant/components/yeelight/light.py @@ -16,7 +16,7 @@ from yeelight.main import BulbException from homeassistant.components.light import ( ATTR_BRIGHTNESS, - ATTR_COLOR_TEMP, + ATTR_COLOR_TEMP_KELVIN, ATTR_EFFECT, ATTR_FLASH, ATTR_HS_COLOR, @@ -39,10 +39,6 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.event import async_call_later from homeassistant.helpers.typing import VolDictType import homeassistant.util.color as color_util -from homeassistant.util.color import ( - color_temperature_kelvin_to_mired as kelvin_to_mired, - color_temperature_mired_to_kelvin as mired_to_kelvin, -) from . import YEELIGHT_FLOW_TRANSITION_SCHEMA from .const import ( @@ -440,8 +436,8 @@ class YeelightBaseLight(YeelightEntity, LightEntity): self._effect = None model_specs = self._bulb.get_model_specs() - self._attr_min_mireds = kelvin_to_mired(model_specs["color_temp"]["max"]) - self._attr_max_mireds = kelvin_to_mired(model_specs["color_temp"]["min"]) + self._attr_max_color_temp_kelvin = model_specs["color_temp"]["max"] + self._attr_min_color_temp_kelvin = model_specs["color_temp"]["min"] self._light_type = LightType.Main @@ -476,10 +472,10 @@ class YeelightBaseLight(YeelightEntity, LightEntity): return self._predefined_effects + self.custom_effects_names @property - def color_temp(self) -> int | None: - """Return the color temperature.""" + def color_temp_kelvin(self) -> int | None: + """Return the color temperature value in Kelvin.""" if temp_in_k := self._get_property("ct"): - self._color_temp = kelvin_to_mired(int(temp_in_k)) + self._color_temp = int(temp_in_k) return self._color_temp @property @@ -678,20 +674,19 @@ class YeelightBaseLight(YeelightEntity, LightEntity): ) @_async_cmd - async def async_set_colortemp(self, colortemp, duration) -> None: + async def async_set_colortemp(self, temp_in_k, duration) -> None: """Set bulb's color temperature.""" if ( - not colortemp + not temp_in_k or not self.supported_color_modes or ColorMode.COLOR_TEMP not in self.supported_color_modes ): return - temp_in_k = mired_to_kelvin(colortemp) if ( not self.device.is_color_flow_enabled and self.color_mode == ColorMode.COLOR_TEMP - and self.color_temp == colortemp + and self.color_temp_kelvin == temp_in_k ): _LOGGER.debug("Color temp already set to: %s", temp_in_k) # Already set, and since we get pushed updates @@ -779,7 +774,7 @@ class YeelightBaseLight(YeelightEntity, LightEntity): async def async_turn_on(self, **kwargs: Any) -> None: """Turn the bulb on.""" brightness = kwargs.get(ATTR_BRIGHTNESS) - colortemp = kwargs.get(ATTR_COLOR_TEMP) + colortemp = kwargs.get(ATTR_COLOR_TEMP_KELVIN) hs_color = kwargs.get(ATTR_HS_COLOR) rgb = kwargs.get(ATTR_RGB_COLOR) flash = kwargs.get(ATTR_FLASH) @@ -933,12 +928,12 @@ class YeelightWithoutNightlightSwitchMixIn(YeelightBaseLight): return super()._brightness_property @property - def color_temp(self) -> int | None: - """Return the color temperature.""" + def color_temp_kelvin(self) -> int | None: + """Return the color temperature value in Kelvin.""" if self.device.is_nightlight_enabled: # Enabling the nightlight locks the colortemp to max - return self.max_mireds - return super().color_temp + return self.min_color_temp_kelvin + return super().color_temp_kelvin class YeelightColorLightWithoutNightlightSwitch( @@ -1081,8 +1076,8 @@ class YeelightAmbientLight(YeelightColorLightWithoutNightlightSwitch): def __init__(self, *args, **kwargs): """Initialize the Yeelight Ambient light.""" super().__init__(*args, **kwargs) - self._attr_min_mireds = kelvin_to_mired(6500) - self._attr_max_mireds = kelvin_to_mired(1700) + self._attr_max_color_temp_kelvin = 6500 + self._attr_min_color_temp_kelvin = 1700 self._light_type = LightType.Ambient diff --git a/tests/components/yeelight/test_light.py b/tests/components/yeelight/test_light.py index 518537262b2..f4ff82e7757 100644 --- a/tests/components/yeelight/test_light.py +++ b/tests/components/yeelight/test_light.py @@ -35,6 +35,7 @@ from homeassistant.components.light import ( FLASH_SHORT, SERVICE_TURN_OFF, SERVICE_TURN_ON, + ColorMode, LightEntityFeature, ) from homeassistant.components.yeelight.const import ( @@ -931,9 +932,7 @@ async def test_device_types( "effect": None, "supported_features": SUPPORT_YEELIGHT, "min_color_temp_kelvin": model_specs["color_temp"]["min"], - "max_color_temp_kelvin": color_temperature_mired_to_kelvin( - color_temperature_kelvin_to_mired(model_specs["color_temp"]["max"]) - ), + "max_color_temp_kelvin": model_specs["color_temp"]["max"], "min_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["max"] ), @@ -962,9 +961,7 @@ async def test_device_types( "rgb_color": (255, 121, 0), "xy_color": (0.62, 0.368), "min_color_temp_kelvin": model_specs["color_temp"]["min"], - "max_color_temp_kelvin": color_temperature_mired_to_kelvin( - color_temperature_kelvin_to_mired(model_specs["color_temp"]["max"]) - ), + "max_color_temp_kelvin": model_specs["color_temp"]["max"], "min_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["max"] ), @@ -992,9 +989,7 @@ async def test_device_types( "effect": None, "supported_features": SUPPORT_YEELIGHT, "min_color_temp_kelvin": model_specs["color_temp"]["min"], - "max_color_temp_kelvin": color_temperature_mired_to_kelvin( - color_temperature_kelvin_to_mired(model_specs["color_temp"]["max"]) - ), + "max_color_temp_kelvin": model_specs["color_temp"]["max"], "min_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["max"] ), @@ -1028,9 +1023,7 @@ async def test_device_types( "effect": None, "supported_features": SUPPORT_YEELIGHT, "min_color_temp_kelvin": model_specs["color_temp"]["min"], - "max_color_temp_kelvin": color_temperature_mired_to_kelvin( - color_temperature_kelvin_to_mired(model_specs["color_temp"]["max"]) - ), + "max_color_temp_kelvin": model_specs["color_temp"]["max"], "min_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["max"] ), @@ -1065,9 +1058,7 @@ async def test_device_types( "effect": None, "supported_features": SUPPORT_YEELIGHT, "min_color_temp_kelvin": model_specs["color_temp"]["min"], - "max_color_temp_kelvin": color_temperature_mired_to_kelvin( - color_temperature_kelvin_to_mired(model_specs["color_temp"]["max"]) - ), + "max_color_temp_kelvin": model_specs["color_temp"]["max"], "min_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["max"] ), @@ -1102,9 +1093,7 @@ async def test_device_types( "effect": None, "supported_features": SUPPORT_YEELIGHT, "min_color_temp_kelvin": model_specs["color_temp"]["min"], - "max_color_temp_kelvin": color_temperature_mired_to_kelvin( - color_temperature_kelvin_to_mired(model_specs["color_temp"]["max"]) - ), + "max_color_temp_kelvin": model_specs["color_temp"]["max"], "min_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["max"] ), @@ -1138,9 +1127,7 @@ async def test_device_types( "effect": None, "supported_features": SUPPORT_YEELIGHT, "min_color_temp_kelvin": model_specs["color_temp"]["min"], - "max_color_temp_kelvin": color_temperature_mired_to_kelvin( - color_temperature_kelvin_to_mired(model_specs["color_temp"]["max"]) - ), + "max_color_temp_kelvin": model_specs["color_temp"]["max"], "min_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["max"] ), @@ -1173,12 +1160,8 @@ async def test_device_types( "effect_list": YEELIGHT_TEMP_ONLY_EFFECT_LIST, "effect": None, "supported_features": SUPPORT_YEELIGHT, - "min_color_temp_kelvin": color_temperature_mired_to_kelvin( - color_temperature_kelvin_to_mired(model_specs["color_temp"]["min"]) - ), - "max_color_temp_kelvin": color_temperature_mired_to_kelvin( - color_temperature_kelvin_to_mired(model_specs["color_temp"]["max"]) - ), + "min_color_temp_kelvin": model_specs["color_temp"]["min"], + "max_color_temp_kelvin": model_specs["color_temp"]["max"], "min_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["max"] ), @@ -1204,12 +1187,8 @@ async def test_device_types( "effect_list": YEELIGHT_TEMP_ONLY_EFFECT_LIST, "effect": None, "supported_features": SUPPORT_YEELIGHT, - "min_color_temp_kelvin": color_temperature_mired_to_kelvin( - color_temperature_kelvin_to_mired(model_specs["color_temp"]["min"]) - ), - "max_color_temp_kelvin": color_temperature_mired_to_kelvin( - color_temperature_kelvin_to_mired(model_specs["color_temp"]["max"]) - ), + "min_color_temp_kelvin": model_specs["color_temp"]["min"], + "max_color_temp_kelvin": model_specs["color_temp"]["max"], "min_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["max"] ), @@ -1217,17 +1196,15 @@ async def test_device_types( model_specs["color_temp"]["min"] ), "brightness": nl_br, - "color_temp_kelvin": color_temperature_mired_to_kelvin( - color_temperature_kelvin_to_mired(model_specs["color_temp"]["min"]) - ), + "color_temp_kelvin": model_specs["color_temp"]["min"], "color_temp": color_temperature_kelvin_to_mired( model_specs["color_temp"]["min"] ), "color_mode": "color_temp", "supported_color_modes": ["color_temp"], - "hs_color": (28.391, 65.659), - "rgb_color": (255, 167, 88), - "xy_color": (0.524, 0.388), + "hs_color": (28.395, 65.723), + "rgb_color": (255, 167, 87), + "xy_color": (0.525, 0.388), }, ) @@ -1245,12 +1222,8 @@ async def test_device_types( "flowing": False, "night_light": True, "supported_features": SUPPORT_YEELIGHT, - "min_color_temp_kelvin": color_temperature_mired_to_kelvin( - color_temperature_kelvin_to_mired(model_specs["color_temp"]["min"]) - ), - "max_color_temp_kelvin": color_temperature_mired_to_kelvin( - color_temperature_kelvin_to_mired(model_specs["color_temp"]["max"]) - ), + "min_color_temp_kelvin": model_specs["color_temp"]["min"], + "max_color_temp_kelvin": model_specs["color_temp"]["max"], "min_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["max"] ), @@ -1279,12 +1252,8 @@ async def test_device_types( "flowing": False, "night_light": True, "supported_features": SUPPORT_YEELIGHT, - "min_color_temp_kelvin": color_temperature_mired_to_kelvin( - color_temperature_kelvin_to_mired(model_specs["color_temp"]["min"]) - ), - "max_color_temp_kelvin": color_temperature_mired_to_kelvin( - color_temperature_kelvin_to_mired(model_specs["color_temp"]["max"]) - ), + "min_color_temp_kelvin": model_specs["color_temp"]["min"], + "max_color_temp_kelvin": model_specs["color_temp"]["max"], "min_mireds": color_temperature_kelvin_to_mired( model_specs["color_temp"]["max"] ), @@ -1292,17 +1261,15 @@ async def test_device_types( model_specs["color_temp"]["min"] ), "brightness": nl_br, - "color_temp_kelvin": color_temperature_mired_to_kelvin( - color_temperature_kelvin_to_mired(model_specs["color_temp"]["min"]) - ), + "color_temp_kelvin": model_specs["color_temp"]["min"], "color_temp": color_temperature_kelvin_to_mired( model_specs["color_temp"]["min"] ), "color_mode": "color_temp", "supported_color_modes": ["color_temp"], - "hs_color": (28.391, 65.659), - "rgb_color": (255, 167, 88), - "xy_color": (0.524, 0.388), + "hs_color": (28.395, 65.723), + "rgb_color": (255, 167, 87), + "xy_color": (0.525, 0.388), }, ) # Background light - color mode CT @@ -1315,16 +1282,18 @@ async def test_device_types( "effect": None, "supported_features": SUPPORT_YEELIGHT, "min_color_temp_kelvin": 1700, - "max_color_temp_kelvin": color_temperature_mired_to_kelvin( - color_temperature_kelvin_to_mired(6500) - ), + "max_color_temp_kelvin": 6500, "min_mireds": color_temperature_kelvin_to_mired(6500), "max_mireds": color_temperature_kelvin_to_mired(1700), "brightness": bg_bright, "color_temp_kelvin": bg_ct, "color_temp": bg_ct_kelvin, "color_mode": "color_temp", - "supported_color_modes": ["color_temp", "hs", "rgb"], + "supported_color_modes": [ + ColorMode.COLOR_TEMP, + ColorMode.HS, + ColorMode.RGB, + ], "hs_color": (27.001, 19.243), "rgb_color": (255, 228, 206), "xy_color": (0.371, 0.349), @@ -1343,9 +1312,7 @@ async def test_device_types( "effect": None, "supported_features": SUPPORT_YEELIGHT, "min_color_temp_kelvin": 1700, - "max_color_temp_kelvin": color_temperature_mired_to_kelvin( - color_temperature_kelvin_to_mired(6500) - ), + "max_color_temp_kelvin": 6500, "min_mireds": color_temperature_kelvin_to_mired(6500), "max_mireds": color_temperature_kelvin_to_mired(1700), "brightness": bg_bright, @@ -1355,7 +1322,11 @@ async def test_device_types( "color_temp": None, "color_temp_kelvin": None, "color_mode": "hs", - "supported_color_modes": ["color_temp", "hs", "rgb"], + "supported_color_modes": [ + ColorMode.COLOR_TEMP, + ColorMode.HS, + ColorMode.RGB, + ], }, name=f"{UNIQUE_FRIENDLY_NAME} Ambilight", entity_id=f"{ENTITY_LIGHT}_ambilight", @@ -1371,9 +1342,7 @@ async def test_device_types( "effect": None, "supported_features": SUPPORT_YEELIGHT, "min_color_temp_kelvin": 1700, - "max_color_temp_kelvin": color_temperature_mired_to_kelvin( - color_temperature_kelvin_to_mired(6500) - ), + "max_color_temp_kelvin": 6500, "min_mireds": color_temperature_kelvin_to_mired(6500), "max_mireds": color_temperature_kelvin_to_mired(1700), "brightness": bg_bright, @@ -1383,7 +1352,11 @@ async def test_device_types( "color_temp": None, "color_temp_kelvin": None, "color_mode": "rgb", - "supported_color_modes": ["color_temp", "hs", "rgb"], + "supported_color_modes": [ + ColorMode.COLOR_TEMP, + ColorMode.HS, + ColorMode.RGB, + ], }, name=f"{UNIQUE_FRIENDLY_NAME} Ambilight", entity_id=f"{ENTITY_LIGHT}_ambilight",