Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Pilight binary sensors."""
2 
3 from __future__ import annotations
4 
5 import datetime
6 
7 import voluptuous as vol
8 
10  PLATFORM_SCHEMA as BINARY_SENSOR_PLATFORM_SCHEMA,
11  BinarySensorEntity,
12 )
13 from homeassistant.const import (
14  CONF_DISARM_AFTER_TRIGGER,
15  CONF_NAME,
16  CONF_PAYLOAD,
17  CONF_PAYLOAD_OFF,
18  CONF_PAYLOAD_ON,
19 )
20 from homeassistant.core import HomeAssistant
21 from homeassistant.helpers import config_validation as cv
22 from homeassistant.helpers.entity_platform import AddEntitiesCallback
23 from homeassistant.helpers.event import track_point_in_time
24 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
25 from homeassistant.util import dt as dt_util
26 
27 from . import EVENT
28 
29 CONF_VARIABLE = "variable"
30 CONF_RESET_DELAY_SEC = "reset_delay_sec"
31 
32 DEFAULT_NAME = "Pilight Binary Sensor"
33 PLATFORM_SCHEMA = BINARY_SENSOR_PLATFORM_SCHEMA.extend(
34  {
35  vol.Required(CONF_VARIABLE): cv.string,
36  vol.Required(CONF_PAYLOAD): vol.Schema(dict),
37  vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
38  vol.Optional(CONF_PAYLOAD_ON, default="on"): vol.Any(
39  cv.positive_int, cv.small_float, cv.string
40  ),
41  vol.Optional(CONF_PAYLOAD_OFF, default="off"): vol.Any(
42  cv.positive_int, cv.small_float, cv.string
43  ),
44  vol.Optional(CONF_DISARM_AFTER_TRIGGER, default=False): cv.boolean,
45  vol.Optional(CONF_RESET_DELAY_SEC, default=30): cv.positive_int,
46  }
47 )
48 
49 
51  hass: HomeAssistant,
52  config: ConfigType,
53  add_entities: AddEntitiesCallback,
54  discovery_info: DiscoveryInfoType | None = None,
55 ) -> None:
56  """Set up Pilight Binary Sensor."""
57  if config.get(CONF_DISARM_AFTER_TRIGGER):
59  [
61  hass=hass,
62  name=config.get(CONF_NAME),
63  variable=config.get(CONF_VARIABLE),
64  payload=config.get(CONF_PAYLOAD),
65  on_value=config.get(CONF_PAYLOAD_ON),
66  off_value=config.get(CONF_PAYLOAD_OFF),
67  rst_dly_sec=config.get(CONF_RESET_DELAY_SEC),
68  )
69  ]
70  )
71  else:
73  [
75  hass=hass,
76  name=config.get(CONF_NAME),
77  variable=config.get(CONF_VARIABLE),
78  payload=config.get(CONF_PAYLOAD),
79  on_value=config.get(CONF_PAYLOAD_ON),
80  off_value=config.get(CONF_PAYLOAD_OFF),
81  )
82  ]
83  )
84 
85 
87  """Representation of a binary sensor that can be updated using Pilight."""
88 
89  def __init__(self, hass, name, variable, payload, on_value, off_value):
90  """Initialize the sensor."""
91  self._state_state = False
92  self._hass_hass = hass
93  self._name_name = name
94  self._variable_variable = variable
95  self._payload_payload = payload
96  self._on_value_on_value = on_value
97  self._off_value_off_value = off_value
98 
99  hass.bus.listen(EVENT, self._handle_code_handle_code)
100 
101  @property
102  def name(self):
103  """Return the name of the sensor."""
104  return self._name_name
105 
106  @property
107  def is_on(self):
108  """Return True if the binary sensor is on."""
109  return self._state_state
110 
111  def _handle_code(self, call):
112  """Handle received code by the pilight-daemon.
113 
114  If the code matches the defined payload
115  of this sensor the sensor state is changed accordingly.
116  """
117  # Check if received code matches defined playoad
118  # True if payload is contained in received code dict
119  payload_ok = True
120  for key in self._payload_payload:
121  if key not in call.data:
122  payload_ok = False
123  continue
124  if self._payload_payload[key] != call.data[key]:
125  payload_ok = False
126  # Read out variable if payload ok
127  if payload_ok:
128  if self._variable_variable not in call.data:
129  return
130  value = call.data[self._variable_variable]
131  self._state_state = value == self._on_value_on_value
132  self.schedule_update_ha_stateschedule_update_ha_state()
133 
134 
136  """Representation of a binary sensor that can be updated using Pilight."""
137 
138  def __init__(
139  self, hass, name, variable, payload, on_value, off_value, rst_dly_sec=30
140  ):
141  """Initialize the sensor."""
142  self._state_state = False
143  self._hass_hass = hass
144  self._name_name = name
145  self._variable_variable = variable
146  self._payload_payload = payload
147  self._on_value_on_value = on_value
148  self._off_value_off_value = off_value
149  self._reset_delay_sec_reset_delay_sec = rst_dly_sec
150  self._delay_after_delay_after = None
151  self._hass_hass = hass
152 
153  hass.bus.listen(EVENT, self._handle_code_handle_code)
154 
155  @property
156  def name(self):
157  """Return the name of the sensor."""
158  return self._name_name
159 
160  @property
161  def is_on(self):
162  """Return True if the binary sensor is on."""
163  return self._state_state
164 
165  def _reset_state(self, call):
166  self._state_state = False
167  self._delay_after_delay_after = None
168  self.schedule_update_ha_stateschedule_update_ha_state()
169 
170  def _handle_code(self, call):
171  """Handle received code by the pilight-daemon.
172 
173  If the code matches the defined payload
174  of this sensor the sensor state is changed accordingly.
175  """
176  # Check if received code matches defined payload
177  # True if payload is contained in received code dict
178  payload_ok = True
179  for key in self._payload_payload:
180  if key not in call.data:
181  payload_ok = False
182  continue
183  if self._payload_payload[key] != call.data[key]:
184  payload_ok = False
185  # Read out variable if payload ok
186  if payload_ok:
187  if self._variable_variable not in call.data:
188  return
189  value = call.data[self._variable_variable]
190  self._state_state = value == self._on_value_on_value
191  if self._delay_after_delay_after is None:
192  self._delay_after_delay_after = dt_util.utcnow() + datetime.timedelta(
193  seconds=self._reset_delay_sec_reset_delay_sec
194  )
195  track_point_in_time(self._hass_hass, self._reset_state_reset_state, self._delay_after_delay_after)
196  self.schedule_update_ha_stateschedule_update_ha_state()
def __init__(self, hass, name, variable, payload, on_value, off_value)
def __init__(self, hass, name, variable, payload, on_value, off_value, rst_dly_sec=30)
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)