Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for binary_sensor entities."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass, field
6 
7 from gardena_bluetooth.const import Sensor, Valve
8 from gardena_bluetooth.parse import CharacteristicBool
9 
11  BinarySensorDeviceClass,
12  BinarySensorEntity,
13  BinarySensorEntityDescription,
14 )
15 from homeassistant.const import EntityCategory
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 
19 from . import GardenaBluetoothConfigEntry
20 from .entity import GardenaBluetoothDescriptorEntity
21 
22 
23 @dataclass(frozen=True)
25  """Description of entity."""
26 
27  char: CharacteristicBool = field(default_factory=lambda: CharacteristicBool(""))
28 
29  @property
30  def context(self) -> set[str]:
31  """Context needed for update coordinator."""
32  return {self.char.uuid}
33 
34 
35 DESCRIPTIONS = (
37  key=Valve.connected_state.uuid,
38  translation_key="valve_connected_state",
39  device_class=BinarySensorDeviceClass.CONNECTIVITY,
40  entity_category=EntityCategory.DIAGNOSTIC,
41  char=Valve.connected_state,
42  ),
44  key=Sensor.connected_state.uuid,
45  translation_key="sensor_connected_state",
46  device_class=BinarySensorDeviceClass.CONNECTIVITY,
47  entity_category=EntityCategory.DIAGNOSTIC,
48  char=Sensor.connected_state,
49  ),
50 )
51 
52 
54  hass: HomeAssistant,
55  entry: GardenaBluetoothConfigEntry,
56  async_add_entities: AddEntitiesCallback,
57 ) -> None:
58  """Set up binary sensor based on a config entry."""
59  coordinator = entry.runtime_data
60  entities = [
61  GardenaBluetoothBinarySensor(coordinator, description, description.context)
62  for description in DESCRIPTIONS
63  if description.key in coordinator.characteristics
64  ]
65  async_add_entities(entities)
66 
67 
69  GardenaBluetoothDescriptorEntity, BinarySensorEntity
70 ):
71  """Representation of a binary sensor."""
72 
73  entity_description: GardenaBluetoothBinarySensorEntityDescription
74 
75  def _handle_coordinator_update(self) -> None:
76  char = self.entity_descriptionentity_description.char
77  self._attr_is_on_attr_is_on = self.coordinator.get_cached(char)
CharacteristicType|None get_cached(self, Characteristic[CharacteristicType] char)
Definition: coordinator.py:80
None async_setup_entry(HomeAssistant hass, GardenaBluetoothConfigEntry entry, AddEntitiesCallback async_add_entities)