Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Ecovacs select entity module."""
2 
3 from collections.abc import Callable
4 from dataclasses import dataclass
5 from typing import Any, Generic
6 
7 from deebot_client.capabilities import CapabilitySetTypes
8 from deebot_client.device import Device
9 from deebot_client.events import WaterInfoEvent, WorkModeEvent
10 
11 from homeassistant.components.select import SelectEntity, SelectEntityDescription
12 from homeassistant.const import EntityCategory
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from . import EcovacsConfigEntry
17 from .entity import EcovacsCapabilityEntityDescription, EcovacsDescriptionEntity, EventT
18 from .util import get_name_key, get_supported_entitites
19 
20 
21 @dataclass(kw_only=True, frozen=True)
23  SelectEntityDescription,
24  EcovacsCapabilityEntityDescription,
25  Generic[EventT],
26 ):
27  """Ecovacs select entity description."""
28 
29  current_option_fn: Callable[[EventT], str | None]
30  options_fn: Callable[[CapabilitySetTypes], list[str]]
31 
32 
33 ENTITY_DESCRIPTIONS: tuple[EcovacsSelectEntityDescription, ...] = (
34  EcovacsSelectEntityDescription[WaterInfoEvent](
35  capability_fn=lambda caps: caps.water,
36  current_option_fn=lambda e: get_name_key(e.amount),
37  options_fn=lambda water: [get_name_key(amount) for amount in water.types],
38  key="water_amount",
39  translation_key="water_amount",
40  entity_category=EntityCategory.CONFIG,
41  ),
42  EcovacsSelectEntityDescription[WorkModeEvent](
43  capability_fn=lambda caps: caps.clean.work_mode,
44  current_option_fn=lambda e: get_name_key(e.mode),
45  options_fn=lambda cap: [get_name_key(mode) for mode in cap.types],
46  key="work_mode",
47  translation_key="work_mode",
48  entity_registry_enabled_default=False,
49  entity_category=EntityCategory.CONFIG,
50  ),
51 )
52 
53 
55  hass: HomeAssistant,
56  config_entry: EcovacsConfigEntry,
57  async_add_entities: AddEntitiesCallback,
58 ) -> None:
59  """Add entities for passed config_entry in HA."""
60  controller = config_entry.runtime_data
61  entities = get_supported_entitites(
62  controller, EcovacsSelectEntity, ENTITY_DESCRIPTIONS
63  )
64  if entities:
65  async_add_entities(entities)
66 
67 
69  EcovacsDescriptionEntity[CapabilitySetTypes[EventT, str]],
70  SelectEntity,
71 ):
72  """Ecovacs select entity."""
73 
74  _attr_current_option: str | None = None
75  entity_description: EcovacsSelectEntityDescription
76 
77  def __init__(
78  self,
79  device: Device,
80  capability: CapabilitySetTypes[EventT, str],
81  entity_description: EcovacsSelectEntityDescription,
82  **kwargs: Any,
83  ) -> None:
84  """Initialize entity."""
85  super().__init__(device, capability, entity_description, **kwargs)
86  self._attr_options_attr_options = entity_description.options_fn(capability)
87 
88  async def async_added_to_hass(self) -> None:
89  """Set up the event listeners now that hass is ready."""
90  await super().async_added_to_hass()
91 
92  async def on_event(event: EventT) -> None:
93  self._attr_current_option_attr_current_option = self.entity_descriptionentity_description.current_option_fn(event)
94  self.async_write_ha_stateasync_write_ha_state()
95 
96  self._subscribe_subscribe(self._capability_capability.event, on_event)
97 
98  async def async_select_option(self, option: str) -> None:
99  """Change the selected option."""
100  await self._device_device.execute_command(self._capability_capability.set(option))
None _subscribe(self, type[EventT] event_type, Callable[[EventT], Coroutine[Any, Any, None]] callback)
Definition: entity.py:87
None __init__(self, Device device, CapabilitySetTypes[EventT, str] capability, EcovacsSelectEntityDescription entity_description, **Any kwargs)
Definition: select.py:83
None async_setup_entry(HomeAssistant hass, EcovacsConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: select.py:58
list[EcovacsEntity] get_supported_entitites(EcovacsController controller, type[EcovacsDescriptionEntity] entity_class, tuple[EcovacsCapabilityEntityDescription,...] descriptions)
Definition: util.py:37