Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for HomeMatic binary sensors."""
2 
3 from __future__ import annotations
4 
6  BinarySensorDeviceClass,
7  BinarySensorEntity,
8 )
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
12 
13 from .const import ATTR_DISCOVER_DEVICES, ATTR_DISCOVERY_TYPE, DISCOVER_BATTERY
14 from .entity import HMDevice
15 
16 SENSOR_TYPES_CLASS = {
17  "IPShutterContact": BinarySensorDeviceClass.OPENING,
18  "IPShutterContactSabotage": BinarySensorDeviceClass.OPENING,
19  "MaxShutterContact": BinarySensorDeviceClass.OPENING,
20  "Motion": BinarySensorDeviceClass.MOTION,
21  "MotionV2": BinarySensorDeviceClass.MOTION,
22  "PresenceIP": BinarySensorDeviceClass.MOTION,
23  "Remote": None,
24  "RemoteMotion": None,
25  "ShutterContact": BinarySensorDeviceClass.OPENING,
26  "Smoke": BinarySensorDeviceClass.SMOKE,
27  "SmokeV2": BinarySensorDeviceClass.SMOKE,
28  "TiltSensor": None,
29  "WeatherSensor": None,
30  "IPContact": BinarySensorDeviceClass.OPENING,
31  "MotionIP": BinarySensorDeviceClass.MOTION,
32  "MotionIPV2": BinarySensorDeviceClass.MOTION,
33  "MotionIPContactSabotage": BinarySensorDeviceClass.MOTION,
34  "IPRemoteMotionV2": BinarySensorDeviceClass.MOTION,
35 }
36 
37 
39  hass: HomeAssistant,
40  config: ConfigType,
41  add_entities: AddEntitiesCallback,
42  discovery_info: DiscoveryInfoType | None = None,
43 ) -> None:
44  """Set up the HomeMatic binary sensor platform."""
45  if discovery_info is None:
46  return
47 
48  devices: list[BinarySensorEntity] = []
49  for conf in discovery_info[ATTR_DISCOVER_DEVICES]:
50  if discovery_info[ATTR_DISCOVERY_TYPE] == DISCOVER_BATTERY:
51  devices.append(HMBatterySensor(conf))
52  else:
53  devices.append(HMBinarySensor(conf))
54 
55  add_entities(devices, True)
56 
57 
59  """Representation of a binary HomeMatic device."""
60 
61  @property
62  def is_on(self):
63  """Return true if switch is on."""
64  if not self.availableavailableavailable:
65  return False
66  return bool(self._hm_get_state_hm_get_state())
67 
68  @property
69  def device_class(self):
70  """Return the class of this sensor from DEVICE_CLASSES."""
71  # If state is MOTION (Only RemoteMotion working)
72  if self._state_state_state == "MOTION":
73  return BinarySensorDeviceClass.MOTION
74  return SENSOR_TYPES_CLASS.get(self._hmdevice_hmdevice.__class__.__name__)
75 
76  def _init_data_struct(self):
77  """Generate the data dictionary (self._data) from metadata."""
78  # Add state to data struct
79  if self._state_state_state:
80  self._data.update({self._state_state_state: None})
81 
82 
84  """Representation of an HomeMatic low battery sensor."""
85 
86  _attr_device_class = BinarySensorDeviceClass.BATTERY
87 
88  @property
89  def is_on(self):
90  """Return True if battery is low."""
91  return bool(self._hm_get_state_hm_get_state())
92 
93  def _init_data_struct(self):
94  """Generate the data dictionary (self._data) from metadata."""
95  # Add state to data struct
96  if self._state_state:
97  self._data.update({self._state_state: None})
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)