Migrate switchbot lights to use Kelvin (#132695)

pull/132627/merge
epenet 2024-12-09 16:25:25 +01:00 committed by GitHub
parent a20347963e
commit 46e513615e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 11 deletions

View File

@ -8,17 +8,13 @@ from switchbot import ColorMode as SwitchBotColorMode, SwitchbotBaseLight
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP,
ATTR_COLOR_TEMP_KELVIN,
ATTR_RGB_COLOR,
ColorMode,
LightEntity,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.color import (
color_temperature_kelvin_to_mired,
color_temperature_mired_to_kelvin,
)
from .coordinator import SwitchbotConfigEntry, SwitchbotDataUpdateCoordinator
from .entity import SwitchbotEntity
@ -50,8 +46,8 @@ class SwitchbotLightEntity(SwitchbotEntity, LightEntity):
"""Initialize the Switchbot light."""
super().__init__(coordinator)
device = self._device
self._attr_min_mireds = color_temperature_kelvin_to_mired(device.max_temp)
self._attr_max_mireds = color_temperature_kelvin_to_mired(device.min_temp)
self._attr_max_color_temp_kelvin = device.max_temp
self._attr_min_color_temp_kelvin = device.min_temp
self._attr_supported_color_modes = {
SWITCHBOT_COLOR_MODE_TO_HASS[mode] for mode in device.color_modes
}
@ -64,7 +60,7 @@ class SwitchbotLightEntity(SwitchbotEntity, LightEntity):
self._attr_is_on = self._device.on
self._attr_brightness = max(0, min(255, round(device.brightness * 2.55)))
if device.color_mode == SwitchBotColorMode.COLOR_TEMP:
self._attr_color_temp = color_temperature_kelvin_to_mired(device.color_temp)
self._attr_color_temp_kelvin = device.color_temp
self._attr_color_mode = ColorMode.COLOR_TEMP
return
self._attr_rgb_color = device.rgb
@ -77,10 +73,9 @@ class SwitchbotLightEntity(SwitchbotEntity, LightEntity):
if (
self.supported_color_modes
and ColorMode.COLOR_TEMP in self.supported_color_modes
and ATTR_COLOR_TEMP in kwargs
and ATTR_COLOR_TEMP_KELVIN in kwargs
):
color_temp = kwargs[ATTR_COLOR_TEMP]
kelvin = max(2700, min(6500, color_temperature_mired_to_kelvin(color_temp)))
kelvin = max(2700, min(6500, kwargs[ATTR_COLOR_TEMP_KELVIN]))
await self._device.set_color_temp(brightness, kelvin)
return
if ATTR_RGB_COLOR in kwargs: