Skip to content

Commit

Permalink
Migrate switchbot lights to use Kelvin (#132695)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Dec 9, 2024
1 parent a203479 commit 46e5136
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions homeassistant/components/switchbot/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@

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
Expand Down Expand Up @@ -50,8 +46,8 @@ def __init__(self, coordinator: SwitchbotDataUpdateCoordinator) -> None:
"""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
}
Expand All @@ -64,7 +60,7 @@ def _async_update_attrs(self) -> None:
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
Expand All @@ -77,10 +73,9 @@ async def async_turn_on(self, **kwargs: Any) -> None:
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:
Expand Down

0 comments on commit 46e5136

Please sign in to comment.