2022-03-24 02:14:39 +00:00
|
|
|
"""Support for SleepIQ foundation preset selection."""
|
2024-03-08 13:33:51 +00:00
|
|
|
|
2022-03-24 02:14:39 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-12-26 22:31:00 +00:00
|
|
|
from asyncsleepiq import (
|
|
|
|
FootWarmingTemps,
|
|
|
|
Side,
|
|
|
|
SleepIQBed,
|
|
|
|
SleepIQFootWarmer,
|
|
|
|
SleepIQPreset,
|
|
|
|
)
|
2022-03-24 02:14:39 +00:00
|
|
|
|
|
|
|
from homeassistant.components.select import SelectEntity
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
2023-12-26 22:31:00 +00:00
|
|
|
from .const import DOMAIN, FOOT_WARMER
|
2023-01-07 13:13:16 +00:00
|
|
|
from .coordinator import SleepIQData, SleepIQDataUpdateCoordinator
|
2023-12-26 22:31:00 +00:00
|
|
|
from .entity import SleepIQBedEntity, SleepIQSleeperEntity, sleeper_for_side
|
2022-03-24 02:14:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up the SleepIQ foundation preset select entities."""
|
|
|
|
data: SleepIQData = hass.data[DOMAIN][entry.entry_id]
|
2023-12-26 22:31:00 +00:00
|
|
|
entities: list[SleepIQBedEntity] = []
|
|
|
|
for bed in data.client.beds.values():
|
2024-03-14 09:22:20 +00:00
|
|
|
entities.extend(
|
|
|
|
SleepIQSelectEntity(data.data_coordinator, bed, preset)
|
|
|
|
for preset in bed.foundation.presets
|
|
|
|
)
|
|
|
|
entities.extend(
|
|
|
|
SleepIQFootWarmingTempSelectEntity(data.data_coordinator, bed, foot_warmer)
|
|
|
|
for foot_warmer in bed.foundation.foot_warmers
|
|
|
|
)
|
2023-12-26 22:31:00 +00:00
|
|
|
async_add_entities(entities)
|
2022-03-24 02:14:39 +00:00
|
|
|
|
|
|
|
|
2023-01-07 13:13:16 +00:00
|
|
|
class SleepIQSelectEntity(SleepIQBedEntity[SleepIQDataUpdateCoordinator], SelectEntity):
|
2022-03-24 02:14:39 +00:00
|
|
|
"""Representation of a SleepIQ select entity."""
|
|
|
|
|
|
|
|
def __init__(
|
2023-01-07 13:13:16 +00:00
|
|
|
self,
|
|
|
|
coordinator: SleepIQDataUpdateCoordinator,
|
|
|
|
bed: SleepIQBed,
|
|
|
|
preset: SleepIQPreset,
|
2022-03-24 02:14:39 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize the select entity."""
|
|
|
|
self.preset = preset
|
|
|
|
|
2022-04-02 08:44:16 +00:00
|
|
|
self._attr_name = f"SleepNumber {bed.name} Foundation Preset"
|
|
|
|
self._attr_unique_id = f"{bed.id}_preset"
|
|
|
|
if preset.side != Side.NONE:
|
|
|
|
self._attr_name += f" {preset.side_full}"
|
2023-02-14 16:26:33 +00:00
|
|
|
self._attr_unique_id += f"_{preset.side.value}"
|
2023-04-20 22:01:49 +00:00
|
|
|
self._attr_options = preset.options
|
2022-03-24 02:14:39 +00:00
|
|
|
|
|
|
|
super().__init__(coordinator, bed)
|
|
|
|
self._async_update_attrs()
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_update_attrs(self) -> None:
|
|
|
|
"""Update entity attributes."""
|
|
|
|
self._attr_current_option = self.preset.preset
|
|
|
|
|
|
|
|
async def async_select_option(self, option: str) -> None:
|
|
|
|
"""Change the current preset."""
|
2022-03-25 20:47:28 +00:00
|
|
|
await self.preset.set_preset(option)
|
2022-03-24 02:14:39 +00:00
|
|
|
self._attr_current_option = option
|
|
|
|
self.async_write_ha_state()
|
2023-12-26 22:31:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SleepIQFootWarmingTempSelectEntity(
|
|
|
|
SleepIQSleeperEntity[SleepIQDataUpdateCoordinator], SelectEntity
|
|
|
|
):
|
|
|
|
"""Representation of a SleepIQ foot warming temperature select entity."""
|
|
|
|
|
|
|
|
_attr_icon = "mdi:heat-wave"
|
|
|
|
_attr_options = [e.name.lower() for e in FootWarmingTemps]
|
|
|
|
_attr_translation_key = "foot_warmer_temp"
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: SleepIQDataUpdateCoordinator,
|
|
|
|
bed: SleepIQBed,
|
|
|
|
foot_warmer: SleepIQFootWarmer,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the select entity."""
|
|
|
|
self.foot_warmer = foot_warmer
|
|
|
|
sleeper = sleeper_for_side(bed, foot_warmer.side)
|
|
|
|
super().__init__(coordinator, bed, sleeper, FOOT_WARMER)
|
|
|
|
self._async_update_attrs()
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_update_attrs(self) -> None:
|
|
|
|
"""Update entity attributes."""
|
|
|
|
self._attr_current_option = FootWarmingTemps(
|
|
|
|
self.foot_warmer.temperature
|
|
|
|
).name.lower()
|
|
|
|
|
|
|
|
async def async_select_option(self, option: str) -> None:
|
|
|
|
"""Change the current preset."""
|
|
|
|
temperature = FootWarmingTemps[option.upper()]
|
|
|
|
timer = self.foot_warmer.timer or 120
|
|
|
|
|
|
|
|
if temperature == 0:
|
|
|
|
await self.foot_warmer.turn_off()
|
|
|
|
else:
|
|
|
|
await self.foot_warmer.turn_on(temperature, timer)
|
|
|
|
|
|
|
|
self._attr_current_option = option
|
|
|
|
await self.coordinator.async_request_refresh()
|
|
|
|
self.async_write_ha_state()
|