Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for the Hive binary sensors."""
2 
3 from datetime import timedelta
4 from typing import Any
5 
6 from apyhiveapi import Hive
7 
9  BinarySensorDeviceClass,
10  BinarySensorEntity,
11  BinarySensorEntityDescription,
12 )
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from .const import DOMAIN
18 from .entity import HiveEntity
19 
20 PARALLEL_UPDATES = 0
21 SCAN_INTERVAL = timedelta(seconds=15)
22 
23 
24 BINARY_SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
26  key="contactsensor", device_class=BinarySensorDeviceClass.OPENING
27  ),
29  key="motionsensor",
30  device_class=BinarySensorDeviceClass.MOTION,
31  ),
33  key="Connectivity",
34  device_class=BinarySensorDeviceClass.CONNECTIVITY,
35  ),
37  key="SMOKE_CO",
38  device_class=BinarySensorDeviceClass.SMOKE,
39  ),
41  key="DOG_BARK",
42  device_class=BinarySensorDeviceClass.SOUND,
43  ),
45  key="GLASS_BREAK",
46  device_class=BinarySensorDeviceClass.SOUND,
47  ),
48 )
49 
50 SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
52  key="Heating_State",
53  translation_key="heating",
54  ),
56  key="Heating_Boost",
57  translation_key="heating",
58  ),
60  key="Hotwater_State",
61  translation_key="hot_water",
62  ),
64  key="Hotwater_Boost",
65  translation_key="hot_water",
66  ),
67 )
68 
69 
71  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
72 ) -> None:
73  """Set up Hive thermostat based on a config entry."""
74 
75  hive = hass.data[DOMAIN][entry.entry_id]
76 
77  sensors: list[BinarySensorEntity] = []
78 
79  devices = hive.session.deviceList.get("binary_sensor")
80  sensors.extend(
81  HiveBinarySensorEntity(hive, dev, description)
82  for dev in devices
83  for description in BINARY_SENSOR_TYPES
84  if dev["hiveType"] == description.key
85  )
86 
87  devices = hive.session.deviceList.get("sensor")
88  sensors.extend(
89  HiveSensorEntity(hive, dev, description)
90  for dev in devices
91  for description in SENSOR_TYPES
92  if dev["hiveType"] == description.key
93  )
94 
95  async_add_entities(sensors, True)
96 
97 
99  """Representation of a Hive binary sensor."""
100 
101  def __init__(
102  self,
103  hive: Hive,
104  hive_device: dict[str, Any],
105  entity_description: BinarySensorEntityDescription,
106  ) -> None:
107  """Initialise hive binary sensor."""
108  super().__init__(hive, hive_device)
109  self.entity_descriptionentity_description = entity_description
110 
111  async def async_update(self) -> None:
112  """Update all Node data from Hive."""
113  await self.hivehive.session.updateData(self.devicedevicedevice)
114  self.devicedevicedevice = await self.hivehive.sensor.getSensor(self.devicedevicedevice)
115  self.attributesattributes = self.devicedevicedevice.get("attributes", {})
116  self._attr_is_on_attr_is_on = self.devicedevicedevice["status"]["state"]
117  if self.devicedevicedevice["hiveType"] != "Connectivity":
118  self._attr_available_attr_available = self.devicedevicedevice["deviceData"].get("online")
119  else:
120  self._attr_available_attr_available = True
121 
122 
124  """Hive Sensor Entity."""
125 
126  def __init__(
127  self,
128  hive: Hive,
129  hive_device: dict[str, Any],
130  entity_description: BinarySensorEntityDescription,
131  ) -> None:
132  """Initialise hive sensor."""
133  super().__init__(hive, hive_device)
134  self.entity_descriptionentity_description = entity_description
135 
136  async def async_update(self) -> None:
137  """Update all Node data from Hive."""
138  await self.hivehive.session.updateData(self.devicedevicedevice)
139  self.devicedevicedevice = await self.hivehive.sensor.getSensor(self.devicedevicedevice)
140  self._attr_is_on_attr_is_on = self.devicedevicedevice["status"]["state"] == "ON"
141  self._attr_available_attr_available = self.devicedevicedevice["deviceData"].get("online")
None __init__(self, Hive hive, dict[str, Any] hive_device, BinarySensorEntityDescription entity_description)
None __init__(self, Hive hive, dict[str, Any] hive_device, BinarySensorEntityDescription entity_description)
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)