Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Binary sensor support for Wireless Sensor Tags."""
2 
3 from __future__ import annotations
4 
5 import voluptuous as vol
6 
8  PLATFORM_SCHEMA as BINARY_SENSOR_PLATFORM_SCHEMA,
9  BinarySensorEntity,
10 )
11 from homeassistant.const import CONF_MONITORED_CONDITIONS, STATE_OFF, STATE_ON, Platform
12 from homeassistant.core import HomeAssistant, callback
14 from homeassistant.helpers.dispatcher import async_dispatcher_connect
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
17 
18 from .const import DOMAIN, SIGNAL_BINARY_EVENT_UPDATE
19 from .entity import WirelessTagBaseSensor
20 from .util import async_migrate_unique_id
21 
22 # On means in range, Off means out of range
23 SENSOR_PRESENCE = "presence"
24 
25 # On means motion detected, Off means clear
26 SENSOR_MOTION = "motion"
27 
28 # On means open, Off means closed
29 SENSOR_DOOR = "door"
30 
31 # On means temperature become too cold, Off means normal
32 SENSOR_COLD = "cold"
33 
34 # On means hot, Off means normal
35 SENSOR_HEAT = "heat"
36 
37 # On means too dry (humidity), Off means normal
38 SENSOR_DRY = "dry"
39 
40 # On means too wet (humidity), Off means normal
41 SENSOR_WET = "wet"
42 
43 # On means light detected, Off means no light
44 SENSOR_LIGHT = "light"
45 
46 # On means moisture detected (wet), Off means no moisture (dry)
47 SENSOR_MOISTURE = "moisture"
48 
49 # On means tag battery is low, Off means normal
50 SENSOR_BATTERY = "battery"
51 
52 # Sensor types: Name, device_class, push notification type representing 'on',
53 # attr to check
54 SENSOR_TYPES = {
55  SENSOR_PRESENCE: "Presence",
56  SENSOR_MOTION: "Motion",
57  SENSOR_DOOR: "Door",
58  SENSOR_COLD: "Cold",
59  SENSOR_HEAT: "Heat",
60  SENSOR_DRY: "Too dry",
61  SENSOR_WET: "Too wet",
62  SENSOR_LIGHT: "Light",
63  SENSOR_MOISTURE: "Leak",
64  SENSOR_BATTERY: "Low Battery",
65 }
66 
67 
68 PLATFORM_SCHEMA = BINARY_SENSOR_PLATFORM_SCHEMA.extend(
69  {
70  vol.Required(CONF_MONITORED_CONDITIONS, default=[]): vol.All(
71  cv.ensure_list, [vol.In(SENSOR_TYPES)]
72  )
73  }
74 )
75 
76 
78  hass: HomeAssistant,
79  config: ConfigType,
80  async_add_entities: AddEntitiesCallback,
81  discovery_info: DiscoveryInfoType | None = None,
82 ) -> None:
83  """Set up the platform for a WirelessTags."""
84  platform = hass.data[DOMAIN]
85 
86  sensors = []
87  tags = platform.tags
88  for tag in tags.values():
89  allowed_sensor_types = tag.supported_binary_events_types
90  for sensor_type in config[CONF_MONITORED_CONDITIONS]:
91  if sensor_type in allowed_sensor_types:
92  async_migrate_unique_id(hass, tag, Platform.BINARY_SENSOR, sensor_type)
93  sensors.append(WirelessTagBinarySensor(platform, tag, sensor_type))
94 
95  async_add_entities(sensors, True)
96 
97 
99  """A binary sensor implementation for WirelessTags."""
100 
101  def __init__(self, api, tag, sensor_type):
102  """Initialize a binary sensor for a Wireless Sensor Tags."""
103  super().__init__(api, tag)
104  self._sensor_type_sensor_type = sensor_type
105  self._name_name_name = f"{self._tag.name} {self.event.human_readable_name}"
106  self._attr_unique_id_attr_unique_id = f"{self._uuid}_{self._sensor_type}"
107 
108  async def async_added_to_hass(self) -> None:
109  """Register callbacks."""
110  tag_id = self.tag_idtag_id
111  event_type = self.device_classdevice_classdevice_class
112  mac = self.tag_manager_mactag_manager_mac
113  self.async_on_removeasync_on_remove(
115  self.hasshass,
116  SIGNAL_BINARY_EVENT_UPDATE.format(tag_id, event_type, mac),
117  self._on_binary_event_callback_on_binary_event_callback,
118  )
119  )
120 
121  @property
122  def is_on(self):
123  """Return True if the binary sensor is on."""
124  return self._state_state_state == STATE_ON
125 
126  @property
127  def device_class(self):
128  """Return the class of the binary sensor."""
129  return self._sensor_type_sensor_type
130 
131  @property
132  def event(self):
133  """Binary event of tag."""
134  return self._tag_tag_tag.event[self._sensor_type_sensor_type]
135 
136  @property
137  def principal_value(self):
138  """Return value of tag.
139 
140  Subclasses need override based on type of sensor.
141  """
142  return STATE_ON if self.eventevent.is_state_on else STATE_OFF
143 
145  """Use raw princial value."""
146  return self.principal_valueprincipal_valueprincipal_value
147 
148  @callback
149  def _on_binary_event_callback(self, new_tag):
150  """Update state from arrived push notification."""
151  self._tag_tag_tag = new_tag
152  self._state_state_state = self.updated_state_valueupdated_state_valueupdated_state_value()
153  self.async_write_ha_stateasync_write_ha_state()
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None async_migrate_unique_id(HomeAssistant hass, ConfigEntry config_entry, Appliance device)
Definition: __init__.py:84
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103