Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Demo platform that has two fake binary sensors."""
2 
3 from __future__ import annotations
4 
6  BinarySensorDeviceClass,
7  BinarySensorEntity,
8 )
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.device_registry import DeviceInfo
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from . import DOMAIN
15 
16 
18  hass: HomeAssistant,
19  config_entry: ConfigEntry,
20  async_add_entities: AddEntitiesCallback,
21 ) -> None:
22  """Set up the demo binary sensor platform."""
24  [
26  "binary_1",
27  "Basement Floor Wet",
28  False,
29  BinarySensorDeviceClass.MOISTURE,
30  ),
32  "binary_2", "Movement Backyard", True, BinarySensorDeviceClass.MOTION
33  ),
34  ]
35  )
36 
37 
39  """representation of a Demo binary sensor."""
40 
41  _attr_has_entity_name = True
42  _attr_name = None
43  _attr_should_poll = False
44 
45  def __init__(
46  self,
47  unique_id: str,
48  device_name: str,
49  state: bool,
50  device_class: BinarySensorDeviceClass,
51  ) -> None:
52  """Initialize the demo sensor."""
53  self._unique_id_unique_id = unique_id
54  self._state_state = state
55  self._attr_device_class_attr_device_class = device_class
56  self._attr_device_info_attr_device_info = DeviceInfo(
57  identifiers={
58  # Serial numbers are unique identifiers within a specific domain
59  (DOMAIN, self.unique_idunique_idunique_id)
60  },
61  name=device_name,
62  )
63 
64  @property
65  def unique_id(self) -> str:
66  """Return the unique id."""
67  return self._unique_id_unique_id
68 
69  @property
70  def is_on(self) -> bool:
71  """Return true if the binary sensor is on."""
72  return self._state_state
None __init__(self, str unique_id, str device_name, bool state, BinarySensorDeviceClass device_class)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)