Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for SimpliSafe binary sensors."""
2 
3 from __future__ import annotations
4 
5 from simplipy.device import DeviceTypes, DeviceV3
6 from simplipy.device.sensor.v3 import SensorV3
7 from simplipy.system.v3 import SystemV3
8 
10  BinarySensorDeviceClass,
11  BinarySensorEntity,
12 )
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.const import EntityCategory
15 from homeassistant.core import HomeAssistant, callback
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from . import SimpliSafe
19 from .const import DOMAIN, LOGGER
20 from .entity import SimpliSafeEntity
21 
22 SUPPORTED_BATTERY_SENSOR_TYPES = [
23  DeviceTypes.CARBON_MONOXIDE,
24  DeviceTypes.DOORBELL,
25  DeviceTypes.ENTRY,
26  DeviceTypes.GLASS_BREAK,
27  DeviceTypes.KEYCHAIN,
28  DeviceTypes.KEYPAD,
29  DeviceTypes.LEAK,
30  DeviceTypes.LOCK,
31  DeviceTypes.LOCK_KEYPAD,
32  DeviceTypes.MOTION,
33  DeviceTypes.MOTION_V2,
34  DeviceTypes.PANIC_BUTTON,
35  DeviceTypes.REMOTE,
36  DeviceTypes.SIREN,
37  DeviceTypes.SMOKE,
38  DeviceTypes.SMOKE_AND_CARBON_MONOXIDE,
39  DeviceTypes.TEMPERATURE,
40 ]
41 
42 TRIGGERED_SENSOR_TYPES = {
43  DeviceTypes.CARBON_MONOXIDE: BinarySensorDeviceClass.GAS,
44  DeviceTypes.ENTRY: BinarySensorDeviceClass.DOOR,
45  DeviceTypes.GLASS_BREAK: BinarySensorDeviceClass.SAFETY,
46  DeviceTypes.LEAK: BinarySensorDeviceClass.MOISTURE,
47  DeviceTypes.MOTION: BinarySensorDeviceClass.MOTION,
48  DeviceTypes.MOTION_V2: BinarySensorDeviceClass.MOTION,
49  DeviceTypes.SIREN: BinarySensorDeviceClass.SAFETY,
50  DeviceTypes.SMOKE: BinarySensorDeviceClass.SMOKE,
51  # Although this sensor can technically apply to both smoke and carbon, we use the
52  # SMOKE device class for simplicity:
53  DeviceTypes.SMOKE_AND_CARBON_MONOXIDE: BinarySensorDeviceClass.SMOKE,
54 }
55 
56 
58  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
59 ) -> None:
60  """Set up SimpliSafe binary sensors based on a config entry."""
61  simplisafe = hass.data[DOMAIN][entry.entry_id]
62 
63  sensors: list[BatteryBinarySensor | TriggeredBinarySensor] = []
64 
65  for system in simplisafe.systems.values():
66  if system.version == 2:
67  LOGGER.warning("Skipping sensor setup for V2 system: %s", system.system_id)
68  continue
69 
70  for sensor in system.sensors.values():
71  if sensor.type in TRIGGERED_SENSOR_TYPES:
72  sensors.append(
74  simplisafe,
75  system,
76  sensor,
77  TRIGGERED_SENSOR_TYPES[sensor.type],
78  )
79  )
80  if sensor.type in SUPPORTED_BATTERY_SENSOR_TYPES:
81  sensors.append(BatteryBinarySensor(simplisafe, system, sensor))
82 
83  sensors.extend(
84  BatteryBinarySensor(simplisafe, system, lock)
85  for lock in system.locks.values()
86  )
87 
88  async_add_entities(sensors)
89 
90 
92  """Define a binary sensor related to whether an entity has been triggered."""
93 
94  def __init__(
95  self,
96  simplisafe: SimpliSafe,
97  system: SystemV3,
98  sensor: SensorV3,
99  device_class: BinarySensorDeviceClass,
100  ) -> None:
101  """Initialize."""
102  super().__init__(simplisafe, system, device=sensor)
103 
104  self._attr_device_class_attr_device_class = device_class
105  self._device_device: SensorV3
106 
107  @callback
108  def async_update_from_rest_api(self) -> None:
109  """Update the entity with the provided REST API data."""
110  self._attr_is_on_attr_is_on = self._device_device.triggered
111 
112 
114  """Define a SimpliSafe battery binary sensor entity."""
115 
116  _attr_device_class = BinarySensorDeviceClass.BATTERY
117  _attr_entity_category = EntityCategory.DIAGNOSTIC
118 
119  def __init__(
120  self, simplisafe: SimpliSafe, system: SystemV3, device: DeviceV3
121  ) -> None:
122  """Initialize."""
123  super().__init__(simplisafe, system, device=device)
124 
125  self._attr_unique_id_attr_unique_id_attr_unique_id = f"{super().unique_id}-battery"
126  self._device_device: DeviceV3
127 
128  @callback
129  def async_update_from_rest_api(self) -> None:
130  """Update the entity with the provided REST API data."""
131  self._attr_is_on_attr_is_on = self._device_device.low_battery
None __init__(self, SimpliSafe simplisafe, SystemV3 system, DeviceV3 device)
None __init__(self, SimpliSafe simplisafe, SystemV3 system, SensorV3 sensor, BinarySensorDeviceClass device_class)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)