Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Support for hunterdouglass_powerview settings."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable, Coroutine
6 from dataclasses import dataclass
7 from typing import Any, Final
8 
9 from aiopvapi.helpers.constants import ATTR_NAME, FUNCTION_SET_POWER
10 from aiopvapi.resources.shade import BaseShade
11 
12 from homeassistant.components.select import SelectEntity, SelectEntityDescription
13 from homeassistant.const import EntityCategory
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from .coordinator import PowerviewShadeUpdateCoordinator
18 from .entity import ShadeEntity
19 from .model import PowerviewConfigEntry, PowerviewDeviceInfo
20 
21 
22 @dataclass(frozen=True)
24  """Mixin to describe a select entity."""
25 
26  current_fn: Callable[[BaseShade], Any]
27  select_fn: Callable[[BaseShade, str], Coroutine[Any, Any, bool]]
28  create_entity_fn: Callable[[BaseShade], bool]
29  options_fn: Callable[[BaseShade], list[str]]
30 
31 
32 @dataclass(frozen=True)
34  SelectEntityDescription, PowerviewSelectDescriptionMixin
35 ):
36  """A class that describes select entities."""
37 
38  entity_category: EntityCategory = EntityCategory.CONFIG
39 
40 
41 DROPDOWNS: Final = [
43  key="powersource",
44  translation_key="power_source",
45  icon="mdi:power-plug-outline",
46  current_fn=lambda shade: shade.get_power_source(),
47  options_fn=lambda shade: shade.supported_power_sources(),
48  select_fn=lambda shade, option: shade.set_power_source(option),
49  create_entity_fn=lambda shade: shade.is_supported(FUNCTION_SET_POWER),
50  ),
51 ]
52 
53 
55  hass: HomeAssistant,
56  entry: PowerviewConfigEntry,
57  async_add_entities: AddEntitiesCallback,
58 ) -> None:
59  """Set up the hunter douglas select entities."""
60  pv_entry = entry.runtime_data
61  entities: list[PowerViewSelect] = []
62  for shade in pv_entry.shade_data.values():
63  if not shade.has_battery_info():
64  continue
65  room_name = getattr(pv_entry.room_data.get(shade.room_id), ATTR_NAME, "")
66  entities.extend(
68  pv_entry.coordinator,
69  pv_entry.device_info,
70  room_name,
71  shade,
72  shade.name,
73  description,
74  )
75  for description in DROPDOWNS
76  if description.create_entity_fn(shade)
77  )
78  async_add_entities(entities)
79 
80 
82  """Representation of a select entity."""
83 
84  def __init__(
85  self,
86  coordinator: PowerviewShadeUpdateCoordinator,
87  device_info: PowerviewDeviceInfo,
88  room_name: str,
89  shade: BaseShade,
90  name: str,
91  description: PowerviewSelectDescription,
92  ) -> None:
93  """Initialize the select entity."""
94  super().__init__(coordinator, device_info, room_name, shade, name)
95  self.entity_description: PowerviewSelectDescription = description
96  self._attr_unique_id_attr_unique_id_attr_unique_id = f"{self._attr_unique_id}_{description.key}"
97 
98  @property
99  def current_option(self) -> str | None:
100  """Return the selected entity option to represent the entity state."""
101  return self.entity_description.current_fn(self._shade_shade)
102 
103  @property
104  def options(self) -> list[str]:
105  """Return a set of selectable options."""
106  return self.entity_description.options_fn(self._shade_shade)
107 
108  async def async_select_option(self, option: str) -> None:
109  """Change the selected option."""
110  await self.entity_description.select_fn(self._shade_shade, option)
111  # force update data to ensure new info is in coordinator
112  async with self.coordinator.radio_operation_lock:
113  await self._shade_shade.refresh(suppress_timeout=True)
114  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, PowerviewShadeUpdateCoordinator coordinator, PowerviewDeviceInfo device_info, str room_name, BaseShade shade, str name, PowerviewSelectDescription description)
Definition: select.py:92
None async_setup_entry(HomeAssistant hass, PowerviewConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: select.py:58