Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Module for SIA Binary Sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Iterable
6 from dataclasses import dataclass
7 import logging
8 
9 from pysiaalarm import SIAEvent
10 
12  BinarySensorDeviceClass,
13  BinarySensorEntity,
14  BinarySensorEntityDescription,
15 )
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE, EntityCategory
18 from homeassistant.core import HomeAssistant, State, callback
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from .const import (
22  CONF_ACCOUNT,
23  CONF_ACCOUNTS,
24  CONF_ZONES,
25  KEY_CONNECTIVITY,
26  KEY_MOISTURE,
27  KEY_POWER,
28  KEY_SMOKE,
29  SIA_HUB_ZONE,
30 )
31 from .entity import SIABaseEntity, SIAEntityDescription
32 
33 _LOGGER = logging.getLogger(__name__)
34 
35 
36 @dataclass(frozen=True)
38  BinarySensorEntityDescription,
39  SIAEntityDescription,
40 ):
41  """Describes SIA sensor entity."""
42 
43 
44 ENTITY_DESCRIPTION_POWER = SIABinarySensorEntityDescription(
45  key=KEY_POWER,
46  device_class=BinarySensorDeviceClass.POWER,
47  entity_category=EntityCategory.DIAGNOSTIC,
48  code_consequences={
49  "AT": False,
50  "AR": True,
51  },
52 )
53 
54 ENTITY_DESCRIPTION_SMOKE = SIABinarySensorEntityDescription(
55  key=KEY_SMOKE,
56  device_class=BinarySensorDeviceClass.SMOKE,
57  code_consequences={
58  "GA": True,
59  "GH": False,
60  "FA": True,
61  "FH": False,
62  "KA": True,
63  "KH": False,
64  },
65  entity_registry_enabled_default=False,
66 )
67 
68 ENTITY_DESCRIPTION_MOISTURE = SIABinarySensorEntityDescription(
69  key=KEY_MOISTURE,
70  device_class=BinarySensorDeviceClass.MOISTURE,
71  code_consequences={
72  "WA": True,
73  "WH": False,
74  },
75  entity_registry_enabled_default=False,
76 )
77 
78 ENTITY_DESCRIPTION_CONNECTIVITY = SIABinarySensorEntityDescription(
79  key=KEY_CONNECTIVITY,
80  device_class=BinarySensorDeviceClass.CONNECTIVITY,
81  entity_category=EntityCategory.DIAGNOSTIC,
82  code_consequences={"RP": True},
83 )
84 
85 
86 def generate_binary_sensors(entry: ConfigEntry) -> Iterable[SIABinarySensor]:
87  """Generate binary sensors.
88 
89  For each Account there is one power sensor with zone == 0.
90  For each Zone in each Account there is one smoke and one moisture sensor.
91  """
92  for account_data in entry.data[CONF_ACCOUNTS]:
93  account = account_data[CONF_ACCOUNT]
94  zones = entry.options[CONF_ACCOUNTS][account][CONF_ZONES]
95 
97  entry, account, SIA_HUB_ZONE, ENTITY_DESCRIPTION_CONNECTIVITY
98  )
99  yield SIABinarySensor(entry, account, SIA_HUB_ZONE, ENTITY_DESCRIPTION_POWER)
100  for zone in range(1, zones + 1):
101  yield SIABinarySensor(entry, account, zone, ENTITY_DESCRIPTION_SMOKE)
102  yield SIABinarySensor(entry, account, zone, ENTITY_DESCRIPTION_MOISTURE)
103 
104 
106  hass: HomeAssistant,
107  entry: ConfigEntry,
108  async_add_entities: AddEntitiesCallback,
109 ) -> None:
110  """Set up SIA binary sensors from a config entry."""
112 
113 
115  """Class for SIA Binary Sensors."""
116 
117  entity_description: SIABinarySensorEntityDescription
118 
119  def handle_last_state(self, last_state: State | None) -> None:
120  """Handle the last state."""
121  if last_state is not None and last_state.state is not None:
122  if last_state.state == STATE_ON:
123  self._attr_is_on_attr_is_on = True
124  elif last_state.state == STATE_OFF:
125  self._attr_is_on_attr_is_on = False
126  elif last_state.state == STATE_UNAVAILABLE:
127  self._attr_available_attr_available_attr_available = False
128 
129  def update_state(self, sia_event: SIAEvent) -> bool:
130  """Update the state of the binary sensor.
131 
132  Return True if the event was relevant for this entity.
133  """
134  new_state = None
135  if sia_event.code:
136  new_state = self.entity_descriptionentity_description.code_consequences.get(sia_event.code)
137  if new_state is None:
138  return False
139  _LOGGER.debug("New state will be %s", new_state)
140  self._attr_is_on_attr_is_on = bool(new_state)
141  return True
142 
143 
145  """Class for Connectivity Sensor."""
146 
147  @callback
148  def async_post_interval_update(self, _) -> None:
149  """Update state after a ping interval. Overwritten from sia entity base."""
150  self._attr_is_on_attr_is_on_attr_is_on = False
151  self.async_write_ha_stateasync_write_ha_state()
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Iterable[SIABinarySensor] generate_binary_sensors(ConfigEntry entry)