Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Creates a select entity for the headlight of the mower."""
2 
3 import logging
4 from typing import cast
5 
6 from aioautomower.model import HeadlightModes
7 
8 from homeassistant.components.select import SelectEntity
9 from homeassistant.const import EntityCategory
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from . import AutomowerConfigEntry
14 from .coordinator import AutomowerDataUpdateCoordinator
15 from .entity import AutomowerControlEntity, handle_sending_exception
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 PARALLEL_UPDATES = 1
20 
21 HEADLIGHT_MODES: list = [
22  HeadlightModes.ALWAYS_OFF.lower(),
23  HeadlightModes.ALWAYS_ON.lower(),
24  HeadlightModes.EVENING_AND_NIGHT.lower(),
25  HeadlightModes.EVENING_ONLY.lower(),
26 ]
27 
28 
30  hass: HomeAssistant,
31  entry: AutomowerConfigEntry,
32  async_add_entities: AddEntitiesCallback,
33 ) -> None:
34  """Set up select platform."""
35  coordinator = entry.runtime_data
37  AutomowerSelectEntity(mower_id, coordinator)
38  for mower_id in coordinator.data
39  if coordinator.data[mower_id].capabilities.headlights
40  )
41 
42 
44  """Defining the headlight mode entity."""
45 
46  _attr_options = HEADLIGHT_MODES
47  _attr_entity_category = EntityCategory.CONFIG
48  _attr_translation_key = "headlight_mode"
49 
50  def __init__(
51  self,
52  mower_id: str,
53  coordinator: AutomowerDataUpdateCoordinator,
54  ) -> None:
55  """Set up select platform."""
56  super().__init__(mower_id, coordinator)
57  self._attr_unique_id_attr_unique_id = f"{mower_id}_headlight_mode"
58 
59  @property
60  def current_option(self) -> str:
61  """Return the current option for the entity."""
62  return cast(
63  HeadlightModes, self.mower_attributesmower_attributes.settings.headlight.mode
64  ).lower()
65 
66  @handle_sending_exception()
67  async def async_select_option(self, option: str) -> None:
68  """Change the selected option."""
69  await self.coordinator.api.commands.set_headlight_mode(
70  self.mower_idmower_id, cast(HeadlightModes, option.upper())
71  )
None __init__(self, str mower_id, AutomowerDataUpdateCoordinator coordinator)
Definition: select.py:54
None async_setup_entry(HomeAssistant hass, AutomowerConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: select.py:33