Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """LED BLE integration light platform."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from led_ble import LEDBLE
8 
10  ATTR_BRIGHTNESS,
11  ATTR_EFFECT,
12  ATTR_RGB_COLOR,
13  ATTR_WHITE,
14  ColorMode,
15  LightEntity,
16  LightEntityFeature,
17 )
18 from homeassistant.config_entries import ConfigEntry
19 from homeassistant.core import HomeAssistant, callback
20 from homeassistant.helpers import device_registry as dr
21 from homeassistant.helpers.device_registry import DeviceInfo
22 from homeassistant.helpers.entity_platform import AddEntitiesCallback
24  CoordinatorEntity,
25  DataUpdateCoordinator,
26 )
27 
28 from .const import DEFAULT_EFFECT_SPEED, DOMAIN
29 from .models import LEDBLEData
30 
31 
33  hass: HomeAssistant,
34  entry: ConfigEntry,
35  async_add_entities: AddEntitiesCallback,
36 ) -> None:
37  """Set up the light platform for LEDBLE."""
38  data: LEDBLEData = hass.data[DOMAIN][entry.entry_id]
39  async_add_entities([LEDBLEEntity(data.coordinator, data.device, entry.title)])
40 
41 
42 class LEDBLEEntity(CoordinatorEntity[DataUpdateCoordinator[None]], LightEntity):
43  """Representation of LEDBLE device."""
44 
45  _attr_supported_color_modes = {ColorMode.RGB, ColorMode.WHITE}
46  _attr_has_entity_name = True
47  _attr_name = None
48  _attr_supported_features = LightEntityFeature.EFFECT
49 
50  def __init__(
51  self, coordinator: DataUpdateCoordinator[None], device: LEDBLE, name: str
52  ) -> None:
53  """Initialize an ledble light."""
54  super().__init__(coordinator)
55  self._device_device = device
56  self._attr_unique_id_attr_unique_id = device.address
57  self._attr_device_info_attr_device_info = DeviceInfo(
58  name=name,
59  model=f"{device.model_data.description} {hex(device.model_num)}",
60  sw_version=hex(device.version_num),
61  connections={(dr.CONNECTION_BLUETOOTH, device.address)},
62  )
63  self._async_update_attrs_async_update_attrs()
64 
65  @callback
66  def _async_update_attrs(self) -> None:
67  """Handle updating _attr values."""
68  device = self._device_device
69  self._attr_color_mode_attr_color_mode = ColorMode.WHITE if device.w else ColorMode.RGB
70  self._attr_brightness_attr_brightness = device.brightness
71  self._attr_rgb_color_attr_rgb_color = device.rgb_unscaled
72  self._attr_is_on_attr_is_on = device.on
73  self._attr_effect_attr_effect = device.effect
74  self._attr_effect_list_attr_effect_list = device.effect_list
75 
76  async def _async_set_effect(self, effect: str, brightness: int) -> None:
77  """Set an effect."""
78  await self._device_device.async_set_effect(
79  effect,
80  self._device_device.speed or DEFAULT_EFFECT_SPEED,
81  round(brightness / 255 * 100),
82  )
83 
84  async def async_turn_on(self, **kwargs: Any) -> None:
85  """Instruct the light to turn on."""
86  brightness = kwargs.get(ATTR_BRIGHTNESS, self.brightnessbrightness)
87  if effect := kwargs.get(ATTR_EFFECT):
88  await self._async_set_effect_async_set_effect(effect, brightness)
89  return
90  if ATTR_RGB_COLOR in kwargs:
91  rgb = kwargs[ATTR_RGB_COLOR]
92  await self._device_device.set_rgb(rgb, brightness)
93  return
94  if ATTR_BRIGHTNESS in kwargs:
95  await self._device_device.set_brightness(brightness)
96  return
97  if ATTR_WHITE in kwargs:
98  await self._device_device.set_white(kwargs[ATTR_WHITE])
99  return
100  await self._device_device.turn_on()
101 
102  async def async_turn_off(self, **kwargs: Any) -> None:
103  """Instruct the light to turn off."""
104  await self._device_device.turn_off()
105 
106  @callback
107  def _handle_coordinator_update(self, *args: Any) -> None:
108  """Handle data update."""
109  self._async_update_attrs_async_update_attrs()
110  self.async_write_ha_stateasync_write_ha_state()
111 
112  async def async_added_to_hass(self) -> None:
113  """Register callbacks."""
114  self.async_on_removeasync_on_remove(
115  self._device_device.register_callback(self._handle_coordinator_update_handle_coordinator_update)
116  )
117  return await super().async_added_to_hass()
None __init__(self, DataUpdateCoordinator[None] coordinator, LEDBLE device, str name)
Definition: light.py:52
None _async_set_effect(self, str effect, int brightness)
Definition: light.py:76
None _handle_coordinator_update(self, *Any args)
Definition: light.py:107
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
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, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: light.py:36