2020-10-12 17:31:55 +00:00
|
|
|
"""Support for SimpliSafe binary sensors."""
|
|
|
|
from simplipy.entity import EntityTypes
|
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
DEVICE_CLASS_BATTERY,
|
|
|
|
DEVICE_CLASS_DOOR,
|
|
|
|
DEVICE_CLASS_GAS,
|
|
|
|
DEVICE_CLASS_MOISTURE,
|
2020-11-05 01:59:54 +00:00
|
|
|
DEVICE_CLASS_MOTION,
|
|
|
|
DEVICE_CLASS_SAFETY,
|
2020-10-12 17:31:55 +00:00
|
|
|
DEVICE_CLASS_SMOKE,
|
|
|
|
BinarySensorEntity,
|
|
|
|
)
|
|
|
|
from homeassistant.core import callback
|
|
|
|
|
2020-11-04 19:34:14 +00:00
|
|
|
from . import SimpliSafeBaseSensor
|
2020-10-22 16:01:10 +00:00
|
|
|
from .const import DATA_CLIENT, DOMAIN, LOGGER
|
2020-10-12 17:31:55 +00:00
|
|
|
|
|
|
|
SUPPORTED_BATTERY_SENSOR_TYPES = [
|
|
|
|
EntityTypes.carbon_monoxide,
|
2020-10-17 19:41:01 +00:00
|
|
|
EntityTypes.entry,
|
2020-11-09 22:18:18 +00:00
|
|
|
EntityTypes.glass_break,
|
2020-10-12 17:31:55 +00:00
|
|
|
EntityTypes.leak,
|
2020-11-05 01:59:54 +00:00
|
|
|
EntityTypes.lock_keypad,
|
2020-11-09 22:18:18 +00:00
|
|
|
EntityTypes.motion,
|
|
|
|
EntityTypes.siren,
|
2020-10-17 19:41:01 +00:00
|
|
|
EntityTypes.smoke,
|
2020-10-12 17:31:55 +00:00
|
|
|
EntityTypes.temperature,
|
|
|
|
]
|
|
|
|
|
2020-11-04 19:34:14 +00:00
|
|
|
TRIGGERED_SENSOR_TYPES = {
|
2020-10-12 17:31:55 +00:00
|
|
|
EntityTypes.carbon_monoxide: DEVICE_CLASS_GAS,
|
2020-11-04 18:41:08 +00:00
|
|
|
EntityTypes.entry: DEVICE_CLASS_DOOR,
|
2020-11-05 01:59:54 +00:00
|
|
|
EntityTypes.glass_break: DEVICE_CLASS_SAFETY,
|
2020-10-12 17:31:55 +00:00
|
|
|
EntityTypes.leak: DEVICE_CLASS_MOISTURE,
|
2020-11-05 01:59:54 +00:00
|
|
|
EntityTypes.motion: DEVICE_CLASS_MOTION,
|
2020-11-09 22:18:18 +00:00
|
|
|
EntityTypes.siren: DEVICE_CLASS_SAFETY,
|
2020-11-04 18:41:08 +00:00
|
|
|
EntityTypes.smoke: DEVICE_CLASS_SMOKE,
|
2020-10-12 17:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, entry, async_add_entities):
|
|
|
|
"""Set up SimpliSafe binary sensors based on a config entry."""
|
|
|
|
simplisafe = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id]
|
2020-10-16 17:01:16 +00:00
|
|
|
sensors = []
|
2020-10-22 16:01:10 +00:00
|
|
|
|
2020-10-16 17:01:16 +00:00
|
|
|
for system in simplisafe.systems.values():
|
2020-10-22 16:01:10 +00:00
|
|
|
if system.version == 2:
|
|
|
|
LOGGER.info("Skipping sensor setup for V2 system: %s", system.system_id)
|
|
|
|
continue
|
|
|
|
|
2020-10-16 17:01:16 +00:00
|
|
|
for sensor in system.sensors.values():
|
2020-11-04 19:34:14 +00:00
|
|
|
if sensor.type in TRIGGERED_SENSOR_TYPES:
|
|
|
|
sensors.append(
|
|
|
|
TriggeredBinarySensor(
|
|
|
|
simplisafe,
|
|
|
|
system,
|
|
|
|
sensor,
|
|
|
|
TRIGGERED_SENSOR_TYPES[sensor.type],
|
|
|
|
)
|
|
|
|
)
|
2020-10-16 17:01:16 +00:00
|
|
|
if sensor.type in SUPPORTED_BATTERY_SENSOR_TYPES:
|
2020-11-04 18:41:08 +00:00
|
|
|
sensors.append(BatteryBinarySensor(simplisafe, system, sensor))
|
2020-10-12 17:31:55 +00:00
|
|
|
|
2020-10-16 17:01:16 +00:00
|
|
|
async_add_entities(sensors)
|
2020-10-12 17:31:55 +00:00
|
|
|
|
|
|
|
|
2020-11-04 19:34:14 +00:00
|
|
|
class TriggeredBinarySensor(SimpliSafeBaseSensor, BinarySensorEntity):
|
2020-11-04 18:41:08 +00:00
|
|
|
"""Define a binary sensor related to whether an entity has been triggered."""
|
|
|
|
|
2020-11-04 19:34:14 +00:00
|
|
|
def __init__(self, simplisafe, system, sensor, device_class):
|
2020-11-04 18:41:08 +00:00
|
|
|
"""Initialize."""
|
|
|
|
super().__init__(simplisafe, system, sensor)
|
2020-11-04 19:34:14 +00:00
|
|
|
self._device_class = device_class
|
2020-10-12 17:31:55 +00:00
|
|
|
self._is_on = False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return type of sensor."""
|
2020-11-04 19:34:14 +00:00
|
|
|
return self._device_class
|
2020-10-12 17:31:55 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if the sensor is on."""
|
|
|
|
return self._is_on
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_update_from_rest_api(self):
|
|
|
|
"""Update the entity with the provided REST API data."""
|
|
|
|
self._is_on = self._sensor.triggered
|
|
|
|
|
|
|
|
|
2020-11-04 19:34:14 +00:00
|
|
|
class BatteryBinarySensor(SimpliSafeBaseSensor, BinarySensorEntity):
|
2020-10-12 17:31:55 +00:00
|
|
|
"""Define a SimpliSafe battery binary sensor entity."""
|
|
|
|
|
|
|
|
def __init__(self, simplisafe, system, sensor):
|
|
|
|
"""Initialize."""
|
2020-11-04 18:41:08 +00:00
|
|
|
super().__init__(simplisafe, system, sensor)
|
2020-10-12 17:31:55 +00:00
|
|
|
self._is_low = False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return type of sensor."""
|
|
|
|
return DEVICE_CLASS_BATTERY
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return unique ID of sensor."""
|
|
|
|
return f"{self._sensor.serial}-battery"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if the battery is low."""
|
|
|
|
return self._is_low
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_update_from_rest_api(self):
|
|
|
|
"""Update the entity with the provided REST API data."""
|
|
|
|
self._is_low = self._sensor.low_battery
|