Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Support for MotionMount numeric control."""
2 
3 from datetime import timedelta
4 import logging
5 import socket
6 
7 import motionmount
8 
9 from homeassistant.components.select import SelectEntity
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.core import HomeAssistant
12 from homeassistant.exceptions import HomeAssistantError
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from .const import DOMAIN, WALL_PRESET_NAME
16 from .entity import MotionMountEntity
17 
18 _LOGGER = logging.getLogger(__name__)
19 SCAN_INTERVAL = timedelta(seconds=60)
20 
21 
23  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
24 ) -> None:
25  """Set up Vogel's MotionMount from a config entry."""
26  mm = hass.data[DOMAIN][entry.entry_id]
27 
28  async_add_entities([MotionMountPresets(mm, entry)], True)
29 
30 
32  """The presets of a MotionMount."""
33 
34  _attr_should_poll = True
35  _attr_translation_key = "motionmount_preset"
36 
37  def __init__(
38  self,
39  mm: motionmount.MotionMount,
40  config_entry: ConfigEntry,
41  ) -> None:
42  """Initialize Preset selector."""
43  super().__init__(mm, config_entry)
44  self._attr_unique_id_attr_unique_id = f"{self._base_unique_id}-preset"
45  self._presets_presets: list[motionmount.Preset] = []
46 
47  def _update_options(self, presets: list[motionmount.Preset]) -> None:
48  """Convert presets to select options."""
49  options = [f"{preset.index}: {preset.name}" for preset in presets]
50  options.insert(0, WALL_PRESET_NAME)
51 
52  self._attr_options_attr_options = options
53 
54  async def async_update(self) -> None:
55  """Get latest state from MotionMount."""
56  if not await self._ensure_connected_ensure_connected():
57  return
58 
59  try:
60  self._presets_presets = await self.mmmm.get_presets()
61  except (TimeoutError, socket.gaierror) as ex:
62  _LOGGER.warning("Failed to communicate with MotionMount: %s", ex)
63  else:
64  self._update_options_update_options(self._presets_presets)
65 
66  @property
67  def current_option(self) -> str | None:
68  """Get the current option."""
69  # When the mount is moving we return the currently selected option
70  if self.mmmm.is_moving:
71  return self._attr_current_option_attr_current_option
72 
73  # When the mount isn't moving we select the option that matches the current position
74  self._attr_current_option_attr_current_option = None
75  if self.mmmm.extension == 0 and self.mmmm.turn == 0:
76  self._attr_current_option_attr_current_option = self._attr_options_attr_options[0] # Select Wall preset
77  else:
78  for preset in self._presets_presets:
79  if (
80  preset.extension == self.mmmm.extension
81  and preset.turn == self.mmmm.turn
82  ):
83  self._attr_current_option_attr_current_option = f"{preset.index}: {preset.name}"
84  break
85 
86  return self._attr_current_option_attr_current_option
87 
88  async def async_select_option(self, option: str) -> None:
89  """Set the new option."""
90  index = int(option[:1])
91  try:
92  await self.mmmm.go_to_preset(index)
93  except (TimeoutError, socket.gaierror) as ex:
94  raise HomeAssistantError(
95  translation_domain=DOMAIN,
96  translation_key="failed_communication",
97  ) from ex
98  else:
99  self._attr_current_option_attr_current_option = option
None _update_options(self, list[motionmount.Preset] presets)
Definition: select.py:47
None __init__(self, motionmount.MotionMount mm, ConfigEntry config_entry)
Definition: select.py:41
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: select.py:24