Migrate switchbot lights to use Kelvin (#132695)
parent
a20347963e
commit
46e513615e
|
@ -8,17 +8,13 @@ from switchbot import ColorMode as SwitchBotColorMode, SwitchbotBaseLight
|
||||||
|
|
||||||
from homeassistant.components.light import (
|
from homeassistant.components.light import (
|
||||||
ATTR_BRIGHTNESS,
|
ATTR_BRIGHTNESS,
|
||||||
ATTR_COLOR_TEMP,
|
ATTR_COLOR_TEMP_KELVIN,
|
||||||
ATTR_RGB_COLOR,
|
ATTR_RGB_COLOR,
|
||||||
ColorMode,
|
ColorMode,
|
||||||
LightEntity,
|
LightEntity,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
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 .coordinator import SwitchbotConfigEntry, SwitchbotDataUpdateCoordinator
|
||||||
from .entity import SwitchbotEntity
|
from .entity import SwitchbotEntity
|
||||||
|
@ -50,8 +46,8 @@ class SwitchbotLightEntity(SwitchbotEntity, LightEntity):
|
||||||
"""Initialize the Switchbot light."""
|
"""Initialize the Switchbot light."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
device = self._device
|
device = self._device
|
||||||
self._attr_min_mireds = color_temperature_kelvin_to_mired(device.max_temp)
|
self._attr_max_color_temp_kelvin = device.max_temp
|
||||||
self._attr_max_mireds = color_temperature_kelvin_to_mired(device.min_temp)
|
self._attr_min_color_temp_kelvin = device.min_temp
|
||||||
self._attr_supported_color_modes = {
|
self._attr_supported_color_modes = {
|
||||||
SWITCHBOT_COLOR_MODE_TO_HASS[mode] for mode in device.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_is_on = self._device.on
|
||||||
self._attr_brightness = max(0, min(255, round(device.brightness * 2.55)))
|
self._attr_brightness = max(0, min(255, round(device.brightness * 2.55)))
|
||||||
if device.color_mode == SwitchBotColorMode.COLOR_TEMP:
|
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
|
self._attr_color_mode = ColorMode.COLOR_TEMP
|
||||||
return
|
return
|
||||||
self._attr_rgb_color = device.rgb
|
self._attr_rgb_color = device.rgb
|
||||||
|
@ -77,10 +73,9 @@ class SwitchbotLightEntity(SwitchbotEntity, LightEntity):
|
||||||
if (
|
if (
|
||||||
self.supported_color_modes
|
self.supported_color_modes
|
||||||
and ColorMode.COLOR_TEMP in 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, kwargs[ATTR_COLOR_TEMP_KELVIN]))
|
||||||
kelvin = max(2700, min(6500, color_temperature_mired_to_kelvin(color_temp)))
|
|
||||||
await self._device.set_color_temp(brightness, kelvin)
|
await self._device.set_color_temp(brightness, kelvin)
|
||||||
return
|
return
|
||||||
if ATTR_RGB_COLOR in kwargs:
|
if ATTR_RGB_COLOR in kwargs:
|
||||||
|
|
Loading…
Reference in New Issue