Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Envisalink zone bypass switches."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from homeassistant.components.switch import SwitchEntity
9 from homeassistant.core import HomeAssistant, callback
10 from homeassistant.helpers.dispatcher import async_dispatcher_connect
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
13 
14 from . import CONF_ZONENAME, DATA_EVL, SIGNAL_ZONE_BYPASS_UPDATE, ZONE_SCHEMA
15 from .entity import EnvisalinkEntity
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 
21  hass: HomeAssistant,
22  config: ConfigType,
23  async_add_entities: AddEntitiesCallback,
24  discovery_info: DiscoveryInfoType | None = None,
25 ) -> None:
26  """Set up the Envisalink switch entities."""
27  if not discovery_info:
28  return
29  configured_zones = discovery_info["zones"]
30 
31  entities = []
32  for zone_num in configured_zones:
33  entity_config_data = ZONE_SCHEMA(configured_zones[zone_num])
34  zone_name = f"{entity_config_data[CONF_ZONENAME]}_bypass"
35  _LOGGER.debug("Setting up zone_bypass switch: %s", zone_name)
36 
37  entity = EnvisalinkSwitch(
38  hass,
39  zone_num,
40  zone_name,
41  hass.data[DATA_EVL].alarm_state["zone"][zone_num],
42  hass.data[DATA_EVL],
43  )
44  entities.append(entity)
45 
46  async_add_entities(entities)
47 
48 
50  """Representation of an Envisalink switch."""
51 
52  def __init__(self, hass, zone_number, zone_name, info, controller):
53  """Initialize the switch."""
54  self._zone_number_zone_number = zone_number
55 
56  super().__init__(zone_name, info, controller)
57 
58  async def async_added_to_hass(self) -> None:
59  """Register callbacks."""
60  self.async_on_removeasync_on_remove(
62  self.hasshass, SIGNAL_ZONE_BYPASS_UPDATE, self.async_update_callbackasync_update_callback
63  )
64  )
65 
66  @property
67  def is_on(self):
68  """Return the boolean response if the zone is bypassed."""
69  return self._info_info["bypassed"]
70 
71  async def async_turn_on(self, **kwargs: Any) -> None:
72  """Send the bypass keypress sequence to toggle the zone bypass."""
73  self._controller_controller.toggle_zone_bypass(self._zone_number_zone_number)
74 
75  async def async_turn_off(self, **kwargs: Any) -> None:
76  """Send the bypass keypress sequence to toggle the zone bypass."""
77  self._controller_controller.toggle_zone_bypass(self._zone_number_zone_number)
78 
79  @callback
80  def async_update_callback(self, bypass_map):
81  """Update the zone bypass state in HA, if needed."""
82  if bypass_map is None or self._zone_number_zone_number in bypass_map:
83  _LOGGER.debug("Bypass state changed for zone %d", self._zone_number_zone_number)
84  self.async_write_ha_stateasync_write_ha_state()
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103