2022-02-16 14:51:29 +00:00
|
|
|
"""Entity for the SleepIQ integration."""
|
2022-02-18 18:50:44 +00:00
|
|
|
from abc import abstractmethod
|
|
|
|
|
|
|
|
from asyncsleepiq import SleepIQBed, SleepIQSleeper
|
|
|
|
|
2022-02-16 14:51:29 +00:00
|
|
|
from homeassistant.core import callback
|
2022-02-18 23:42:35 +00:00
|
|
|
from homeassistant.helpers import device_registry
|
2022-02-19 17:54:52 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo, Entity
|
2022-02-18 18:50:44 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
)
|
2022-02-16 14:51:29 +00:00
|
|
|
|
2022-03-12 19:58:03 +00:00
|
|
|
from .const import ENTITY_TYPES, ICON_OCCUPIED
|
2022-02-16 14:51:29 +00:00
|
|
|
|
|
|
|
|
2022-02-22 07:53:04 +00:00
|
|
|
def device_from_bed(bed: SleepIQBed) -> DeviceInfo:
|
|
|
|
"""Create a device given a bed."""
|
|
|
|
return DeviceInfo(
|
|
|
|
connections={(device_registry.CONNECTION_NETWORK_MAC, bed.mac_addr)},
|
|
|
|
manufacturer="SleepNumber",
|
|
|
|
name=bed.name,
|
|
|
|
model=bed.model,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-02-19 17:54:52 +00:00
|
|
|
class SleepIQEntity(Entity):
|
|
|
|
"""Implementation of a SleepIQ entity."""
|
|
|
|
|
|
|
|
def __init__(self, bed: SleepIQBed) -> None:
|
|
|
|
"""Initialize the SleepIQ entity."""
|
|
|
|
self.bed = bed
|
2022-02-22 07:53:04 +00:00
|
|
|
self._attr_device_info = device_from_bed(bed)
|
2022-02-19 17:54:52 +00:00
|
|
|
|
|
|
|
|
2022-03-03 19:42:33 +00:00
|
|
|
class SleepIQBedEntity(CoordinatorEntity):
|
2022-02-16 14:51:29 +00:00
|
|
|
"""Implementation of a SleepIQ sensor."""
|
|
|
|
|
|
|
|
_attr_icon = ICON_OCCUPIED
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2022-02-18 18:50:44 +00:00
|
|
|
coordinator: DataUpdateCoordinator,
|
|
|
|
bed: SleepIQBed,
|
2022-02-16 14:51:29 +00:00
|
|
|
) -> None:
|
2022-02-19 17:54:52 +00:00
|
|
|
"""Initialize the SleepIQ sensor entity."""
|
2022-02-16 14:51:29 +00:00
|
|
|
super().__init__(coordinator)
|
2022-02-19 17:54:52 +00:00
|
|
|
self.bed = bed
|
2022-02-22 07:53:04 +00:00
|
|
|
self._attr_device_info = device_from_bed(bed)
|
2022-02-19 17:54:52 +00:00
|
|
|
self._async_update_attrs()
|
|
|
|
|
2022-02-16 14:51:29 +00:00
|
|
|
@callback
|
|
|
|
def _handle_coordinator_update(self) -> None:
|
|
|
|
"""Handle updated data from the coordinator."""
|
|
|
|
self._async_update_attrs()
|
|
|
|
super()._handle_coordinator_update()
|
|
|
|
|
|
|
|
@callback
|
2022-02-18 18:50:44 +00:00
|
|
|
@abstractmethod
|
2022-02-16 14:51:29 +00:00
|
|
|
def _async_update_attrs(self) -> None:
|
|
|
|
"""Update sensor attributes."""
|
2022-02-22 07:53:04 +00:00
|
|
|
|
|
|
|
|
2022-03-03 19:42:33 +00:00
|
|
|
class SleepIQSleeperEntity(SleepIQBedEntity):
|
2022-02-22 07:53:04 +00:00
|
|
|
"""Implementation of a SleepIQ sensor."""
|
|
|
|
|
|
|
|
_attr_icon = ICON_OCCUPIED
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: DataUpdateCoordinator,
|
|
|
|
bed: SleepIQBed,
|
2022-03-03 19:42:33 +00:00
|
|
|
sleeper: SleepIQSleeper,
|
|
|
|
name: str,
|
2022-02-22 07:53:04 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize the SleepIQ sensor entity."""
|
2022-03-03 19:42:33 +00:00
|
|
|
self.sleeper = sleeper
|
|
|
|
super().__init__(coordinator, bed)
|
|
|
|
|
2022-03-12 19:58:03 +00:00
|
|
|
self._attr_name = f"SleepNumber {bed.name} {sleeper.name} {ENTITY_TYPES[name]}"
|
2022-03-12 22:54:26 +00:00
|
|
|
self._attr_unique_id = f"{sleeper.sleeper_id}_{name}"
|