Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Support for Electric Kiwi hour of free power."""
2 
3 from __future__ import annotations
4 
5 import logging
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
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 from homeassistant.helpers.update_coordinator import CoordinatorEntity
13 
14 from .const import ATTRIBUTION, DOMAIN, HOP_COORDINATOR
15 from .coordinator import ElectricKiwiHOPDataCoordinator
16 
17 _LOGGER = logging.getLogger(__name__)
18 ATTR_EK_HOP_SELECT = "hop_select"
19 
21  entity_category=EntityCategory.CONFIG,
22  key=ATTR_EK_HOP_SELECT,
23  translation_key="hop_selector",
24 )
25 
26 
28  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
29 ) -> None:
30  """Electric Kiwi select setup."""
31  hop_coordinator: ElectricKiwiHOPDataCoordinator = hass.data[DOMAIN][entry.entry_id][
32  HOP_COORDINATOR
33  ]
34 
35  _LOGGER.debug("Setting up select entity")
36  async_add_entities([ElectricKiwiSelectHOPEntity(hop_coordinator, HOP_SELECT)])
37 
38 
40  CoordinatorEntity[ElectricKiwiHOPDataCoordinator], SelectEntity
41 ):
42  """Entity object for seeing and setting the hour of free power."""
43 
44  entity_description: SelectEntityDescription
45  _attr_has_entity_name = True
46  _attr_attribution = ATTRIBUTION
47  values_dict: dict[str, int]
48 
49  def __init__(
50  self,
51  coordinator: ElectricKiwiHOPDataCoordinator,
52  description: SelectEntityDescription,
53  ) -> None:
54  """Initialise the HOP selection entity."""
55  super().__init__(coordinator)
56  self._attr_unique_id_attr_unique_id = (
57  f"{coordinator._ek_api.customer_number}" # noqa: SLF001
58  f"_{coordinator._ek_api.connection_id}_{description.key}" # noqa: SLF001
59  )
60  self.entity_descriptionentity_description = description
61  self.values_dictvalues_dict = coordinator.get_hop_options()
62  self._attr_options_attr_options = list(self.values_dictvalues_dict)
63 
64  @property
65  def current_option(self) -> str | None:
66  """Return the currently selected option."""
67  return (
68  f"{self.coordinator.data.start.start_time}"
69  f" - {self.coordinator.data.end.end_time}"
70  )
71 
72  async def async_select_option(self, option: str) -> None:
73  """Change the selected option."""
74  value = self.values_dictvalues_dict[option]
75  await self.coordinator.async_update_hop(value)
76  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, ElectricKiwiHOPDataCoordinator coordinator, SelectEntityDescription description)
Definition: select.py:53
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: select.py:29