Migrate tuya lights to use Kelvin (#132803)

This commit is contained in:
epenet 2024-12-10 10:09:20 +01:00 committed by GitHub
parent bd6df06248
commit f0e7cb5794
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -10,7 +10,7 @@ from tuya_sharing import CustomerDevice, Manager
from homeassistant.components.light import ( from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP, ATTR_COLOR_TEMP_KELVIN,
ATTR_HS_COLOR, ATTR_HS_COLOR,
ColorMode, ColorMode,
LightEntity, LightEntity,
@ -21,6 +21,7 @@ from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import color as color_util
from . import TuyaConfigEntry from . import TuyaConfigEntry
from .const import TUYA_DISCOVERY_NEW, DPCode, DPType, WorkMode from .const import TUYA_DISCOVERY_NEW, DPCode, DPType, WorkMode
@ -49,6 +50,9 @@ DEFAULT_COLOR_TYPE_DATA_V2 = ColorTypeData(
v_type=IntegerTypeData(DPCode.COLOUR_DATA_HSV, min=1, scale=0, max=1000, step=1), v_type=IntegerTypeData(DPCode.COLOUR_DATA_HSV, min=1, scale=0, max=1000, step=1),
) )
MAX_MIREDS = 500 # 2000 K
MIN_MIREDS = 153 # 6500 K
@dataclass(frozen=True) @dataclass(frozen=True)
class TuyaLightEntityDescription(LightEntityDescription): class TuyaLightEntityDescription(LightEntityDescription):
@ -457,6 +461,8 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
_color_mode: DPCode | None = None _color_mode: DPCode | None = None
_color_temp: IntegerTypeData | None = None _color_temp: IntegerTypeData | None = None
_fixed_color_mode: ColorMode | None = None _fixed_color_mode: ColorMode | None = None
_attr_min_color_temp_kelvin = 2000 # 500 Mireds
_attr_max_color_temp_kelvin = 6500 # 153 Mireds
def __init__( def __init__(
self, self,
@ -532,7 +538,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
"""Turn on or control the light.""" """Turn on or control the light."""
commands = [{"code": self.entity_description.key, "value": True}] commands = [{"code": self.entity_description.key, "value": True}]
if self._color_temp and ATTR_COLOR_TEMP in kwargs: if self._color_temp and ATTR_COLOR_TEMP_KELVIN in kwargs:
if self._color_mode_dpcode: if self._color_mode_dpcode:
commands += [ commands += [
{ {
@ -546,9 +552,11 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
"code": self._color_temp.dpcode, "code": self._color_temp.dpcode,
"value": round( "value": round(
self._color_temp.remap_value_from( self._color_temp.remap_value_from(
kwargs[ATTR_COLOR_TEMP], color_util.color_temperature_kelvin_to_mired(
self.min_mireds, kwargs[ATTR_COLOR_TEMP_KELVIN]
self.max_mireds, ),
MIN_MIREDS,
MAX_MIREDS,
reverse=True, reverse=True,
) )
), ),
@ -560,7 +568,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
or ( or (
ATTR_BRIGHTNESS in kwargs ATTR_BRIGHTNESS in kwargs
and self.color_mode == ColorMode.HS and self.color_mode == ColorMode.HS
and ATTR_COLOR_TEMP not in kwargs and ATTR_COLOR_TEMP_KELVIN not in kwargs
) )
): ):
if self._color_mode_dpcode: if self._color_mode_dpcode:
@ -688,8 +696,8 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
return round(brightness) return round(brightness)
@property @property
def color_temp(self) -> int | None: def color_temp_kelvin(self) -> int | None:
"""Return the color_temp of the light.""" """Return the color temperature value in Kelvin."""
if not self._color_temp: if not self._color_temp:
return None return None
@ -697,9 +705,9 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
if temperature is None: if temperature is None:
return None return None
return round( return color_util.color_temperature_mired_to_kelvin(
self._color_temp.remap_value_to( self._color_temp.remap_value_to(
temperature, self.min_mireds, self.max_mireds, reverse=True temperature, MIN_MIREDS, MAX_MIREDS, reverse=True
) )
) )