Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for MySensors binary sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Any
8 
10  BinarySensorDeviceClass,
11  BinarySensorEntity,
12  BinarySensorEntityDescription,
13 )
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.const import Platform
16 from homeassistant.core import HomeAssistant, callback
17 from homeassistant.helpers.dispatcher import async_dispatcher_connect
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from . import setup_mysensors_platform
21 from .const import MYSENSORS_DISCOVERY, DiscoveryInfo
22 from .entity import MySensorsChildEntity
23 from .helpers import on_unload
24 
25 
26 @dataclass(frozen=True)
28  """Describe a MySensors binary sensor entity."""
29 
30  is_on: Callable[[int, dict[int, str]], bool] = (
31  lambda value_type, values: values[value_type] == "1"
32  )
33 
34 
35 SENSORS: dict[str, MySensorsBinarySensorDescription] = {
37  key="S_DOOR",
38  device_class=BinarySensorDeviceClass.DOOR,
39  ),
41  key="S_MOTION",
42  device_class=BinarySensorDeviceClass.MOTION,
43  ),
45  key="S_SMOKE",
46  device_class=BinarySensorDeviceClass.SMOKE,
47  ),
48  "S_SPRINKLER": MySensorsBinarySensorDescription(
49  key="S_SPRINKLER",
50  device_class=BinarySensorDeviceClass.SAFETY,
51  ),
52  "S_WATER_LEAK": MySensorsBinarySensorDescription(
53  key="S_WATER_LEAK",
54  device_class=BinarySensorDeviceClass.SAFETY,
55  ),
57  key="S_SOUND",
58  device_class=BinarySensorDeviceClass.SOUND,
59  ),
60  "S_VIBRATION": MySensorsBinarySensorDescription(
61  key="S_VIBRATION",
62  device_class=BinarySensorDeviceClass.VIBRATION,
63  ),
65  key="S_MOISTURE",
66  device_class=BinarySensorDeviceClass.MOISTURE,
67  ),
68 }
69 
70 
72  hass: HomeAssistant,
73  config_entry: ConfigEntry,
74  async_add_entities: AddEntitiesCallback,
75 ) -> None:
76  """Set up this platform for a specific ConfigEntry(==Gateway)."""
77 
78  @callback
79  def async_discover(discovery_info: DiscoveryInfo) -> None:
80  """Discover and add a MySensors binary_sensor."""
82  hass,
83  Platform.BINARY_SENSOR,
84  discovery_info,
85  MySensorsBinarySensor,
86  async_add_entities=async_add_entities,
87  )
88 
89  on_unload(
90  hass,
91  config_entry.entry_id,
93  hass,
94  MYSENSORS_DISCOVERY.format(config_entry.entry_id, Platform.BINARY_SENSOR),
95  async_discover,
96  ),
97  )
98 
99 
101  """Representation of a MySensors binary sensor child node."""
102 
103  entity_description: MySensorsBinarySensorDescription
104 
105  def __init__(self, *args: Any, **kwargs: Any) -> None:
106  """Set up the instance."""
107  super().__init__(*args, **kwargs)
108  presentation = self.gateway.const.Presentation
109  self.entity_descriptionentity_description = SENSORS[presentation(self.child_typechild_type).name]
110 
111  @property
112  def is_on(self) -> bool:
113  """Return True if the binary sensor is on."""
114  return self.entity_descriptionentity_description.is_on(self.value_type, self._child_child.values)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
None on_unload(HomeAssistant hass, GatewayId gateway_id, Callable fnct)
Definition: helpers.py:45
None async_discover(DiscoveryInfo discovery_info)
Definition: sensor.py:217
list[MySensorsChildEntity]|None setup_mysensors_platform(HomeAssistant hass, Platform domain, DiscoveryInfo discovery_info, type[MySensorsChildEntity]|Mapping[SensorType, type[MySensorsChildEntity]] device_class,(tuple|None) device_args=None, Callable|None async_add_entities=None)
Definition: __init__.py:112
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103