Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Support for SleepIQ foundation preset selection."""
2 
3 from __future__ import annotations
4 
5 from asyncsleepiq import (
6  FootWarmingTemps,
7  Side,
8  SleepIQBed,
9  SleepIQFootWarmer,
10  SleepIQPreset,
11 )
12 
13 from homeassistant.components.select import SelectEntity
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.core import HomeAssistant, callback
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from .const import DOMAIN, FOOT_WARMER
19 from .coordinator import SleepIQData, SleepIQDataUpdateCoordinator
20 from .entity import SleepIQBedEntity, SleepIQSleeperEntity, sleeper_for_side
21 
22 
24  hass: HomeAssistant,
25  entry: ConfigEntry,
26  async_add_entities: AddEntitiesCallback,
27 ) -> None:
28  """Set up the SleepIQ foundation preset select entities."""
29  data: SleepIQData = hass.data[DOMAIN][entry.entry_id]
30  entities: list[SleepIQBedEntity] = []
31  for bed in data.client.beds.values():
32  entities.extend(
33  SleepIQSelectEntity(data.data_coordinator, bed, preset)
34  for preset in bed.foundation.presets
35  )
36  entities.extend(
37  SleepIQFootWarmingTempSelectEntity(data.data_coordinator, bed, foot_warmer)
38  for foot_warmer in bed.foundation.foot_warmers
39  )
40  async_add_entities(entities)
41 
42 
43 class SleepIQSelectEntity(SleepIQBedEntity[SleepIQDataUpdateCoordinator], SelectEntity):
44  """Representation of a SleepIQ select entity."""
45 
46  def __init__(
47  self,
48  coordinator: SleepIQDataUpdateCoordinator,
49  bed: SleepIQBed,
50  preset: SleepIQPreset,
51  ) -> None:
52  """Initialize the select entity."""
53  self.presetpreset = preset
54 
55  self._attr_name_attr_name = f"SleepNumber {bed.name} Foundation Preset"
56  self._attr_unique_id_attr_unique_id = f"{bed.id}_preset"
57  if preset.side != Side.NONE:
58  self._attr_name_attr_name += f" {preset.side_full}"
59  self._attr_unique_id_attr_unique_id += f"_{preset.side.value}"
60  self._attr_options_attr_options = preset.options
61 
62  super().__init__(coordinator, bed)
63  self._async_update_attrs_async_update_attrs()
64 
65  @callback
66  def _async_update_attrs(self) -> None:
67  """Update entity attributes."""
68  self._attr_current_option_attr_current_option = self.presetpreset.preset
69 
70  async def async_select_option(self, option: str) -> None:
71  """Change the current preset."""
72  await self.presetpreset.set_preset(option)
73  self._attr_current_option_attr_current_option = option
74  self.async_write_ha_stateasync_write_ha_state()
75 
76 
78  SleepIQSleeperEntity[SleepIQDataUpdateCoordinator], SelectEntity
79 ):
80  """Representation of a SleepIQ foot warming temperature select entity."""
81 
82  _attr_icon = "mdi:heat-wave"
83  _attr_options = [e.name.lower() for e in FootWarmingTemps]
84  _attr_translation_key = "foot_warmer_temp"
85 
86  def __init__(
87  self,
88  coordinator: SleepIQDataUpdateCoordinator,
89  bed: SleepIQBed,
90  foot_warmer: SleepIQFootWarmer,
91  ) -> None:
92  """Initialize the select entity."""
93  self.foot_warmerfoot_warmer = foot_warmer
94  sleeper = sleeper_for_side(bed, foot_warmer.side)
95  super().__init__(coordinator, bed, sleeper, FOOT_WARMER)
96  self._async_update_attrs_async_update_attrs()
97 
98  @callback
99  def _async_update_attrs(self) -> None:
100  """Update entity attributes."""
101  self._attr_current_option_attr_current_option = FootWarmingTemps(
102  self.foot_warmerfoot_warmer.temperature
103  ).name.lower()
104 
105  async def async_select_option(self, option: str) -> None:
106  """Change the current preset."""
107  temperature = FootWarmingTemps[option.upper()]
108  timer = self.foot_warmerfoot_warmer.timer or 120
109 
110  if temperature == 0:
111  await self.foot_warmerfoot_warmer.turn_off()
112  else:
113  await self.foot_warmerfoot_warmer.turn_on(temperature, timer)
114 
115  self._attr_current_option_attr_current_option = option
116  await self.coordinator.async_request_refresh()
117  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, SleepIQDataUpdateCoordinator coordinator, SleepIQBed bed, SleepIQFootWarmer foot_warmer)
Definition: select.py:91
None __init__(self, SleepIQDataUpdateCoordinator coordinator, SleepIQBed bed, SleepIQPreset preset)
Definition: select.py:51
SleepIQSleeper sleeper_for_side(SleepIQBed bed, str side)
Definition: entity.py:29
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: select.py:27