Migrate tuya lights to use Kelvin (#132803)

pull/132817/head
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
1 changed files with 18 additions and 10 deletions

View File

@ -10,7 +10,7 @@ from tuya_sharing import CustomerDevice, Manager
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP,
ATTR_COLOR_TEMP_KELVIN,
ATTR_HS_COLOR,
ColorMode,
LightEntity,
@ -21,6 +21,7 @@ from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import color as color_util
from . import TuyaConfigEntry
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),
)
MAX_MIREDS = 500 # 2000 K
MIN_MIREDS = 153 # 6500 K
@dataclass(frozen=True)
class TuyaLightEntityDescription(LightEntityDescription):
@ -457,6 +461,8 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
_color_mode: DPCode | None = None
_color_temp: IntegerTypeData | 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__(
self,
@ -532,7 +538,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
"""Turn on or control the light."""
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:
commands += [
{
@ -546,9 +552,11 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
"code": self._color_temp.dpcode,
"value": round(
self._color_temp.remap_value_from(
kwargs[ATTR_COLOR_TEMP],
self.min_mireds,
self.max_mireds,
color_util.color_temperature_kelvin_to_mired(
kwargs[ATTR_COLOR_TEMP_KELVIN]
),
MIN_MIREDS,
MAX_MIREDS,
reverse=True,
)
),
@ -560,7 +568,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
or (
ATTR_BRIGHTNESS in kwargs
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:
@ -688,8 +696,8 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
return round(brightness)
@property
def color_temp(self) -> int | None:
"""Return the color_temp of the light."""
def color_temp_kelvin(self) -> int | None:
"""Return the color temperature value in Kelvin."""
if not self._color_temp:
return None
@ -697,9 +705,9 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
if temperature is None:
return None
return round(
return color_util.color_temperature_mired_to_kelvin(
self._color_temp.remap_value_to(
temperature, self.min_mireds, self.max_mireds, reverse=True
temperature, MIN_MIREDS, MAX_MIREDS, reverse=True
)
)