Migrate smartthings lights to use Kelvin (#132699)

This commit is contained in:
epenet 2024-12-09 22:55:06 +01:00 committed by GitHub
parent abc79a9f1c
commit dcbedb5ae5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -10,7 +10,7 @@ from pysmartthings import Capability
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,
ATTR_TRANSITION, ATTR_TRANSITION,
ColorMode, ColorMode,
@ -21,7 +21,6 @@ from homeassistant.components.light import (
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
import homeassistant.util.color as color_util
from .const import DATA_BROKERS, DOMAIN from .const import DATA_BROKERS, DOMAIN
from .entity import SmartThingsEntity from .entity import SmartThingsEntity
@ -79,12 +78,12 @@ class SmartThingsLight(SmartThingsEntity, LightEntity):
# SmartThings does not expose this attribute, instead it's # SmartThings does not expose this attribute, instead it's
# implemented within each device-type handler. This value is the # implemented within each device-type handler. This value is the
# lowest kelvin found supported across 20+ handlers. # lowest kelvin found supported across 20+ handlers.
_attr_max_mireds = 500 # 2000K _attr_min_color_temp_kelvin = 2000 # 500 mireds
# SmartThings does not expose this attribute, instead it's # SmartThings does not expose this attribute, instead it's
# implemented within each device-type handler. This value is the # implemented within each device-type handler. This value is the
# highest kelvin found supported across 20+ handlers. # highest kelvin found supported across 20+ handlers.
_attr_min_mireds = 111 # 9000K _attr_max_color_temp_kelvin = 9000 # 111 mireds
def __init__(self, device): def __init__(self, device):
"""Initialize a SmartThingsLight.""" """Initialize a SmartThingsLight."""
@ -122,8 +121,8 @@ class SmartThingsLight(SmartThingsEntity, LightEntity):
"""Turn the light on.""" """Turn the light on."""
tasks = [] tasks = []
# Color temperature # Color temperature
if ATTR_COLOR_TEMP in kwargs: if ATTR_COLOR_TEMP_KELVIN in kwargs:
tasks.append(self.async_set_color_temp(kwargs[ATTR_COLOR_TEMP])) tasks.append(self.async_set_color_temp(kwargs[ATTR_COLOR_TEMP_KELVIN]))
# Color # Color
if ATTR_HS_COLOR in kwargs: if ATTR_HS_COLOR in kwargs:
tasks.append(self.async_set_color(kwargs[ATTR_HS_COLOR])) tasks.append(self.async_set_color(kwargs[ATTR_HS_COLOR]))
@ -164,9 +163,7 @@ class SmartThingsLight(SmartThingsEntity, LightEntity):
) )
# Color Temperature # Color Temperature
if ColorMode.COLOR_TEMP in self._attr_supported_color_modes: if ColorMode.COLOR_TEMP in self._attr_supported_color_modes:
self._attr_color_temp = color_util.color_temperature_kelvin_to_mired( self._attr_color_temp_kelvin = self._device.status.color_temperature
self._device.status.color_temperature
)
# Color # Color
if ColorMode.HS in self._attr_supported_color_modes: if ColorMode.HS in self._attr_supported_color_modes:
self._attr_hs_color = ( self._attr_hs_color = (
@ -181,10 +178,9 @@ class SmartThingsLight(SmartThingsEntity, LightEntity):
saturation = max(min(float(hs_color[1]), 100.0), 0.0) saturation = max(min(float(hs_color[1]), 100.0), 0.0)
await self._device.set_color(hue, saturation, set_status=True) await self._device.set_color(hue, saturation, set_status=True)
async def async_set_color_temp(self, value: float): async def async_set_color_temp(self, value: int):
"""Set the color temperature of the device.""" """Set the color temperature of the device."""
kelvin = color_util.color_temperature_mired_to_kelvin(value) kelvin = max(min(value, 30000), 1)
kelvin = max(min(kelvin, 30000), 1)
await self._device.set_color_temperature(kelvin, set_status=True) await self._device.set_color_temperature(kelvin, set_status=True)
async def async_set_level(self, brightness: int, transition: int): async def async_set_level(self, brightness: int, transition: int):