Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Risco alarm zones."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from itertools import chain
7 from typing import Any
8 
9 from pyrisco.cloud.zone import Zone as CloudZone
10 from pyrisco.common import System
11 from pyrisco.local.zone import Zone as LocalZone
12 
14  BinarySensorDeviceClass,
15  BinarySensorEntity,
16  BinarySensorEntityDescription,
17 )
18 from homeassistant.config_entries import ConfigEntry
19 from homeassistant.core import HomeAssistant
20 from homeassistant.helpers.device_registry import DeviceInfo
21 from homeassistant.helpers.dispatcher import async_dispatcher_connect
22 from homeassistant.helpers.entity_platform import AddEntitiesCallback
23 
24 from . import LocalData, is_local
25 from .const import DATA_COORDINATOR, DOMAIN, SYSTEM_UPDATE_SIGNAL
26 from .coordinator import RiscoDataUpdateCoordinator
27 from .entity import RiscoCloudZoneEntity, RiscoLocalZoneEntity
28 
29 SYSTEM_ENTITY_DESCRIPTIONS = [
31  key="low_battery_trouble",
32  translation_key="low_battery_trouble",
33  device_class=BinarySensorDeviceClass.BATTERY,
34  ),
36  key="ac_trouble",
37  translation_key="ac_trouble",
38  device_class=BinarySensorDeviceClass.PROBLEM,
39  ),
41  key="monitoring_station_1_trouble",
42  translation_key="monitoring_station_1_trouble",
43  device_class=BinarySensorDeviceClass.PROBLEM,
44  ),
46  key="monitoring_station_2_trouble",
47  translation_key="monitoring_station_2_trouble",
48  device_class=BinarySensorDeviceClass.PROBLEM,
49  ),
51  key="monitoring_station_3_trouble",
52  translation_key="monitoring_station_3_trouble",
53  device_class=BinarySensorDeviceClass.PROBLEM,
54  ),
56  key="phone_line_trouble",
57  translation_key="phone_line_trouble",
58  device_class=BinarySensorDeviceClass.PROBLEM,
59  ),
61  key="clock_trouble",
62  translation_key="clock_trouble",
63  device_class=BinarySensorDeviceClass.PROBLEM,
64  ),
66  key="box_tamper",
67  translation_key="box_tamper",
68  device_class=BinarySensorDeviceClass.TAMPER,
69  ),
70 ]
71 
72 
74  hass: HomeAssistant,
75  config_entry: ConfigEntry,
76  async_add_entities: AddEntitiesCallback,
77 ) -> None:
78  """Set up the Risco alarm control panel."""
79  if is_local(config_entry):
80  local_data: LocalData = hass.data[DOMAIN][config_entry.entry_id]
81  zone_entities = (
82  entity
83  for zone_id, zone in local_data.system.zones.items()
84  for entity in (
85  RiscoLocalBinarySensor(local_data.system.id, zone_id, zone),
86  RiscoLocalAlarmedBinarySensor(local_data.system.id, zone_id, zone),
87  RiscoLocalArmedBinarySensor(local_data.system.id, zone_id, zone),
88  )
89  )
90 
91  system_entities = (
93  local_data.system.id, local_data.system.system, entity_description
94  )
95  for entity_description in SYSTEM_ENTITY_DESCRIPTIONS
96  )
97 
98  async_add_entities(chain(system_entities, zone_entities))
99  else:
100  coordinator: RiscoDataUpdateCoordinator = hass.data[DOMAIN][
101  config_entry.entry_id
102  ][DATA_COORDINATOR]
104  RiscoCloudBinarySensor(coordinator, zone_id, zone)
105  for zone_id, zone in coordinator.data.zones.items()
106  )
107 
108 
110  """Representation of a Risco cloud zone as a binary sensor."""
111 
112  _attr_device_class = BinarySensorDeviceClass.MOTION
113  _attr_name = None
114 
115  def __init__(
116  self, coordinator: RiscoDataUpdateCoordinator, zone_id: int, zone: CloudZone
117  ) -> None:
118  """Init the zone."""
119  super().__init__(coordinator=coordinator, suffix="", zone_id=zone_id, zone=zone)
120 
121  @property
122  def is_on(self) -> bool | None:
123  """Return true if sensor is on."""
124  return self._zone_zone.triggered
125 
126 
128  """Representation of a Risco local zone as a binary sensor."""
129 
130  _attr_device_class = BinarySensorDeviceClass.MOTION
131  _attr_name = None
132 
133  def __init__(self, system_id: str, zone_id: int, zone: LocalZone) -> None:
134  """Init the zone."""
135  super().__init__(system_id=system_id, suffix="", zone_id=zone_id, zone=zone)
136 
137  @property
138  def extra_state_attributes(self) -> Mapping[str, Any] | None:
139  """Return the state attributes."""
140  return {
141  **(super().extra_state_attributes or {}),
142  "groups": self._zone_zone.groups,
143  }
144 
145  @property
146  def is_on(self) -> bool | None:
147  """Return true if sensor is on."""
148  return self._zone_zone.triggered
149 
150 
152  """Representation whether a zone in Risco local is currently triggering an alarm."""
153 
154  _attr_translation_key = "alarmed"
155 
156  def __init__(self, system_id: str, zone_id: int, zone: LocalZone) -> None:
157  """Init the zone."""
158  super().__init__(
159  system_id=system_id,
160  suffix="_alarmed",
161  zone_id=zone_id,
162  zone=zone,
163  )
164 
165  @property
166  def is_on(self) -> bool | None:
167  """Return true if sensor is on."""
168  return self._zone_zone.alarmed
169 
170 
172  """Representation whether a zone in Risco local is currently armed."""
173 
174  _attr_translation_key = "armed"
175 
176  def __init__(self, system_id: str, zone_id: int, zone: LocalZone) -> None:
177  """Init the zone."""
178  super().__init__(
179  system_id=system_id,
180  suffix="_armed",
181  zone_id=zone_id,
182  zone=zone,
183  )
184 
185  @property
186  def is_on(self) -> bool | None:
187  """Return true if sensor is on."""
188  return self._zone_zone.armed
189 
190 
192  """Risco local system binary sensor class."""
193 
194  _attr_should_poll = False
195  _attr_has_entity_name = True
196 
197  def __init__(
198  self,
199  system_id: str,
200  system: System,
201  entity_description: BinarySensorEntityDescription,
202  ) -> None:
203  """Init the sensor."""
204  self._system_system = system
205  self._property_property = entity_description.key
206  self._attr_unique_id_attr_unique_id = f"{system_id}_{self._property}"
207  self._attr_device_info_attr_device_info = DeviceInfo(
208  identifiers={(DOMAIN, system_id)},
209  manufacturer="Risco",
210  name=system.name,
211  )
212  self.entity_descriptionentity_description = entity_description
213 
214  async def async_added_to_hass(self) -> None:
215  """Subscribe to updates."""
216  self.async_on_removeasync_on_remove(
218  self.hasshass, SYSTEM_UPDATE_SIGNAL, self.async_write_ha_stateasync_write_ha_state
219  )
220  )
221 
222  @property
223  def is_on(self) -> bool | None:
224  """Return true if sensor is on."""
225  return getattr(self._system_system, self._property_property)
None __init__(self, RiscoDataUpdateCoordinator coordinator, int zone_id, CloudZone zone)
None __init__(self, str system_id, int zone_id, LocalZone zone)
None __init__(self, str system_id, int zone_id, LocalZone zone)
None __init__(self, str system_id, int zone_id, LocalZone zone)
None __init__(self, str system_id, System system, BinarySensorEntityDescription entity_description)
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
bool is_local(ConfigEntry entry)
Definition: __init__.py:58
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103