Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Qwikswitch Binary Sensors."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from pyqwikswitch.qwikswitch import SENSORS
8 
9 from homeassistant.components.binary_sensor import BinarySensorEntity
10 from homeassistant.core import HomeAssistant, callback
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
13 
14 from . import DOMAIN as QWIKSWITCH
15 from .entity import QSEntity
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 
21  hass: HomeAssistant,
22  _: ConfigType,
23  add_entities: AddEntitiesCallback,
24  discovery_info: DiscoveryInfoType | None = None,
25 ) -> None:
26  """Add binary sensor from the main Qwikswitch component."""
27  if discovery_info is None:
28  return
29 
30  qsusb = hass.data[QWIKSWITCH]
31  _LOGGER.debug("Setup qwikswitch.binary_sensor %s, %s", qsusb, discovery_info)
32  devs = [QSBinarySensor(sensor) for sensor in discovery_info[QWIKSWITCH]]
33  add_entities(devs)
34 
35 
37  """Sensor based on a Qwikswitch relay/dimmer module."""
38 
39  _val = False
40 
41  def __init__(self, sensor):
42  """Initialize the sensor."""
43 
44  super().__init__(sensor["id"], sensor["name"])
45  self.channelchannel = sensor["channel"]
46  sensor_type = sensor["type"]
47 
48  self._decode, _ = SENSORS[sensor_type]
49  self._invert_invert = not sensor.get("invert", False)
50  self._class_class = sensor.get("class", "door")
51 
52  @callback
53  def update_packet(self, packet):
54  """Receive update packet from QSUSB."""
55  val = self._decode(packet, channel=self.channelchannel)
56  _LOGGER.debug(
57  "Update %s (%s:%s) decoded as %s: %s",
58  self.entity_identity_id,
59  self.qsidqsid,
60  self.channelchannel,
61  val,
62  packet,
63  )
64  if val is not None:
65  self._val_val_val = bool(val)
66  self.async_write_ha_stateasync_write_ha_state()
67 
68  @property
69  def is_on(self):
70  """Check if device is on (non-zero)."""
71  return self._val_val_val == self._invert_invert
72 
73  @property
74  def unique_id(self):
75  """Return a unique identifier for this sensor."""
76  return f"qs{self.qsid}:{self.channel}"
77 
78  @property
79  def device_class(self):
80  """Return the class of this sensor."""
81  return self._class_class
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None async_setup_platform(HomeAssistant hass, ConfigType _, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)