Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Select entities for the Motionblinds Bluetooth integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from motionblindsble.const import MotionBlindType, MotionSpeedLevel
8 from motionblindsble.device import MotionDevice
9 
10 from homeassistant.components.select import SelectEntity, SelectEntityDescription
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import EntityCategory
13 from homeassistant.core import HomeAssistant, callback
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from .const import ATTR_SPEED, CONF_MAC_CODE, DOMAIN
17 from .entity import MotionblindsBLEEntity
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 PARALLEL_UPDATES = 0
22 
23 
24 SELECT_TYPES: dict[str, SelectEntityDescription] = {
25  ATTR_SPEED: SelectEntityDescription(
26  key=ATTR_SPEED,
27  translation_key=ATTR_SPEED,
28  entity_category=EntityCategory.CONFIG,
29  options=["1", "2", "3"],
30  )
31 }
32 
33 
35  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
36 ) -> None:
37  """Set up select entities based on a config entry."""
38 
39  device: MotionDevice = hass.data[DOMAIN][entry.entry_id]
40 
41  if device.blind_type not in {MotionBlindType.CURTAIN, MotionBlindType.VERTICAL}:
42  async_add_entities([SpeedSelect(device, entry, SELECT_TYPES[ATTR_SPEED])])
43 
44 
46  """Representation of a speed select entity."""
47 
48  def __init__(
49  self,
50  device: MotionDevice,
51  entry: ConfigEntry,
52  entity_description: SelectEntityDescription,
53  ) -> None:
54  """Initialize the speed select entity."""
55  super().__init__(
56  device, entry, entity_description, unique_id_suffix=entity_description.key
57  )
58  self._attr_current_option_attr_current_option = None
59 
60  async def async_added_to_hass(self) -> None:
61  """Register device callbacks."""
62  _LOGGER.debug(
63  "(%s) Setting up speed select entity",
64  self.entryentry.data[CONF_MAC_CODE],
65  )
66  self.devicedevice.register_speed_callback(self.async_update_speedasync_update_speed)
67 
68  @callback
69  def async_update_speed(self, speed_level: MotionSpeedLevel | None) -> None:
70  """Update the speed sensor value."""
71  self._attr_current_option_attr_current_option = str(speed_level.value) if speed_level else None
72  self.async_write_ha_stateasync_write_ha_state()
73 
74  async def async_select_option(self, option: str) -> None:
75  """Change the selected speed sensor value."""
76  speed_level = MotionSpeedLevel(int(option))
77  await self.devicedevice.speed(speed_level)
78  self._attr_current_option_attr_current_option = str(speed_level.value) if speed_level else None
79  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, MotionDevice device, ConfigEntry entry, SelectEntityDescription entity_description)
Definition: select.py:53
None async_update_speed(self, MotionSpeedLevel|None speed_level)
Definition: select.py:69
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: select.py:36