core/homeassistant/components/owlet/binary_sensor.py

78 lines
2.2 KiB
Python
Raw Normal View History

2019-02-16 09:12:16 +00:00
"""Support for Owlet binary sensors."""
from datetime import timedelta
from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.util import dt as dt_util
from . import DOMAIN as OWLET_DOMAIN
2019-02-16 09:12:16 +00:00
from .const import SENSOR_BASE_STATION, SENSOR_MOVEMENT
SCAN_INTERVAL = timedelta(seconds=120)
BINARY_CONDITIONS = {
2019-07-31 19:25:30 +00:00
SENSOR_BASE_STATION: {"name": "Base Station", "device_class": "power"},
SENSOR_MOVEMENT: {"name": "Movement", "device_class": "motion"},
2019-02-16 09:12:16 +00:00
}
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up owlet binary sensor."""
if discovery_info is None:
return
device = hass.data[OWLET_DOMAIN]
entities = []
for condition in BINARY_CONDITIONS:
if condition in device.monitor:
entities.append(OwletBinarySensor(device, condition))
add_entities(entities, True)
class OwletBinarySensor(BinarySensorDevice):
"""Representation of owlet binary sensor."""
def __init__(self, device, condition):
"""Init owlet binary sensor."""
self._device = device
self._condition = condition
self._state = None
self._base_on = False
self._prop_expiration = None
self._is_charging = None
@property
def name(self):
"""Return sensor name."""
2019-07-31 19:25:30 +00:00
return "{} {}".format(
self._device.name, BINARY_CONDITIONS[self._condition]["name"]
)
2019-02-16 09:12:16 +00:00
@property
def is_on(self):
"""Return current state of sensor."""
return self._state
@property
def device_class(self):
"""Return the device class."""
2019-07-31 19:25:30 +00:00
return BINARY_CONDITIONS[self._condition]["device_class"]
2019-02-16 09:12:16 +00:00
def update(self):
"""Update state of sensor."""
self._base_on = self._device.device.base_station_on
self._prop_expiration = self._device.device.prop_expire_time
self._is_charging = self._device.device.charge_status > 0
# handle expired values
if self._prop_expiration < dt_util.now().timestamp():
self._state = False
return
2019-07-31 19:25:30 +00:00
if self._condition == "movement":
2019-02-16 09:12:16 +00:00
if not self._base_on or self._is_charging:
return False
self._state = getattr(self._device.device, self._condition)