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 (
|
|
|
|
DEVICE_CLASS_OCCUPANCY,
|
|
|
|
BinarySensorEntity,
|
|
|
|
)
|
2016-09-13 22:11:50 +00:00
|
|
|
|
2020-04-05 22:46:50 +00:00
|
|
|
from . import SleepIQSensor
|
|
|
|
from .const import DOMAIN, IS_IN_BED, SENSOR_TYPES, SIDES
|
|
|
|
|
2016-09-13 22:11:50 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the SleepIQ sensors."""
|
2016-09-13 22:11:50 +00:00
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
2020-04-05 22:46:50 +00:00
|
|
|
data = hass.data[DOMAIN]
|
2016-09-13 22:11:50 +00:00
|
|
|
data.update()
|
|
|
|
|
2020-04-04 21:14:47 +00:00
|
|
|
dev = []
|
2019-07-07 06:40:02 +00:00
|
|
|
for bed_id, bed in data.beds.items():
|
2020-04-05 22:46:50 +00:00
|
|
|
for side in SIDES:
|
2019-07-07 06:40:02 +00:00
|
|
|
if getattr(bed, side) is not None:
|
|
|
|
dev.append(IsInBedBinarySensor(data, bed_id, side))
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(dev)
|
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."""
|
|
|
|
|
|
|
|
def __init__(self, sleepiq_data, bed_id, side):
|
|
|
|
"""Initialize the sensor."""
|
2020-04-06 17:32:04 +00:00
|
|
|
super().__init__(sleepiq_data, bed_id, side)
|
2016-09-13 22:11:50 +00:00
|
|
|
self._state = None
|
2020-04-06 17:32:04 +00:00
|
|
|
self._name = SENSOR_TYPES[IS_IN_BED]
|
2016-09-13 22:11:50 +00:00
|
|
|
self.update()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return the status of the sensor."""
|
|
|
|
return self._state is True
|
|
|
|
|
|
|
|
@property
|
2017-02-11 04:46:15 +00:00
|
|
|
def device_class(self):
|
2016-09-13 22:11:50 +00:00
|
|
|
"""Return the class of this sensor."""
|
2020-09-12 16:07:13 +00:00
|
|
|
return DEVICE_CLASS_OCCUPANCY
|
2016-09-13 22:11:50 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data from SleepIQ and updates the states."""
|
2020-04-06 17:32:04 +00:00
|
|
|
super().update()
|
2016-09-13 22:11:50 +00:00
|
|
|
self._state = self.side.is_in_bed
|