Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for w800rf32 binary sensors."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 import voluptuous as vol
8 import W800rf32 as w800
9 
11  DEVICE_CLASSES_SCHEMA,
12  PLATFORM_SCHEMA as BINARY_SENSOR_PLATFORM_SCHEMA,
13  BinarySensorEntity,
14 )
15 from homeassistant.const import CONF_DEVICE_CLASS, CONF_DEVICES, CONF_NAME
16 from homeassistant.core import HomeAssistant, callback
17 from homeassistant.helpers import config_validation as cv, event as evt
18 from homeassistant.helpers.dispatcher import async_dispatcher_connect
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
21 
22 from . import W800RF32_DEVICE
23 
24 _LOGGER = logging.getLogger(__name__)
25 
26 CONF_OFF_DELAY = "off_delay"
27 
28 PLATFORM_SCHEMA = BINARY_SENSOR_PLATFORM_SCHEMA.extend(
29  {
30  vol.Required(CONF_DEVICES): {
31  cv.string: vol.Schema(
32  {
33  vol.Optional(CONF_NAME): cv.string,
34  vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
35  vol.Optional(CONF_OFF_DELAY): vol.All(
36  cv.time_period, cv.positive_timedelta
37  ),
38  }
39  )
40  }
41  },
42  extra=vol.ALLOW_EXTRA,
43 )
44 
45 
47  hass: HomeAssistant,
48  config: ConfigType,
49  add_entities: AddEntitiesCallback,
50  discovery_info: DiscoveryInfoType | None = None,
51 ) -> None:
52  """Set up the Binary Sensor platform to w800rf32."""
53  binary_sensors = []
54  # device_id --> "c1 or a3" X10 device. entity (type dictionary)
55  # --> name, device_class etc
56  for device_id, entity in config[CONF_DEVICES].items():
57  _LOGGER.debug(
58  "Add %s w800rf32.binary_sensor (class %s)",
59  entity[CONF_NAME],
60  entity.get(CONF_DEVICE_CLASS),
61  )
62 
63  device = W800rf32BinarySensor(
64  device_id,
65  entity.get(CONF_NAME),
66  entity.get(CONF_DEVICE_CLASS),
67  entity.get(CONF_OFF_DELAY),
68  )
69 
70  binary_sensors.append(device)
71 
72  add_entities(binary_sensors)
73 
74 
76  """A representation of a w800rf32 binary sensor."""
77 
78  _attr_should_poll = False
79 
80  def __init__(self, device_id, name, device_class=None, off_delay=None):
81  """Initialize the w800rf32 sensor."""
82  self._signal_signal = W800RF32_DEVICE.format(device_id)
83  self._name_name = name
84  self._device_class_device_class = device_class
85  self._off_delay_off_delay = off_delay
86  self._state_state = False
87  self._delay_listener_delay_listener = None
88 
89  @callback
90  def _off_delay_listener(self, now):
91  """Switch device off after a delay."""
92  self._delay_listener_delay_listener = None
93  self.update_stateupdate_state(False)
94 
95  @property
96  def name(self):
97  """Return the device name."""
98  return self._name_name
99 
100  @property
101  def device_class(self):
102  """Return the sensor class."""
103  return self._device_class_device_class
104 
105  @property
106  def is_on(self):
107  """Return true if the sensor state is True."""
108  return self._state_state
109 
110  @callback
111  def binary_sensor_update(self, event):
112  """Call for control updates from the w800rf32 gateway."""
113 
114  if not isinstance(event, w800.W800rf32Event):
115  return
116 
117  dev_id = event.device
118  command = event.command
119 
120  _LOGGER.debug(
121  "BinarySensor update (Device ID: %s Command %s ...)", dev_id, command
122  )
123 
124  # Update the w800rf32 device state
125  if command in ("On", "Off"):
126  is_on = command == "On"
127  self.update_stateupdate_state(is_on)
128 
129  if self.is_onis_on and self._off_delay_off_delay is not None and self._delay_listener_delay_listener is None:
130  self._delay_listener_delay_listener = evt.async_call_later(
131  self.hasshass, self._off_delay_off_delay, self._off_delay_listener_off_delay_listener
132  )
133 
134  def update_state(self, state):
135  """Update the state of the device."""
136  self._state_state = state
137  self.async_write_ha_stateasync_write_ha_state()
138 
139  async def async_added_to_hass(self) -> None:
140  """Register update callback."""
141  async_dispatcher_connect(self.hasshass, self._signal_signal, self.binary_sensor_updatebinary_sensor_update)
def __init__(self, device_id, name, device_class=None, off_delay=None)
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103