Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Switchbot integration light platform."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from switchbot import ColorMode as SwitchBotColorMode, SwitchbotBaseLight
8 
10  ATTR_BRIGHTNESS,
11  ATTR_COLOR_TEMP,
12  ATTR_RGB_COLOR,
13  ColorMode,
14  LightEntity,
15 )
16 from homeassistant.core import HomeAssistant, callback
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 from homeassistant.util.color import (
19  color_temperature_kelvin_to_mired,
20  color_temperature_mired_to_kelvin,
21 )
22 
23 from .coordinator import SwitchbotConfigEntry, SwitchbotDataUpdateCoordinator
24 from .entity import SwitchbotEntity
25 
26 SWITCHBOT_COLOR_MODE_TO_HASS = {
27  SwitchBotColorMode.RGB: ColorMode.RGB,
28  SwitchBotColorMode.COLOR_TEMP: ColorMode.COLOR_TEMP,
29 }
30 
31 PARALLEL_UPDATES = 0
32 
33 
35  hass: HomeAssistant,
36  entry: SwitchbotConfigEntry,
37  async_add_entities: AddEntitiesCallback,
38 ) -> None:
39  """Set up the switchbot light."""
40  async_add_entities([SwitchbotLightEntity(entry.runtime_data)])
41 
42 
44  """Representation of switchbot light bulb."""
45 
46  _device: SwitchbotBaseLight
47  _attr_name = None
48 
49  def __init__(self, coordinator: SwitchbotDataUpdateCoordinator) -> None:
50  """Initialize the Switchbot light."""
51  super().__init__(coordinator)
52  device = self._device_device
53  self._attr_min_mireds_attr_min_mireds = color_temperature_kelvin_to_mired(device.max_temp)
54  self._attr_max_mireds_attr_max_mireds = color_temperature_kelvin_to_mired(device.min_temp)
55  self._attr_supported_color_modes_attr_supported_color_modes = {
56  SWITCHBOT_COLOR_MODE_TO_HASS[mode] for mode in device.color_modes
57  }
58  self._async_update_attrs_async_update_attrs_async_update_attrs()
59 
60  @callback
61  def _async_update_attrs(self) -> None:
62  """Handle updating _attr values."""
63  device = self._device_device
64  self._attr_is_on_attr_is_on = self._device_device.on
65  self._attr_brightness_attr_brightness = max(0, min(255, round(device.brightness * 2.55)))
66  if device.color_mode == SwitchBotColorMode.COLOR_TEMP:
67  self._attr_color_temp_attr_color_temp = color_temperature_kelvin_to_mired(device.color_temp)
68  self._attr_color_mode_attr_color_mode = ColorMode.COLOR_TEMP
69  return
70  self._attr_rgb_color_attr_rgb_color = device.rgb
71  self._attr_color_mode_attr_color_mode = ColorMode.RGB
72 
73  async def async_turn_on(self, **kwargs: Any) -> None:
74  """Instruct the light to turn on."""
75  brightness = round(kwargs.get(ATTR_BRIGHTNESS, self.brightnessbrightness) / 255 * 100)
76 
77  if (
78  self.supported_color_modessupported_color_modes
79  and ColorMode.COLOR_TEMP in self.supported_color_modessupported_color_modes
80  and ATTR_COLOR_TEMP in kwargs
81  ):
82  color_temp = kwargs[ATTR_COLOR_TEMP]
83  kelvin = max(2700, min(6500, color_temperature_mired_to_kelvin(color_temp)))
84  await self._device_device.set_color_temp(brightness, kelvin)
85  return
86  if ATTR_RGB_COLOR in kwargs:
87  rgb = kwargs[ATTR_RGB_COLOR]
88  await self._device_device.set_rgb(brightness, rgb[0], rgb[1], rgb[2])
89  return
90  if ATTR_BRIGHTNESS in kwargs:
91  await self._device_device.set_brightness(brightness)
92  return
93  await self._device_device.turn_on()
94 
95  async def async_turn_off(self, **kwargs: Any) -> None:
96  """Instruct the light to turn off."""
97  await self._device_device.turn_off()
set[ColorMode]|set[str]|None supported_color_modes(self)
Definition: __init__.py:1302
None __init__(self, SwitchbotDataUpdateCoordinator coordinator)
Definition: light.py:49
None turn_off(self, **Any kwargs)
Definition: entity.py:1705
None turn_on(self, **Any kwargs)
Definition: entity.py:1697
None async_setup_entry(HomeAssistant hass, SwitchbotConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: light.py:38
int color_temperature_mired_to_kelvin(float mired_temperature)
Definition: color.py:631
int color_temperature_kelvin_to_mired(float kelvin_temperature)
Definition: color.py:636