2022-02-18 18:50:44 +00:00
|
|
|
"""Support for SleepIQ Sensor."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from asyncsleepiq import SleepIQBed, SleepIQSleeper
|
|
|
|
|
2022-08-10 21:59:50 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity, SensorStateClass
|
2022-02-16 14:51:29 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2022-01-03 18:06:08 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2022-02-18 18:50:44 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
2016-09-13 22:11:50 +00:00
|
|
|
|
2022-03-03 20:27:22 +00:00
|
|
|
from .const import DOMAIN, PRESSURE, SLEEP_NUMBER
|
2022-02-22 07:53:04 +00:00
|
|
|
from .coordinator import SleepIQData
|
2022-03-03 19:42:33 +00:00
|
|
|
from .entity import SleepIQSleeperEntity
|
2016-09-13 22:11:50 +00:00
|
|
|
|
2022-03-03 20:27:22 +00:00
|
|
|
SENSORS = [PRESSURE, SLEEP_NUMBER]
|
|
|
|
|
2016-09-13 22:11:50 +00:00
|
|
|
|
2022-02-16 14:51:29 +00:00
|
|
|
async def async_setup_entry(
|
2022-01-03 18:06:08 +00:00
|
|
|
hass: HomeAssistant,
|
2022-02-16 14:51:29 +00:00
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
2022-01-03 18:06:08 +00:00
|
|
|
) -> None:
|
2022-02-16 14:51:29 +00:00
|
|
|
"""Set up the SleepIQ bed sensors."""
|
2022-02-22 07:53:04 +00:00
|
|
|
data: SleepIQData = hass.data[DOMAIN][entry.entry_id]
|
2022-02-16 14:51:29 +00:00
|
|
|
async_add_entities(
|
2022-03-03 20:27:22 +00:00
|
|
|
SleepIQSensorEntity(data.data_coordinator, bed, sleeper, sensor_type)
|
2022-02-22 07:53:04 +00:00
|
|
|
for bed in data.client.beds.values()
|
2022-02-18 18:50:44 +00:00
|
|
|
for sleeper in bed.sleepers
|
2022-03-03 20:27:22 +00:00
|
|
|
for sensor_type in SENSORS
|
2022-02-16 14:51:29 +00:00
|
|
|
)
|
2016-09-13 22:11:50 +00:00
|
|
|
|
|
|
|
|
2022-03-03 20:27:22 +00:00
|
|
|
class SleepIQSensorEntity(SleepIQSleeperEntity, SensorEntity):
|
2022-02-18 18:50:44 +00:00
|
|
|
"""Representation of an SleepIQ Entity with CoordinatorEntity."""
|
|
|
|
|
|
|
|
_attr_icon = "mdi:bed"
|
2016-09-13 22:11:50 +00:00
|
|
|
|
2022-02-16 14:51:29 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2022-02-18 18:50:44 +00:00
|
|
|
coordinator: DataUpdateCoordinator,
|
|
|
|
bed: SleepIQBed,
|
|
|
|
sleeper: SleepIQSleeper,
|
2022-03-03 20:27:22 +00:00
|
|
|
sensor_type: str,
|
2022-02-16 14:51:29 +00:00
|
|
|
) -> None:
|
2022-02-18 18:50:44 +00:00
|
|
|
"""Initialize the sensor."""
|
2022-03-03 20:27:22 +00:00
|
|
|
self.sensor_type = sensor_type
|
2022-08-10 21:59:50 +00:00
|
|
|
self._attr_state_class = SensorStateClass.MEASUREMENT
|
2022-03-03 20:27:22 +00:00
|
|
|
super().__init__(coordinator, bed, sleeper, sensor_type)
|
2022-02-16 14:51:29 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_update_attrs(self) -> None:
|
|
|
|
"""Update sensor attributes."""
|
2022-03-03 20:27:22 +00:00
|
|
|
self._attr_native_value = getattr(self.sleeper, self.sensor_type)
|