Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Ness D8X/D16X zone states - represented as binary sensors."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.components.binary_sensor import BinarySensorEntity
6 from homeassistant.core import HomeAssistant, callback
7 from homeassistant.helpers.dispatcher import async_dispatcher_connect
8 from homeassistant.helpers.entity_platform import AddEntitiesCallback
9 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
10 
11 from . import (
12  CONF_ZONE_ID,
13  CONF_ZONE_NAME,
14  CONF_ZONE_TYPE,
15  CONF_ZONES,
16  SIGNAL_ZONE_CHANGED,
17  ZoneChangedData,
18 )
19 
20 
22  hass: HomeAssistant,
23  config: ConfigType,
24  async_add_entities: AddEntitiesCallback,
25  discovery_info: DiscoveryInfoType | None = None,
26 ) -> None:
27  """Set up the Ness Alarm binary sensor devices."""
28  if not discovery_info:
29  return
30 
31  configured_zones = discovery_info[CONF_ZONES]
32 
33  devices = []
34 
35  for zone_config in configured_zones:
36  zone_type = zone_config[CONF_ZONE_TYPE]
37  zone_name = zone_config[CONF_ZONE_NAME]
38  zone_id = zone_config[CONF_ZONE_ID]
39  device = NessZoneBinarySensor(
40  zone_id=zone_id, name=zone_name, zone_type=zone_type
41  )
42  devices.append(device)
43 
44  async_add_entities(devices)
45 
46 
48  """Representation of an Ness alarm zone as a binary sensor."""
49 
50  _attr_should_poll = False
51 
52  def __init__(self, zone_id, name, zone_type):
53  """Initialize the binary_sensor."""
54  self._zone_id_zone_id = zone_id
55  self._name_name = name
56  self._type_type = zone_type
57  self._state_state = 0
58 
59  async def async_added_to_hass(self) -> None:
60  """Register callbacks."""
61  self.async_on_removeasync_on_remove(
63  self.hasshass, SIGNAL_ZONE_CHANGED, self._handle_zone_change_handle_zone_change
64  )
65  )
66 
67  @property
68  def name(self):
69  """Return the name of the entity."""
70  return self._name_name
71 
72  @property
73  def is_on(self):
74  """Return true if sensor is on."""
75  return self._state_state == 1
76 
77  @property
78  def device_class(self):
79  """Return the class of this sensor, from DEVICE_CLASSES."""
80  return self._type_type
81 
82  @callback
83  def _handle_zone_change(self, data: ZoneChangedData):
84  """Handle zone state update."""
85  if self._zone_id_zone_id == data.zone_id:
86  self._state_state = data.state
87  self.async_write_ha_stateasync_write_ha_state()
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
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