Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Vanderbilt (formerly Siemens) SPC alarm systems."""
2 
3 from __future__ import annotations
4 
5 from pyspcwebgw import SpcWebGateway
6 from pyspcwebgw.const import ZoneInput, ZoneType
7 from pyspcwebgw.zone import Zone
8 
10  BinarySensorDeviceClass,
11  BinarySensorEntity,
12 )
13 from homeassistant.core import HomeAssistant, callback
14 from homeassistant.helpers.dispatcher import async_dispatcher_connect
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
17 
18 from . import DATA_API, SIGNAL_UPDATE_SENSOR
19 
20 
21 def _get_device_class(zone_type: ZoneType) -> BinarySensorDeviceClass | None:
22  return {
23  ZoneType.ALARM: BinarySensorDeviceClass.MOTION,
24  ZoneType.ENTRY_EXIT: BinarySensorDeviceClass.OPENING,
25  ZoneType.FIRE: BinarySensorDeviceClass.SMOKE,
26  ZoneType.TECHNICAL: BinarySensorDeviceClass.POWER,
27  }.get(zone_type)
28 
29 
31  hass: HomeAssistant,
32  config: ConfigType,
33  async_add_entities: AddEntitiesCallback,
34  discovery_info: DiscoveryInfoType | None = None,
35 ) -> None:
36  """Set up the SPC binary sensor."""
37  if discovery_info is None:
38  return
39  api: SpcWebGateway = hass.data[DATA_API]
41  [
42  SpcBinarySensor(zone)
43  for zone in api.zones.values()
44  if _get_device_class(zone.type)
45  ]
46  )
47 
48 
50  """Representation of a sensor based on a SPC zone."""
51 
52  _attr_should_poll = False
53 
54  def __init__(self, zone: Zone) -> None:
55  """Initialize the sensor device."""
56  self._zone_zone = zone
57  self._attr_name_attr_name = zone.name
58  self._attr_device_class_attr_device_class = _get_device_class(zone.type)
59 
60  async def async_added_to_hass(self) -> None:
61  """Call for adding new entities."""
62  self.async_on_removeasync_on_remove(
64  self.hasshass,
65  SIGNAL_UPDATE_SENSOR.format(self._zone_zone.id),
66  self._update_callback_update_callback,
67  )
68  )
69 
70  @callback
71  def _update_callback(self) -> None:
72  """Call update method."""
73  self.async_schedule_update_ha_stateasync_schedule_update_ha_state(True)
74 
75  @property
76  def is_on(self) -> bool:
77  """Whether the device is switched on."""
78  return self._zone_zone.input == ZoneInput.OPEN
None async_schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1265
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
BinarySensorDeviceClass|None _get_device_class(ZoneType zone_type)
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103