Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Support for DROP selects."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Awaitable, Callable
6 from dataclasses import dataclass
7 import logging
8 from typing import Any
9 
10 from homeassistant.components.select import SelectEntity, SelectEntityDescription
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from .const import CONF_DEVICE_TYPE, DEV_HUB, DOMAIN
16 from .coordinator import DROPDeviceDataUpdateCoordinator
17 from .entity import DROPEntity
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 # Select type constants
22 PROTECT_MODE = "protect_mode"
23 
24 PROTECT_MODE_OPTIONS = ["away", "home", "schedule"]
25 
26 
27 @dataclass(kw_only=True, frozen=True)
29  """Describes DROP select entity."""
30 
31  value_fn: Callable[[DROPDeviceDataUpdateCoordinator], int | None]
32  set_fn: Callable[[DROPDeviceDataUpdateCoordinator, str], Awaitable[Any]]
33 
34 
35 SELECTS: list[DROPSelectEntityDescription] = [
37  key=PROTECT_MODE,
38  translation_key=PROTECT_MODE,
39  options=PROTECT_MODE_OPTIONS,
40  value_fn=lambda device: device.drop_api.protect_mode(),
41  set_fn=lambda device, value: device.set_protect_mode(value),
42  )
43 ]
44 
45 # Defines which selects are used by each device type
46 DEVICE_SELECTS: dict[str, list[str]] = {
47  DEV_HUB: [PROTECT_MODE],
48 }
49 
50 
52  hass: HomeAssistant,
53  config_entry: ConfigEntry,
54  async_add_entities: AddEntitiesCallback,
55 ) -> None:
56  """Set up the DROP selects from config entry."""
57  _LOGGER.debug(
58  "Set up select for device type %s with entry_id is %s",
59  config_entry.data[CONF_DEVICE_TYPE],
60  config_entry.entry_id,
61  )
62 
63  if config_entry.data[CONF_DEVICE_TYPE] in DEVICE_SELECTS:
65  DROPSelect(hass.data[DOMAIN][config_entry.entry_id], select)
66  for select in SELECTS
67  if select.key in DEVICE_SELECTS[config_entry.data[CONF_DEVICE_TYPE]]
68  )
69 
70 
72  """Representation of a DROP select."""
73 
74  entity_description: DROPSelectEntityDescription
75 
76  def __init__(
77  self,
78  coordinator: DROPDeviceDataUpdateCoordinator,
79  entity_description: DROPSelectEntityDescription,
80  ) -> None:
81  """Initialize the select."""
82  super().__init__(entity_description.key, coordinator)
83  self.entity_descriptionentity_description = entity_description
84 
85  @property
86  def current_option(self) -> str | None:
87  """Return the current selected option."""
88  val = self.entity_descriptionentity_description.value_fn(self.coordinator)
89  return str(val) if val else None
90 
91  async def async_select_option(self, option: str) -> None:
92  """Update the current selected option."""
93  await self.entity_descriptionentity_description.set_fn(self.coordinator, option)
None __init__(self, DROPDeviceDataUpdateCoordinator coordinator, DROPSelectEntityDescription entity_description)
Definition: select.py:80
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: select.py:55