Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Support for Broadlink selects."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.components.select import SelectEntity
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 from . import BroadlinkDevice
13 from .const import DOMAIN
14 from .entity import BroadlinkEntity
15 
16 DAY_ID_TO_NAME = {
17  1: "monday",
18  2: "tuesday",
19  3: "wednesday",
20  4: "thursday",
21  5: "friday",
22  6: "saturday",
23  7: "sunday",
24 }
25 DAY_NAME_TO_ID = {v: k for k, v in DAY_ID_TO_NAME.items()}
26 
27 
29  hass: HomeAssistant,
30  config_entry: ConfigEntry,
31  async_add_entities: AddEntitiesCallback,
32 ) -> None:
33  """Set up the Broadlink select."""
34  device = hass.data[DOMAIN].devices[config_entry.entry_id]
36 
37 
39  """Representation of a Broadlink day of week."""
40 
41  _attr_has_entity_name = True
42  _attr_current_option: str | None = None
43  _attr_options = list(DAY_NAME_TO_ID)
44  _attr_translation_key = "day_of_week"
45 
46  def __init__(self, device: BroadlinkDevice) -> None:
47  """Initialize the select."""
48  super().__init__(device)
49 
50  self._attr_unique_id_attr_unique_id = f"{device.unique_id}-dayofweek"
51 
52  def _update_state(self, data: dict[str, Any]) -> None:
53  """Update the state of the entity."""
54  if data is None or "dayofweek" not in data:
55  self._attr_current_option_attr_current_option = None
56  else:
57  self._attr_current_option_attr_current_option = DAY_ID_TO_NAME[data["dayofweek"]]
58 
59  async def async_select_option(self, option: str) -> None:
60  """Change the selected option."""
61  await self._device_device.async_request(
62  self._device_device.api.set_time,
63  hour=self._coordinator_coordinator.data["hour"],
64  minute=self._coordinator_coordinator.data["min"],
65  second=self._coordinator_coordinator.data["sec"],
66  day=DAY_NAME_TO_ID[option],
67  )
68  self._attr_current_option_attr_current_option = option
69  self.async_write_ha_stateasync_write_ha_state()