Migrate yeelight lights to use Kelvin (#132814)

This commit is contained in:
epenet 2024-12-10 10:29:32 +01:00 committed by GitHub
parent be1c225c70
commit d724488376
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 58 additions and 90 deletions

View File

@ -16,7 +16,7 @@ from yeelight.main import BulbException
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_FLASH, ATTR_FLASH,
ATTR_HS_COLOR, 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.event import async_call_later
from homeassistant.helpers.typing import VolDictType from homeassistant.helpers.typing import VolDictType
import homeassistant.util.color as color_util 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 . import YEELIGHT_FLOW_TRANSITION_SCHEMA
from .const import ( from .const import (
@ -440,8 +436,8 @@ class YeelightBaseLight(YeelightEntity, LightEntity):
self._effect = None self._effect = None
model_specs = self._bulb.get_model_specs() model_specs = self._bulb.get_model_specs()
self._attr_min_mireds = kelvin_to_mired(model_specs["color_temp"]["max"]) self._attr_max_color_temp_kelvin = model_specs["color_temp"]["max"]
self._attr_max_mireds = kelvin_to_mired(model_specs["color_temp"]["min"]) self._attr_min_color_temp_kelvin = model_specs["color_temp"]["min"]
self._light_type = LightType.Main self._light_type = LightType.Main
@ -476,10 +472,10 @@ class YeelightBaseLight(YeelightEntity, LightEntity):
return self._predefined_effects + self.custom_effects_names return self._predefined_effects + self.custom_effects_names
@property @property
def color_temp(self) -> int | None: def color_temp_kelvin(self) -> int | None:
"""Return the color temperature.""" """Return the color temperature value in Kelvin."""
if temp_in_k := self._get_property("ct"): 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 return self._color_temp
@property @property
@ -678,20 +674,19 @@ class YeelightBaseLight(YeelightEntity, LightEntity):
) )
@_async_cmd @_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.""" """Set bulb's color temperature."""
if ( if (
not colortemp not temp_in_k
or not self.supported_color_modes or not self.supported_color_modes
or ColorMode.COLOR_TEMP not in self.supported_color_modes or ColorMode.COLOR_TEMP not in self.supported_color_modes
): ):
return return
temp_in_k = mired_to_kelvin(colortemp)
if ( if (
not self.device.is_color_flow_enabled not self.device.is_color_flow_enabled
and self.color_mode == ColorMode.COLOR_TEMP 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) _LOGGER.debug("Color temp already set to: %s", temp_in_k)
# Already set, and since we get pushed updates # 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: async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the bulb on.""" """Turn the bulb on."""
brightness = kwargs.get(ATTR_BRIGHTNESS) 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) hs_color = kwargs.get(ATTR_HS_COLOR)
rgb = kwargs.get(ATTR_RGB_COLOR) rgb = kwargs.get(ATTR_RGB_COLOR)
flash = kwargs.get(ATTR_FLASH) flash = kwargs.get(ATTR_FLASH)
@ -933,12 +928,12 @@ class YeelightWithoutNightlightSwitchMixIn(YeelightBaseLight):
return super()._brightness_property return super()._brightness_property
@property @property
def color_temp(self) -> int | None: def color_temp_kelvin(self) -> int | None:
"""Return the color temperature.""" """Return the color temperature value in Kelvin."""
if self.device.is_nightlight_enabled: if self.device.is_nightlight_enabled:
# Enabling the nightlight locks the colortemp to max # Enabling the nightlight locks the colortemp to max
return self.max_mireds return self.min_color_temp_kelvin
return super().color_temp return super().color_temp_kelvin
class YeelightColorLightWithoutNightlightSwitch( class YeelightColorLightWithoutNightlightSwitch(
@ -1081,8 +1076,8 @@ class YeelightAmbientLight(YeelightColorLightWithoutNightlightSwitch):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
"""Initialize the Yeelight Ambient light.""" """Initialize the Yeelight Ambient light."""
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self._attr_min_mireds = kelvin_to_mired(6500) self._attr_max_color_temp_kelvin = 6500
self._attr_max_mireds = kelvin_to_mired(1700) self._attr_min_color_temp_kelvin = 1700
self._light_type = LightType.Ambient self._light_type = LightType.Ambient

View File

@ -35,6 +35,7 @@ from homeassistant.components.light import (
FLASH_SHORT, FLASH_SHORT,
SERVICE_TURN_OFF, SERVICE_TURN_OFF,
SERVICE_TURN_ON, SERVICE_TURN_ON,
ColorMode,
LightEntityFeature, LightEntityFeature,
) )
from homeassistant.components.yeelight.const import ( from homeassistant.components.yeelight.const import (
@ -931,9 +932,7 @@ async def test_device_types(
"effect": None, "effect": None,
"supported_features": SUPPORT_YEELIGHT, "supported_features": SUPPORT_YEELIGHT,
"min_color_temp_kelvin": model_specs["color_temp"]["min"], "min_color_temp_kelvin": model_specs["color_temp"]["min"],
"max_color_temp_kelvin": color_temperature_mired_to_kelvin( "max_color_temp_kelvin": model_specs["color_temp"]["max"],
color_temperature_kelvin_to_mired(model_specs["color_temp"]["max"])
),
"min_mireds": color_temperature_kelvin_to_mired( "min_mireds": color_temperature_kelvin_to_mired(
model_specs["color_temp"]["max"] model_specs["color_temp"]["max"]
), ),
@ -962,9 +961,7 @@ async def test_device_types(
"rgb_color": (255, 121, 0), "rgb_color": (255, 121, 0),
"xy_color": (0.62, 0.368), "xy_color": (0.62, 0.368),
"min_color_temp_kelvin": model_specs["color_temp"]["min"], "min_color_temp_kelvin": model_specs["color_temp"]["min"],
"max_color_temp_kelvin": color_temperature_mired_to_kelvin( "max_color_temp_kelvin": model_specs["color_temp"]["max"],
color_temperature_kelvin_to_mired(model_specs["color_temp"]["max"])
),
"min_mireds": color_temperature_kelvin_to_mired( "min_mireds": color_temperature_kelvin_to_mired(
model_specs["color_temp"]["max"] model_specs["color_temp"]["max"]
), ),
@ -992,9 +989,7 @@ async def test_device_types(
"effect": None, "effect": None,
"supported_features": SUPPORT_YEELIGHT, "supported_features": SUPPORT_YEELIGHT,
"min_color_temp_kelvin": model_specs["color_temp"]["min"], "min_color_temp_kelvin": model_specs["color_temp"]["min"],
"max_color_temp_kelvin": color_temperature_mired_to_kelvin( "max_color_temp_kelvin": model_specs["color_temp"]["max"],
color_temperature_kelvin_to_mired(model_specs["color_temp"]["max"])
),
"min_mireds": color_temperature_kelvin_to_mired( "min_mireds": color_temperature_kelvin_to_mired(
model_specs["color_temp"]["max"] model_specs["color_temp"]["max"]
), ),
@ -1028,9 +1023,7 @@ async def test_device_types(
"effect": None, "effect": None,
"supported_features": SUPPORT_YEELIGHT, "supported_features": SUPPORT_YEELIGHT,
"min_color_temp_kelvin": model_specs["color_temp"]["min"], "min_color_temp_kelvin": model_specs["color_temp"]["min"],
"max_color_temp_kelvin": color_temperature_mired_to_kelvin( "max_color_temp_kelvin": model_specs["color_temp"]["max"],
color_temperature_kelvin_to_mired(model_specs["color_temp"]["max"])
),
"min_mireds": color_temperature_kelvin_to_mired( "min_mireds": color_temperature_kelvin_to_mired(
model_specs["color_temp"]["max"] model_specs["color_temp"]["max"]
), ),
@ -1065,9 +1058,7 @@ async def test_device_types(
"effect": None, "effect": None,
"supported_features": SUPPORT_YEELIGHT, "supported_features": SUPPORT_YEELIGHT,
"min_color_temp_kelvin": model_specs["color_temp"]["min"], "min_color_temp_kelvin": model_specs["color_temp"]["min"],
"max_color_temp_kelvin": color_temperature_mired_to_kelvin( "max_color_temp_kelvin": model_specs["color_temp"]["max"],
color_temperature_kelvin_to_mired(model_specs["color_temp"]["max"])
),
"min_mireds": color_temperature_kelvin_to_mired( "min_mireds": color_temperature_kelvin_to_mired(
model_specs["color_temp"]["max"] model_specs["color_temp"]["max"]
), ),
@ -1102,9 +1093,7 @@ async def test_device_types(
"effect": None, "effect": None,
"supported_features": SUPPORT_YEELIGHT, "supported_features": SUPPORT_YEELIGHT,
"min_color_temp_kelvin": model_specs["color_temp"]["min"], "min_color_temp_kelvin": model_specs["color_temp"]["min"],
"max_color_temp_kelvin": color_temperature_mired_to_kelvin( "max_color_temp_kelvin": model_specs["color_temp"]["max"],
color_temperature_kelvin_to_mired(model_specs["color_temp"]["max"])
),
"min_mireds": color_temperature_kelvin_to_mired( "min_mireds": color_temperature_kelvin_to_mired(
model_specs["color_temp"]["max"] model_specs["color_temp"]["max"]
), ),
@ -1138,9 +1127,7 @@ async def test_device_types(
"effect": None, "effect": None,
"supported_features": SUPPORT_YEELIGHT, "supported_features": SUPPORT_YEELIGHT,
"min_color_temp_kelvin": model_specs["color_temp"]["min"], "min_color_temp_kelvin": model_specs["color_temp"]["min"],
"max_color_temp_kelvin": color_temperature_mired_to_kelvin( "max_color_temp_kelvin": model_specs["color_temp"]["max"],
color_temperature_kelvin_to_mired(model_specs["color_temp"]["max"])
),
"min_mireds": color_temperature_kelvin_to_mired( "min_mireds": color_temperature_kelvin_to_mired(
model_specs["color_temp"]["max"] model_specs["color_temp"]["max"]
), ),
@ -1173,12 +1160,8 @@ async def test_device_types(
"effect_list": YEELIGHT_TEMP_ONLY_EFFECT_LIST, "effect_list": YEELIGHT_TEMP_ONLY_EFFECT_LIST,
"effect": None, "effect": None,
"supported_features": SUPPORT_YEELIGHT, "supported_features": SUPPORT_YEELIGHT,
"min_color_temp_kelvin": color_temperature_mired_to_kelvin( "min_color_temp_kelvin": model_specs["color_temp"]["min"],
color_temperature_kelvin_to_mired(model_specs["color_temp"]["min"]) "max_color_temp_kelvin": model_specs["color_temp"]["max"],
),
"max_color_temp_kelvin": color_temperature_mired_to_kelvin(
color_temperature_kelvin_to_mired(model_specs["color_temp"]["max"])
),
"min_mireds": color_temperature_kelvin_to_mired( "min_mireds": color_temperature_kelvin_to_mired(
model_specs["color_temp"]["max"] model_specs["color_temp"]["max"]
), ),
@ -1204,12 +1187,8 @@ async def test_device_types(
"effect_list": YEELIGHT_TEMP_ONLY_EFFECT_LIST, "effect_list": YEELIGHT_TEMP_ONLY_EFFECT_LIST,
"effect": None, "effect": None,
"supported_features": SUPPORT_YEELIGHT, "supported_features": SUPPORT_YEELIGHT,
"min_color_temp_kelvin": color_temperature_mired_to_kelvin( "min_color_temp_kelvin": model_specs["color_temp"]["min"],
color_temperature_kelvin_to_mired(model_specs["color_temp"]["min"]) "max_color_temp_kelvin": model_specs["color_temp"]["max"],
),
"max_color_temp_kelvin": color_temperature_mired_to_kelvin(
color_temperature_kelvin_to_mired(model_specs["color_temp"]["max"])
),
"min_mireds": color_temperature_kelvin_to_mired( "min_mireds": color_temperature_kelvin_to_mired(
model_specs["color_temp"]["max"] model_specs["color_temp"]["max"]
), ),
@ -1217,17 +1196,15 @@ async def test_device_types(
model_specs["color_temp"]["min"] model_specs["color_temp"]["min"]
), ),
"brightness": nl_br, "brightness": nl_br,
"color_temp_kelvin": color_temperature_mired_to_kelvin( "color_temp_kelvin": model_specs["color_temp"]["min"],
color_temperature_kelvin_to_mired(model_specs["color_temp"]["min"])
),
"color_temp": color_temperature_kelvin_to_mired( "color_temp": color_temperature_kelvin_to_mired(
model_specs["color_temp"]["min"] model_specs["color_temp"]["min"]
), ),
"color_mode": "color_temp", "color_mode": "color_temp",
"supported_color_modes": ["color_temp"], "supported_color_modes": ["color_temp"],
"hs_color": (28.391, 65.659), "hs_color": (28.395, 65.723),
"rgb_color": (255, 167, 88), "rgb_color": (255, 167, 87),
"xy_color": (0.524, 0.388), "xy_color": (0.525, 0.388),
}, },
) )
@ -1245,12 +1222,8 @@ async def test_device_types(
"flowing": False, "flowing": False,
"night_light": True, "night_light": True,
"supported_features": SUPPORT_YEELIGHT, "supported_features": SUPPORT_YEELIGHT,
"min_color_temp_kelvin": color_temperature_mired_to_kelvin( "min_color_temp_kelvin": model_specs["color_temp"]["min"],
color_temperature_kelvin_to_mired(model_specs["color_temp"]["min"]) "max_color_temp_kelvin": model_specs["color_temp"]["max"],
),
"max_color_temp_kelvin": color_temperature_mired_to_kelvin(
color_temperature_kelvin_to_mired(model_specs["color_temp"]["max"])
),
"min_mireds": color_temperature_kelvin_to_mired( "min_mireds": color_temperature_kelvin_to_mired(
model_specs["color_temp"]["max"] model_specs["color_temp"]["max"]
), ),
@ -1279,12 +1252,8 @@ async def test_device_types(
"flowing": False, "flowing": False,
"night_light": True, "night_light": True,
"supported_features": SUPPORT_YEELIGHT, "supported_features": SUPPORT_YEELIGHT,
"min_color_temp_kelvin": color_temperature_mired_to_kelvin( "min_color_temp_kelvin": model_specs["color_temp"]["min"],
color_temperature_kelvin_to_mired(model_specs["color_temp"]["min"]) "max_color_temp_kelvin": model_specs["color_temp"]["max"],
),
"max_color_temp_kelvin": color_temperature_mired_to_kelvin(
color_temperature_kelvin_to_mired(model_specs["color_temp"]["max"])
),
"min_mireds": color_temperature_kelvin_to_mired( "min_mireds": color_temperature_kelvin_to_mired(
model_specs["color_temp"]["max"] model_specs["color_temp"]["max"]
), ),
@ -1292,17 +1261,15 @@ async def test_device_types(
model_specs["color_temp"]["min"] model_specs["color_temp"]["min"]
), ),
"brightness": nl_br, "brightness": nl_br,
"color_temp_kelvin": color_temperature_mired_to_kelvin( "color_temp_kelvin": model_specs["color_temp"]["min"],
color_temperature_kelvin_to_mired(model_specs["color_temp"]["min"])
),
"color_temp": color_temperature_kelvin_to_mired( "color_temp": color_temperature_kelvin_to_mired(
model_specs["color_temp"]["min"] model_specs["color_temp"]["min"]
), ),
"color_mode": "color_temp", "color_mode": "color_temp",
"supported_color_modes": ["color_temp"], "supported_color_modes": ["color_temp"],
"hs_color": (28.391, 65.659), "hs_color": (28.395, 65.723),
"rgb_color": (255, 167, 88), "rgb_color": (255, 167, 87),
"xy_color": (0.524, 0.388), "xy_color": (0.525, 0.388),
}, },
) )
# Background light - color mode CT # Background light - color mode CT
@ -1315,16 +1282,18 @@ async def test_device_types(
"effect": None, "effect": None,
"supported_features": SUPPORT_YEELIGHT, "supported_features": SUPPORT_YEELIGHT,
"min_color_temp_kelvin": 1700, "min_color_temp_kelvin": 1700,
"max_color_temp_kelvin": color_temperature_mired_to_kelvin( "max_color_temp_kelvin": 6500,
color_temperature_kelvin_to_mired(6500)
),
"min_mireds": color_temperature_kelvin_to_mired(6500), "min_mireds": color_temperature_kelvin_to_mired(6500),
"max_mireds": color_temperature_kelvin_to_mired(1700), "max_mireds": color_temperature_kelvin_to_mired(1700),
"brightness": bg_bright, "brightness": bg_bright,
"color_temp_kelvin": bg_ct, "color_temp_kelvin": bg_ct,
"color_temp": bg_ct_kelvin, "color_temp": bg_ct_kelvin,
"color_mode": "color_temp", "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), "hs_color": (27.001, 19.243),
"rgb_color": (255, 228, 206), "rgb_color": (255, 228, 206),
"xy_color": (0.371, 0.349), "xy_color": (0.371, 0.349),
@ -1343,9 +1312,7 @@ async def test_device_types(
"effect": None, "effect": None,
"supported_features": SUPPORT_YEELIGHT, "supported_features": SUPPORT_YEELIGHT,
"min_color_temp_kelvin": 1700, "min_color_temp_kelvin": 1700,
"max_color_temp_kelvin": color_temperature_mired_to_kelvin( "max_color_temp_kelvin": 6500,
color_temperature_kelvin_to_mired(6500)
),
"min_mireds": color_temperature_kelvin_to_mired(6500), "min_mireds": color_temperature_kelvin_to_mired(6500),
"max_mireds": color_temperature_kelvin_to_mired(1700), "max_mireds": color_temperature_kelvin_to_mired(1700),
"brightness": bg_bright, "brightness": bg_bright,
@ -1355,7 +1322,11 @@ async def test_device_types(
"color_temp": None, "color_temp": None,
"color_temp_kelvin": None, "color_temp_kelvin": None,
"color_mode": "hs", "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", name=f"{UNIQUE_FRIENDLY_NAME} Ambilight",
entity_id=f"{ENTITY_LIGHT}_ambilight", entity_id=f"{ENTITY_LIGHT}_ambilight",
@ -1371,9 +1342,7 @@ async def test_device_types(
"effect": None, "effect": None,
"supported_features": SUPPORT_YEELIGHT, "supported_features": SUPPORT_YEELIGHT,
"min_color_temp_kelvin": 1700, "min_color_temp_kelvin": 1700,
"max_color_temp_kelvin": color_temperature_mired_to_kelvin( "max_color_temp_kelvin": 6500,
color_temperature_kelvin_to_mired(6500)
),
"min_mireds": color_temperature_kelvin_to_mired(6500), "min_mireds": color_temperature_kelvin_to_mired(6500),
"max_mireds": color_temperature_kelvin_to_mired(1700), "max_mireds": color_temperature_kelvin_to_mired(1700),
"brightness": bg_bright, "brightness": bg_bright,
@ -1383,7 +1352,11 @@ async def test_device_types(
"color_temp": None, "color_temp": None,
"color_temp_kelvin": None, "color_temp_kelvin": None,
"color_mode": "rgb", "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", name=f"{UNIQUE_FRIENDLY_NAME} Ambilight",
entity_id=f"{ENTITY_LIGHT}_ambilight", entity_id=f"{ENTITY_LIGHT}_ambilight",