2019-04-03 15:40:03 +00:00
|
|
|
"""Support for SleepIQ sensors."""
|
2020-09-12 16:07:13 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2021-12-19 12:15:09 +00:00
|
|
|
BinarySensorDeviceClass,
|
2020-09-12 16:07:13 +00:00
|
|
|
BinarySensorEntity,
|
|
|
|
)
|
2022-02-16 14:51:29 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2022-01-03 12:13:03 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2016-09-13 22:11:50 +00:00
|
|
|
|
2022-02-16 14:51:29 +00:00
|
|
|
from .const import BED, DOMAIN, ICON_EMPTY, ICON_OCCUPIED, IS_IN_BED, SIDES
|
|
|
|
from .coordinator import SleepIQDataUpdateCoordinator
|
|
|
|
from .entity import SleepIQSensor
|
2020-04-05 22:46:50 +00:00
|
|
|
|
2016-09-13 22:11:50 +00:00
|
|
|
|
2022-02-16 14:51:29 +00:00
|
|
|
async def async_setup_entry(
|
2022-01-03 12:13:03 +00:00
|
|
|
hass: HomeAssistant,
|
2022-02-16 14:51:29 +00:00
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
2022-01-03 12:13:03 +00:00
|
|
|
) -> None:
|
2022-02-16 14:51:29 +00:00
|
|
|
"""Set up the SleepIQ bed binary sensors."""
|
|
|
|
coordinator: SleepIQDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
async_add_entities(
|
|
|
|
IsInBedBinarySensor(coordinator, bed_id, side)
|
|
|
|
for side in SIDES
|
|
|
|
for bed_id in coordinator.data
|
|
|
|
if getattr(coordinator.data[bed_id][BED], side) is not None
|
|
|
|
)
|
2016-09-13 22:11:50 +00:00
|
|
|
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
class IsInBedBinarySensor(SleepIQSensor, BinarySensorEntity):
|
2016-09-13 22:11:50 +00:00
|
|
|
"""Implementation of a SleepIQ presence sensor."""
|
|
|
|
|
2022-02-16 14:51:29 +00:00
|
|
|
_attr_device_class = BinarySensorDeviceClass.OCCUPANCY
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: SleepIQDataUpdateCoordinator,
|
|
|
|
bed_id: str,
|
|
|
|
side: str,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the SleepIQ bed side binary sensor."""
|
|
|
|
super().__init__(coordinator, bed_id, side, IS_IN_BED)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_update_attrs(self) -> None:
|
|
|
|
"""Update sensor attributes."""
|
|
|
|
super()._async_update_attrs()
|
|
|
|
self._attr_is_on = getattr(self.side_data, IS_IN_BED)
|
|
|
|
self._attr_icon = ICON_OCCUPIED if self.is_on else ICON_EMPTY
|