Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Govee light local."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from govee_local_api import GoveeDevice, GoveeLightCapability
9 
11  ATTR_BRIGHTNESS,
12  ATTR_COLOR_TEMP_KELVIN,
13  ATTR_RGB_COLOR,
14  ColorMode,
15  LightEntity,
16  filter_supported_color_modes,
17 )
18 from homeassistant.core import HomeAssistant, callback
19 from homeassistant.helpers.device_registry import DeviceInfo
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 from homeassistant.helpers.update_coordinator import CoordinatorEntity
22 
23 from .const import DOMAIN, MANUFACTURER
24 from .coordinator import GoveeLocalApiCoordinator, GoveeLocalConfigEntry
25 
26 _LOGGER = logging.getLogger(__name__)
27 
28 
30  hass: HomeAssistant,
31  config_entry: GoveeLocalConfigEntry,
32  async_add_entities: AddEntitiesCallback,
33 ) -> None:
34  """Govee light setup."""
35 
36  coordinator = config_entry.runtime_data
37 
38  def discovery_callback(device: GoveeDevice, is_new: bool) -> bool:
39  if is_new:
40  async_add_entities([GoveeLight(coordinator, device)])
41  return True
42 
44  GoveeLight(coordinator, device) for device in coordinator.devices
45  )
46 
47  await coordinator.set_discovery_callback(discovery_callback)
48 
49 
50 class GoveeLight(CoordinatorEntity[GoveeLocalApiCoordinator], LightEntity):
51  """Govee Light."""
52 
53  _attr_has_entity_name = True
54  _attr_name = None
55  _attr_supported_color_modes: set[ColorMode]
56  _fixed_color_mode: ColorMode | None = None
57 
58  def __init__(
59  self,
60  coordinator: GoveeLocalApiCoordinator,
61  device: GoveeDevice,
62  ) -> None:
63  """Govee Light constructor."""
64 
65  super().__init__(coordinator)
66  self._device_device = device
67  device.set_update_callback(self._update_callback_update_callback)
68 
69  self._attr_unique_id_attr_unique_id = device.fingerprint
70 
71  capabilities = device.capabilities
72  color_modes = {ColorMode.ONOFF}
73  if capabilities:
74  if GoveeLightCapability.COLOR_RGB in capabilities:
75  color_modes.add(ColorMode.RGB)
76  if GoveeLightCapability.COLOR_KELVIN_TEMPERATURE in capabilities:
77  color_modes.add(ColorMode.COLOR_TEMP)
78  self._attr_max_color_temp_kelvin_attr_max_color_temp_kelvin = 9000
79  self._attr_min_color_temp_kelvin_attr_min_color_temp_kelvin = 2000
80  if GoveeLightCapability.BRIGHTNESS in capabilities:
81  color_modes.add(ColorMode.BRIGHTNESS)
82 
83  self._attr_supported_color_modes_attr_supported_color_modes = filter_supported_color_modes(color_modes)
84  if len(self._attr_supported_color_modes_attr_supported_color_modes) == 1:
85  # If the light supports only a single color mode, set it now
86  self._fixed_color_mode_fixed_color_mode = next(iter(self._attr_supported_color_modes_attr_supported_color_modes))
87 
88  self._attr_device_info_attr_device_info = DeviceInfo(
89  identifiers={
90  # Serial numbers are unique identifiers within a specific domain
91  (DOMAIN, device.fingerprint)
92  },
93  name=device.sku,
94  manufacturer=MANUFACTURER,
95  model_id=device.sku,
96  serial_number=device.fingerprint,
97  )
98 
99  @property
100  def is_on(self) -> bool:
101  """Return true if device is on (brightness above 0)."""
102  return self._device_device.on
103 
104  @property
105  def brightness(self) -> int:
106  """Return the brightness of this light between 0..255."""
107  return int((self._device_device.brightness / 100.0) * 255.0)
108 
109  @property
110  def color_temp_kelvin(self) -> int | None:
111  """Return the color temperature in Kelvin."""
112  return self._device_device.temperature_color
113 
114  @property
115  def rgb_color(self) -> tuple[int, int, int] | None:
116  """Return the rgb color."""
117  return self._device_device.rgb_color
118 
119  @property
120  def color_mode(self) -> ColorMode | str | None:
121  """Return the color mode."""
122  if self._fixed_color_mode_fixed_color_mode:
123  # The light supports only a single color mode, return it
124  return self._fixed_color_mode_fixed_color_mode
125 
126  # The light supports both color temperature and RGB, determine which
127  # mode the light is in
128  if (
129  self._device_device.temperature_color is not None
130  and self._device_device.temperature_color > 0
131  ):
132  return ColorMode.COLOR_TEMP
133  return ColorMode.RGB
134 
135  async def async_turn_on(self, **kwargs: Any) -> None:
136  """Turn the device on."""
137  if not self.is_onis_onis_on or not kwargs:
138  await self.coordinator.turn_on(self._device_device)
139 
140  if ATTR_BRIGHTNESS in kwargs:
141  brightness: int = int((float(kwargs[ATTR_BRIGHTNESS]) / 255.0) * 100.0)
142  await self.coordinator.set_brightness(self._device_device, brightness)
143 
144  if ATTR_RGB_COLOR in kwargs:
145  self._attr_color_mode_attr_color_mode = ColorMode.RGB
146  red, green, blue = kwargs[ATTR_RGB_COLOR]
147  await self.coordinator.set_rgb_color(self._device_device, red, green, blue)
148  elif ATTR_COLOR_TEMP_KELVIN in kwargs:
149  self._attr_color_mode_attr_color_mode = ColorMode.COLOR_TEMP
150  temperature: float = kwargs[ATTR_COLOR_TEMP_KELVIN]
151  await self.coordinator.set_temperature(self._device_device, int(temperature))
152  self.async_write_ha_stateasync_write_ha_state()
153 
154  async def async_turn_off(self, **kwargs: Any) -> None:
155  """Turn the device off."""
156  await self.coordinator.turn_off(self._device_device)
157  self.async_write_ha_stateasync_write_ha_state()
158 
159  @callback
160  def _update_callback(self, device: GoveeDevice) -> None:
161  self.async_write_ha_stateasync_write_ha_state()
None set_temperature(self, GoveeDevice device, int temperature)
Definition: coordinator.py:83
None set_rgb_color(self, GoveeDevice device, int red, int green, int blue)
Definition: coordinator.py:79
None _update_callback(self, GoveeDevice device)
Definition: light.py:160
None __init__(self, GoveeLocalApiCoordinator coordinator, GoveeDevice device)
Definition: light.py:62
None async_setup_entry(HomeAssistant hass, GoveeLocalConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: light.py:33
set[ColorMode] filter_supported_color_modes(Iterable[ColorMode] color_modes)
Definition: __init__.py:122