Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Select sensor entities for LIFX integration."""
2 
3 from __future__ import annotations
4 
5 from aiolifx_themes.themes import ThemeLibrary
6 
7 from homeassistant.components.select import SelectEntity, SelectEntityDescription
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.const import EntityCategory
10 from homeassistant.core import HomeAssistant, callback
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from .const import (
14  ATTR_THEME,
15  DOMAIN,
16  INFRARED_BRIGHTNESS,
17  INFRARED_BRIGHTNESS_VALUES_MAP,
18 )
19 from .coordinator import LIFXUpdateCoordinator
20 from .entity import LIFXEntity
21 from .util import lifx_features
22 
23 THEME_NAMES = [theme_name.lower() for theme_name in ThemeLibrary().themes]
24 
25 INFRARED_BRIGHTNESS_ENTITY = SelectEntityDescription(
26  key=INFRARED_BRIGHTNESS,
27  translation_key="infrared_brightness",
28  entity_category=EntityCategory.CONFIG,
29  options=list(INFRARED_BRIGHTNESS_VALUES_MAP.values()),
30 )
31 
32 THEME_ENTITY = SelectEntityDescription(
33  key=ATTR_THEME,
34  translation_key="theme",
35  entity_category=EntityCategory.CONFIG,
36  options=THEME_NAMES,
37 )
38 
39 
41  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
42 ) -> None:
43  """Set up LIFX from a config entry."""
44  coordinator: LIFXUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
45 
46  entities: list[LIFXEntity] = []
47 
48  if lifx_features(coordinator.device)["infrared"]:
49  entities.append(
50  LIFXInfraredBrightnessSelectEntity(coordinator, INFRARED_BRIGHTNESS_ENTITY)
51  )
52 
53  if (
54  lifx_features(coordinator.device)["multizone"] is True
55  or lifx_features(coordinator.device)["matrix"] is True
56  ):
57  entities.append(LIFXThemeSelectEntity(coordinator, THEME_ENTITY))
58 
59  async_add_entities(entities)
60 
61 
63  """LIFX Nightvision infrared brightness configuration entity."""
64 
65  def __init__(
66  self,
67  coordinator: LIFXUpdateCoordinator,
68  description: SelectEntityDescription,
69  ) -> None:
70  """Initialise the IR brightness config entity."""
71  super().__init__(coordinator)
72  self.entity_descriptionentity_description = description
73  self._attr_unique_id_attr_unique_id = f"{coordinator.serial_number}_{description.key}"
74  self._attr_current_option_attr_current_option = coordinator.current_infrared_brightness
75 
76  @callback
77  def _handle_coordinator_update(self) -> None:
78  """Handle updated data from the coordinator."""
79  self._async_update_attrs_async_update_attrs()
81 
82  @callback
83  def _async_update_attrs(self) -> None:
84  """Handle coordinator updates."""
85  self._attr_current_option_attr_current_option = self.coordinator.current_infrared_brightness
86 
87  async def async_select_option(self, option: str) -> None:
88  """Update the infrared brightness value."""
89  await self.coordinator.async_set_infrared_brightness(option)
90 
91 
93  """Theme entity for LIFX multizone devices."""
94 
95  def __init__(
96  self,
97  coordinator: LIFXUpdateCoordinator,
98  description: SelectEntityDescription,
99  ) -> None:
100  """Initialise the theme selection entity."""
101 
102  super().__init__(coordinator)
103  self.entity_descriptionentity_description = description
104  self._attr_unique_id_attr_unique_id = f"{coordinator.serial_number}_{description.key}"
105  self._attr_current_option_attr_current_option = None
106 
107  @callback
108  def _handle_coordinator_update(self) -> None:
109  """Handle updated data from the coordinator."""
110  self._async_update_attrs_async_update_attrs()
112 
113  @callback
114  def _async_update_attrs(self) -> None:
115  """Update attrs from coordinator data."""
116  self._attr_current_option_attr_current_option = self.coordinator.last_used_theme
117 
118  async def async_select_option(self, option: str) -> None:
119  """Paint the selected theme onto the device."""
120  await self.coordinator.async_apply_theme(option.lower())
None __init__(self, LIFXUpdateCoordinator coordinator, SelectEntityDescription description)
Definition: select.py:69
None __init__(self, LIFXUpdateCoordinator coordinator, SelectEntityDescription description)
Definition: select.py:99
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: select.py:42
dict[str, Any] lifx_features(Light bulb)
Definition: util.py:78