Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Toon binary sensors."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 
8  BinarySensorDeviceClass,
9  BinarySensorEntity,
10  BinarySensorEntityDescription,
11 )
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from .const import DOMAIN
17 from .coordinator import ToonDataUpdateCoordinator
18 from .entity import (
19  ToonBoilerDeviceEntity,
20  ToonBoilerModuleDeviceEntity,
21  ToonDisplayDeviceEntity,
22  ToonEntity,
23  ToonRequiredKeysMixin,
24 )
25 
26 
28  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
29 ) -> None:
30  """Set up a Toon binary sensor based on a config entry."""
31  coordinator = hass.data[DOMAIN][entry.entry_id]
32 
33  entities = [
34  description.cls(coordinator, description)
35  for description in BINARY_SENSOR_ENTITIES
36  ]
37  if coordinator.data.thermostat.have_opentherm_boiler:
38  entities.extend(
39  [
40  description.cls(coordinator, description)
41  for description in BINARY_SENSOR_ENTITIES_BOILER
42  ]
43  )
44 
45  async_add_entities(entities, True)
46 
47 
49  """Defines an Toon binary sensor."""
50 
51  entity_description: ToonBinarySensorEntityDescription
52 
53  def __init__(
54  self,
55  coordinator: ToonDataUpdateCoordinator,
56  description: ToonBinarySensorEntityDescription,
57  ) -> None:
58  """Initialize the Toon sensor."""
59  super().__init__(coordinator)
60  self.entity_descriptionentity_description = description
61 
62  self._attr_unique_id_attr_unique_id = (
63  # This unique ID is a bit ugly and contains unneeded information.
64  # It is here for legacy / backward compatible reasons.
65  f"{DOMAIN}_{coordinator.data.agreement.agreement_id}_binary_sensor_{description.key}"
66  )
67 
68  @property
69  def is_on(self) -> bool | None:
70  """Return the status of the binary sensor."""
71  section = getattr(self.coordinator.data, self.entity_descriptionentity_description.section)
72  value = getattr(section, self.entity_descriptionentity_description.measurement)
73 
74  if value is None:
75  return None
76 
77  if self.entity_descriptionentity_description.inverted:
78  return not value
79 
80  return value
81 
82 
84  """Defines a Boiler binary sensor."""
85 
86 
87 class ToonDisplayBinarySensor(ToonBinarySensor, ToonDisplayDeviceEntity):
88  """Defines a Toon Display binary sensor."""
89 
90 
92  """Defines a Boiler module binary sensor."""
93 
94 
95 @dataclass(frozen=True)
97  """Mixin for binary sensor required keys."""
98 
99  cls: type[ToonBinarySensor]
100 
101 
102 @dataclass(frozen=True)
104  BinarySensorEntityDescription, ToonBinarySensorRequiredKeysMixin
105 ):
106  """Describes Toon binary sensor entity."""
107 
108  inverted: bool = False
109 
110 
111 BINARY_SENSOR_ENTITIES = (
113  key="thermostat_info_boiler_connected_None",
114  name="Boiler Module Connection",
115  section="thermostat",
116  measurement="boiler_module_connected",
117  device_class=BinarySensorDeviceClass.CONNECTIVITY,
118  entity_registry_enabled_default=False,
119  cls=ToonBoilerModuleBinarySensor,
120  ),
122  key="thermostat_program_overridden",
123  name="Thermostat Program Override",
124  section="thermostat",
125  measurement="program_overridden",
126  icon="mdi:gesture-tap",
127  cls=ToonDisplayBinarySensor,
128  ),
129 )
130 
131 BINARY_SENSOR_ENTITIES_BOILER: tuple[ToonBinarySensorEntityDescription, ...] = (
133  key="thermostat_info_burner_info_1",
134  name="Boiler Heating",
135  section="thermostat",
136  measurement="heating",
137  icon="mdi:fire",
138  entity_registry_enabled_default=False,
139  cls=ToonBoilerBinarySensor,
140  ),
142  key="thermostat_info_burner_info_2",
143  name="Hot Tap Water",
144  section="thermostat",
145  measurement="hot_tapwater",
146  icon="mdi:water-pump",
147  cls=ToonBoilerBinarySensor,
148  ),
150  key="thermostat_info_burner_info_3",
151  name="Boiler Preheating",
152  section="thermostat",
153  measurement="pre_heating",
154  icon="mdi:fire",
155  entity_registry_enabled_default=False,
156  cls=ToonBoilerBinarySensor,
157  ),
159  key="thermostat_info_burner_info_None",
160  name="Boiler Burner",
161  section="thermostat",
162  measurement="burner",
163  icon="mdi:fire",
164  cls=ToonBoilerBinarySensor,
165  ),
167  key="thermostat_info_error_found_255",
168  name="Boiler Status",
169  section="thermostat",
170  measurement="error_found",
171  device_class=BinarySensorDeviceClass.PROBLEM,
172  icon="mdi:alert",
173  cls=ToonBoilerBinarySensor,
174  ),
176  key="thermostat_info_ot_communication_error_0",
177  name="OpenTherm Connection",
178  section="thermostat",
179  measurement="opentherm_communication_error",
180  device_class=BinarySensorDeviceClass.PROBLEM,
181  icon="mdi:check-network-outline",
182  entity_registry_enabled_default=False,
183  cls=ToonBoilerBinarySensor,
184  ),
185 )
None __init__(self, ToonDataUpdateCoordinator coordinator, ToonBinarySensorEntityDescription description)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)