Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for LCN binary sensors."""
2 
3 from collections.abc import Iterable
4 from functools import partial
5 
6 import pypck
7 
8 from homeassistant.components.automation import automations_with_entity
10  DOMAIN as DOMAIN_BINARY_SENSOR,
11  BinarySensorEntity,
12 )
13 from homeassistant.components.script import scripts_with_entity
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.const import CONF_DOMAIN, CONF_ENTITIES, CONF_SOURCE
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19  IssueSeverity,
20  async_create_issue,
21  async_delete_issue,
22 )
23 from homeassistant.helpers.typing import ConfigType
24 
25 from .const import (
26  ADD_ENTITIES_CALLBACKS,
27  BINSENSOR_PORTS,
28  CONF_DOMAIN_DATA,
29  DOMAIN,
30  SETPOINTS,
31 )
32 from .entity import LcnEntity
33 from .helpers import InputType
34 
35 
37  config_entry: ConfigEntry,
38  async_add_entities: AddEntitiesCallback,
39  entity_configs: Iterable[ConfigType],
40 ) -> None:
41  """Add entities for this domain."""
42  entities: list[LcnRegulatorLockSensor | LcnBinarySensor | LcnLockKeysSensor] = []
43  for entity_config in entity_configs:
44  if entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in SETPOINTS:
45  entities.append(LcnRegulatorLockSensor(entity_config, config_entry))
46  elif entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in BINSENSOR_PORTS:
47  entities.append(LcnBinarySensor(entity_config, config_entry))
48  else: # in KEY
49  entities.append(LcnLockKeysSensor(entity_config, config_entry))
50 
51  async_add_entities(entities)
52 
53 
55  hass: HomeAssistant,
56  config_entry: ConfigEntry,
57  async_add_entities: AddEntitiesCallback,
58 ) -> None:
59  """Set up LCN switch entities from a config entry."""
60  add_entities = partial(
61  add_lcn_entities,
62  config_entry,
63  async_add_entities,
64  )
65 
66  hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update(
67  {DOMAIN_BINARY_SENSOR: add_entities}
68  )
69 
71  (
72  entity_config
73  for entity_config in config_entry.data[CONF_ENTITIES]
74  if entity_config[CONF_DOMAIN] == DOMAIN_BINARY_SENSOR
75  ),
76  )
77 
78 
80  """Representation of a LCN binary sensor for regulator locks."""
81 
82  def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
83  """Initialize the LCN binary sensor."""
84  super().__init__(config, config_entry)
85 
86  self.setpoint_variablesetpoint_variable = pypck.lcn_defs.Var[
87  config[CONF_DOMAIN_DATA][CONF_SOURCE]
88  ]
89 
90  async def async_added_to_hass(self) -> None:
91  """Run when entity about to be added to hass."""
92  await super().async_added_to_hass()
93 
94  if not self.device_connectiondevice_connection.is_group:
95  await self.device_connectiondevice_connection.activate_status_request_handler(
96  self.setpoint_variablesetpoint_variable
97  )
98 
99  entity_automations = automations_with_entity(self.hasshass, self.entity_identity_id)
100  entity_scripts = scripts_with_entity(self.hasshass, self.entity_identity_id)
101  if entity_automations + entity_scripts:
103  self.hasshass,
104  DOMAIN,
105  f"deprecated_binary_sensor_{self.entity_id}",
106  breaks_in_ha_version="2025.5.0",
107  is_fixable=False,
108  severity=IssueSeverity.WARNING,
109  translation_key="deprecated_regulatorlock_sensor",
110  translation_placeholders={
111  "entity": f"{DOMAIN_BINARY_SENSOR}.{self.name.lower().replace(' ', '_')}",
112  },
113  )
114 
115  async def async_will_remove_from_hass(self) -> None:
116  """Run when entity will be removed from hass."""
117  await super().async_will_remove_from_hass()
118  if not self.device_connectiondevice_connection.is_group:
119  await self.device_connectiondevice_connection.cancel_status_request_handler(
120  self.setpoint_variablesetpoint_variable
121  )
123  self.hasshass, DOMAIN, f"deprecated_binary_sensor_{self.entity_id}"
124  )
125 
126  def input_received(self, input_obj: InputType) -> None:
127  """Set sensor value when LCN input object (command) is received."""
128  if (
129  not isinstance(input_obj, pypck.inputs.ModStatusVar)
130  or input_obj.get_var() != self.setpoint_variablesetpoint_variable
131  ):
132  return
133 
134  self._attr_is_on_attr_is_on = input_obj.get_value().is_locked_regulator()
135  self.async_write_ha_stateasync_write_ha_state()
136 
137 
139  """Representation of a LCN binary sensor for binary sensor ports."""
140 
141  def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
142  """Initialize the LCN binary sensor."""
143  super().__init__(config, config_entry)
144 
145  self.bin_sensor_portbin_sensor_port = pypck.lcn_defs.BinSensorPort[
146  config[CONF_DOMAIN_DATA][CONF_SOURCE]
147  ]
148 
149  async def async_added_to_hass(self) -> None:
150  """Run when entity about to be added to hass."""
151  await super().async_added_to_hass()
152  if not self.device_connectiondevice_connection.is_group:
153  await self.device_connectiondevice_connection.activate_status_request_handler(
154  self.bin_sensor_portbin_sensor_port
155  )
156 
157  async def async_will_remove_from_hass(self) -> None:
158  """Run when entity will be removed from hass."""
159  await super().async_will_remove_from_hass()
160  if not self.device_connectiondevice_connection.is_group:
161  await self.device_connectiondevice_connection.cancel_status_request_handler(
162  self.bin_sensor_portbin_sensor_port
163  )
164 
165  def input_received(self, input_obj: InputType) -> None:
166  """Set sensor value when LCN input object (command) is received."""
167  if not isinstance(input_obj, pypck.inputs.ModStatusBinSensors):
168  return
169 
170  self._attr_is_on_attr_is_on = input_obj.get_state(self.bin_sensor_portbin_sensor_port.value)
171  self.async_write_ha_stateasync_write_ha_state()
172 
173 
175  """Representation of a LCN sensor for key locks."""
176 
177  def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
178  """Initialize the LCN sensor."""
179  super().__init__(config, config_entry)
180 
181  self.sourcesource = pypck.lcn_defs.Key[config[CONF_DOMAIN_DATA][CONF_SOURCE]]
182 
183  async def async_added_to_hass(self) -> None:
184  """Run when entity about to be added to hass."""
185  await super().async_added_to_hass()
186 
187  if not self.device_connectiondevice_connection.is_group:
188  await self.device_connectiondevice_connection.activate_status_request_handler(self.sourcesource)
189 
190  entity_automations = automations_with_entity(self.hasshass, self.entity_identity_id)
191  entity_scripts = scripts_with_entity(self.hasshass, self.entity_identity_id)
192  if entity_automations + entity_scripts:
194  self.hasshass,
195  DOMAIN,
196  f"deprecated_binary_sensor_{self.entity_id}",
197  breaks_in_ha_version="2025.5.0",
198  is_fixable=False,
199  severity=IssueSeverity.WARNING,
200  translation_key="deprecated_keylock_sensor",
201  translation_placeholders={
202  "entity": f"{DOMAIN_BINARY_SENSOR}.{self.name.lower().replace(' ', '_')}",
203  },
204  )
205 
206  async def async_will_remove_from_hass(self) -> None:
207  """Run when entity will be removed from hass."""
208  await super().async_will_remove_from_hass()
209  if not self.device_connectiondevice_connection.is_group:
210  await self.device_connectiondevice_connection.cancel_status_request_handler(self.sourcesource)
212  self.hasshass, DOMAIN, f"deprecated_binary_sensor_{self.entity_id}"
213  )
214 
215  def input_received(self, input_obj: InputType) -> None:
216  """Set sensor value when LCN input object (command) is received."""
217  if (
218  not isinstance(input_obj, pypck.inputs.ModStatusKeyLocks)
219  or self.sourcesource not in pypck.lcn_defs.Key
220  ):
221  return
222 
223  table_id = ord(self.sourcesource.name[0]) - 65
224  key_id = int(self.sourcesource.name[1]) - 1
225 
226  self._attr_is_on_attr_is_on = input_obj.get_state(table_id, key_id)
227  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, ConfigType config, ConfigEntry config_entry)
None __init__(self, ConfigType config, ConfigEntry config_entry)
None __init__(self, ConfigType config, ConfigEntry config_entry)
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
list[str] automations_with_entity(HomeAssistant hass, str entity_id)
Definition: __init__.py:191
IssData update(pyiss.ISS iss)
Definition: __init__.py:33
None add_lcn_entities(ConfigEntry config_entry, AddEntitiesCallback async_add_entities, Iterable[ConfigType] entity_configs)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
None async_create_issue(HomeAssistant hass, str entry_id)
Definition: repairs.py:69
None async_delete_issue(HomeAssistant hass, str entry_id)
Definition: repairs.py:85
list[str] scripts_with_entity(HomeAssistant hass, str entity_id)
Definition: __init__.py:126