Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Support for tariff selection."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from homeassistant.components.select import SelectEntity
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.const import CONF_NAME, CONF_UNIQUE_ID
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.device import async_device_info_to_link_from_entity
12 from homeassistant.helpers.device_registry import DeviceInfo
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 from homeassistant.helpers.restore_state import RestoreEntity
15 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
16 
17 from .const import CONF_METER, CONF_SOURCE_SENSOR, CONF_TARIFFS, DATA_UTILITY
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 
23  hass: HomeAssistant,
24  config_entry: ConfigEntry,
25  async_add_entities: AddEntitiesCallback,
26 ) -> None:
27  """Initialize Utility Meter config entry."""
28  name = config_entry.title
29  tariffs: list[str] = config_entry.options[CONF_TARIFFS]
30 
31  unique_id = config_entry.entry_id
32 
34  hass,
35  config_entry.options[CONF_SOURCE_SENSOR],
36  )
37 
38  tariff_select = TariffSelect(
39  name=name,
40  tariffs=tariffs,
41  unique_id=unique_id,
42  device_info=device_info,
43  )
44  async_add_entities([tariff_select])
45 
46 
48  hass: HomeAssistant,
49  conf: ConfigType,
50  async_add_entities: AddEntitiesCallback,
51  discovery_info: DiscoveryInfoType | None = None,
52 ) -> None:
53  """Set up the utility meter select."""
54  if discovery_info is None:
55  _LOGGER.error(
56  "This platform is not available to configure "
57  "from 'select:' in configuration.yaml"
58  )
59  return
60 
61  meter: str = discovery_info[CONF_METER]
62  conf_meter_unique_id: str | None = hass.data[DATA_UTILITY][meter].get(
63  CONF_UNIQUE_ID
64  )
65  conf_meter_name = hass.data[DATA_UTILITY][meter].get(CONF_NAME, meter)
66 
68  [
70  name=conf_meter_name,
71  tariffs=discovery_info[CONF_TARIFFS],
72  yaml_slug=meter,
73  unique_id=conf_meter_unique_id,
74  )
75  ]
76  )
77 
78 
80  """Representation of a Tariff selector."""
81 
82  _attr_translation_key = "tariff"
83 
84  def __init__(
85  self,
86  name,
87  tariffs: list[str],
88  *,
89  yaml_slug: str | None = None,
90  unique_id: str | None = None,
91  device_info: DeviceInfo | None = None,
92  ) -> None:
93  """Initialize a tariff selector."""
94  self._attr_name_attr_name = name
95  if yaml_slug: # Backwards compatibility with YAML configuration entries
96  self.entity_identity_identity_id = f"select.{yaml_slug}"
97  self._attr_unique_id_attr_unique_id = unique_id
98  self._attr_device_info_attr_device_info = device_info
99  self._current_tariff_current_tariff: str | None = None
100  self._tariffs_tariffs = tariffs
101  self._attr_should_poll_attr_should_poll = False
102 
103  @property
104  def options(self) -> list[str]:
105  """Return the available tariffs."""
106  return self._tariffs_tariffs
107 
108  @property
109  def current_option(self) -> str | None:
110  """Return current tariff."""
111  return self._current_tariff_current_tariff
112 
113  async def async_added_to_hass(self) -> None:
114  """Run when entity about to be added."""
115  await super().async_added_to_hass()
116 
117  state = await self.async_get_last_stateasync_get_last_state()
118  if not state or state.state not in self._tariffs_tariffs:
119  self._current_tariff_current_tariff = self._tariffs_tariffs[0]
120  else:
121  self._current_tariff_current_tariff = state.state
122 
123  async def async_select_option(self, option: str) -> None:
124  """Select new tariff (option)."""
125  self._current_tariff_current_tariff = option
126  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, name, list[str] tariffs, *str|None yaml_slug=None, str|None unique_id=None, DeviceInfo|None device_info=None)
Definition: select.py:92
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_platform(HomeAssistant hass, ConfigType conf, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: select.py:52
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: select.py:26
dr.DeviceInfo|None async_device_info_to_link_from_entity(HomeAssistant hass, str entity_id_or_uuid)
Definition: device.py:28