"""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, DEVICE_CLASS_SMOKE, BinarySensorEntity, ) from homeassistant.core import callback from . import SimpliSafeEntity from .const import DATA_CLIENT, DOMAIN, LOGGER SUPPORTED_BATTERY_SENSOR_TYPES = [ EntityTypes.carbon_monoxide, EntityTypes.entry, EntityTypes.leak, EntityTypes.lock, EntityTypes.smoke, EntityTypes.temperature, ] SUPPORTED_TRIGGERED_SENSOR_TYPES = [ EntityTypes.carbon_monoxide, EntityTypes.entry, EntityTypes.leak, EntityTypes.smoke, ] DEVICE_CLASSES = { EntityTypes.carbon_monoxide: DEVICE_CLASS_GAS, EntityTypes.entry: DEVICE_CLASS_DOOR, EntityTypes.leak: DEVICE_CLASS_MOISTURE, EntityTypes.smoke: DEVICE_CLASS_SMOKE, } SENSOR_MODELS = { EntityTypes.carbon_monoxide: "Carbon Monoxide Detector", EntityTypes.entry: "Entry Sensor", EntityTypes.leak: "Water Sensor", EntityTypes.smoke: "Smoke Detector", } 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] sensors = [] for system in simplisafe.systems.values(): if system.version == 2: LOGGER.info("Skipping sensor setup for V2 system: %s", system.system_id) continue for sensor in system.sensors.values(): if sensor.type in SUPPORTED_TRIGGERED_SENSOR_TYPES: sensors.append(TriggeredBinarySensor(simplisafe, system, sensor)) if sensor.type in SUPPORTED_BATTERY_SENSOR_TYPES: sensors.append(BatteryBinarySensor(simplisafe, system, sensor)) async_add_entities(sensors) class SimpliSafeBinarySensor(SimpliSafeEntity, BinarySensorEntity): """Define a SimpliSafe binary sensor entity.""" def __init__(self, simplisafe, system, sensor): """Initialize.""" super().__init__(simplisafe, system, sensor.name, serial=sensor.serial) self._device_info["identifiers"] = {(DOMAIN, sensor.serial)} self._device_info["model"] = SENSOR_MODELS[sensor.type] self._device_info["name"] = sensor.name class TriggeredBinarySensor(SimpliSafeBinarySensor): """Define a binary sensor related to whether an entity has been triggered.""" def __init__(self, simplisafe, system, sensor): """Initialize.""" super().__init__(simplisafe, system, sensor) self._system = system self._sensor = sensor self._is_on = False @property def device_class(self): """Return type of sensor.""" return DEVICE_CLASSES[self._sensor.type] @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 class BatteryBinarySensor(SimpliSafeEntity, BinarySensorEntity): """Define a SimpliSafe battery binary sensor entity.""" def __init__(self, simplisafe, system, sensor): """Initialize.""" super().__init__(simplisafe, system, sensor) self._sensor = sensor 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