Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Support for Lektrico select entities."""
2 
3 from collections.abc import Callable, Coroutine
4 from dataclasses import dataclass
5 from typing import Any
6 
7 from lektricowifi import Device
8 
9 from homeassistant.components.select import SelectEntity, SelectEntityDescription
10 from homeassistant.const import ATTR_SERIAL_NUMBER, CONF_TYPE, EntityCategory
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from . import LektricoConfigEntry, LektricoDeviceDataUpdateCoordinator
15 from .entity import LektricoEntity
16 
17 
18 @dataclass(frozen=True, kw_only=True)
20  """Describes Lektrico select entity."""
21 
22  value_fn: Callable[[dict[str, Any]], str]
23  set_value_fn: Callable[[Device, int], Coroutine[Any, Any, dict[Any, Any]]]
24 
25 
26 LB_MODE_OPTIONS = [
27  "disabled",
28  "power",
29  "hybrid",
30  "green",
31 ]
32 
33 
34 SELECTS: tuple[LektricoSelectEntityDescription, ...] = (
36  key="load_balancing_mode",
37  translation_key="load_balancing_mode",
38  options=LB_MODE_OPTIONS,
39  entity_category=EntityCategory.CONFIG,
40  value_fn=lambda data: LB_MODE_OPTIONS[data["lb_mode"]],
41  set_value_fn=lambda device, value: device.set_load_balancing_mode(value),
42  ),
43 )
44 
45 
47  hass: HomeAssistant,
48  entry: LektricoConfigEntry,
49  async_add_entities: AddEntitiesCallback,
50 ) -> None:
51  """Set up Lektrico select entities based on a config entry."""
52 
53  coordinator = entry.runtime_data
54 
57  description,
58  coordinator,
59  f"{entry.data[CONF_TYPE]}_{entry.data[ATTR_SERIAL_NUMBER]}",
60  )
61  for description in SELECTS
62  )
63 
64 
66  """Defines a Lektrico select entity."""
67 
68  entity_description: LektricoSelectEntityDescription
69 
70  def __init__(
71  self,
72  description: LektricoSelectEntityDescription,
73  coordinator: LektricoDeviceDataUpdateCoordinator,
74  device_name: str,
75  ) -> None:
76  """Initialize Lektrico select."""
77  super().__init__(coordinator, device_name)
78  self.entity_descriptionentity_description = description
79  self._attr_unique_id_attr_unique_id = f"{coordinator.serial_number}_{description.key}"
80 
81  @property
82  def current_option(self) -> str | None:
83  """Return the state of the select."""
84  return self.entity_descriptionentity_description.value_fn(self.coordinator.data)
85 
86  async def async_select_option(self, option: str) -> None:
87  """Change the selected option."""
88  await self.entity_descriptionentity_description.set_value_fn(
89  self.coordinator.device, LB_MODE_OPTIONS.index(option)
90  )
91  await self.coordinator.async_request_refresh()
None __init__(self, LektricoSelectEntityDescription description, LektricoDeviceDataUpdateCoordinator coordinator, str device_name)
Definition: select.py:75
None async_setup_entry(HomeAssistant hass, LektricoConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: select.py:50