2020-10-12 17:31:55 +00:00
|
|
|
"""Support for SimpliSafe binary sensors."""
|
2021-07-27 20:11:54 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-10-19 20:09:48 +00:00
|
|
|
from simplipy.device import DeviceTypes
|
|
|
|
from simplipy.device.sensor.v2 import SensorV2
|
|
|
|
from simplipy.device.sensor.v3 import SensorV3
|
2021-07-27 20:11:54 +00:00
|
|
|
from simplipy.system.v2 import SystemV2
|
|
|
|
from simplipy.system.v3 import SystemV3
|
2020-10-12 17:31:55 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
2021-07-27 20:11:54 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-10-12 17:31:55 +00:00
|
|
|
|
2021-07-27 20:11:54 +00:00
|
|
|
from . import SimpliSafe, 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 = [
|
2021-10-19 20:09:48 +00:00
|
|
|
DeviceTypes.carbon_monoxide,
|
|
|
|
DeviceTypes.entry,
|
|
|
|
DeviceTypes.glass_break,
|
|
|
|
DeviceTypes.leak,
|
|
|
|
DeviceTypes.lock_keypad,
|
|
|
|
DeviceTypes.motion,
|
|
|
|
DeviceTypes.siren,
|
|
|
|
DeviceTypes.smoke,
|
|
|
|
DeviceTypes.temperature,
|
2020-10-12 17:31:55 +00:00
|
|
|
]
|
|
|
|
|
2020-11-04 19:34:14 +00:00
|
|
|
TRIGGERED_SENSOR_TYPES = {
|
2021-10-19 20:09:48 +00:00
|
|
|
DeviceTypes.carbon_monoxide: DEVICE_CLASS_GAS,
|
|
|
|
DeviceTypes.entry: DEVICE_CLASS_DOOR,
|
|
|
|
DeviceTypes.glass_break: DEVICE_CLASS_SAFETY,
|
|
|
|
DeviceTypes.leak: DEVICE_CLASS_MOISTURE,
|
|
|
|
DeviceTypes.motion: DEVICE_CLASS_MOTION,
|
|
|
|
DeviceTypes.siren: DEVICE_CLASS_SAFETY,
|
|
|
|
DeviceTypes.smoke: DEVICE_CLASS_SMOKE,
|
2020-10-12 17:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-27 20:11:54 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2020-10-12 17:31:55 +00:00
|
|
|
"""Set up SimpliSafe binary sensors based on a config entry."""
|
2021-10-19 20:09:48 +00:00
|
|
|
simplisafe = hass.data[DOMAIN][entry.entry_id][DATA_CLIENT]
|
2021-07-27 20:11:54 +00:00
|
|
|
|
|
|
|
sensors: list[BatteryBinarySensor | TriggeredBinarySensor] = []
|
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."""
|
|
|
|
|
2021-07-27 20:11:54 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
simplisafe: SimpliSafe,
|
|
|
|
system: SystemV2 | SystemV3,
|
2021-10-19 20:09:48 +00:00
|
|
|
sensor: SensorV2 | SensorV3,
|
2021-07-27 20:11:54 +00:00
|
|
|
device_class: str,
|
|
|
|
) -> None:
|
2020-11-04 18:41:08 +00:00
|
|
|
"""Initialize."""
|
|
|
|
super().__init__(simplisafe, system, sensor)
|
2020-10-12 17:31:55 +00:00
|
|
|
|
2021-07-03 16:16:55 +00:00
|
|
|
self._attr_device_class = device_class
|
2020-10-12 17:31:55 +00:00
|
|
|
|
|
|
|
@callback
|
2021-07-27 20:11:54 +00:00
|
|
|
def async_update_from_rest_api(self) -> None:
|
2020-10-12 17:31:55 +00:00
|
|
|
"""Update the entity with the provided REST API data."""
|
2021-07-03 16:16:55 +00:00
|
|
|
self._attr_is_on = self._sensor.triggered
|
2020-10-12 17:31:55 +00:00
|
|
|
|
|
|
|
|
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."""
|
|
|
|
|
2021-07-03 16:16:55 +00:00
|
|
|
_attr_device_class = DEVICE_CLASS_BATTERY
|
|
|
|
|
2021-07-27 20:11:54 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
simplisafe: SimpliSafe,
|
|
|
|
system: SystemV2 | SystemV3,
|
2021-10-19 20:09:48 +00:00
|
|
|
sensor: SensorV2 | SensorV3,
|
2021-07-27 20:11:54 +00:00
|
|
|
) -> None:
|
2020-10-12 17:31:55 +00:00
|
|
|
"""Initialize."""
|
2020-11-04 18:41:08 +00:00
|
|
|
super().__init__(simplisafe, system, sensor)
|
2020-10-12 17:31:55 +00:00
|
|
|
|
2021-07-03 16:16:55 +00:00
|
|
|
self._attr_unique_id = f"{super().unique_id}-battery"
|
2020-10-12 17:31:55 +00:00
|
|
|
|
|
|
|
@callback
|
2021-07-27 20:11:54 +00:00
|
|
|
def async_update_from_rest_api(self) -> None:
|
2020-10-12 17:31:55 +00:00
|
|
|
"""Update the entity with the provided REST API data."""
|
2021-07-03 16:16:55 +00:00
|
|
|
self._attr_is_on = self._sensor.low_battery
|