Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Binary sensors for renson."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 
7 from renson_endura_delta.field_enum import (
8  AIR_QUALITY_CONTROL_FIELD,
9  BREEZE_ENABLE_FIELD,
10  BREEZE_MET_FIELD,
11  CO2_CONTROL_FIELD,
12  FROST_PROTECTION_FIELD,
13  HUMIDITY_CONTROL_FIELD,
14  PREHEATER_FIELD,
15  DataType,
16  FieldEnum,
17 )
18 from renson_endura_delta.renson import RensonVentilation
19 
21  BinarySensorEntity,
22  BinarySensorEntityDescription,
23 )
24 from homeassistant.config_entries import ConfigEntry
25 from homeassistant.const import EntityCategory
26 from homeassistant.core import HomeAssistant, callback
27 from homeassistant.helpers.entity_platform import AddEntitiesCallback
28 
29 from .const import DOMAIN
30 from .coordinator import RensonCoordinator
31 from .entity import RensonEntity
32 
33 
34 @dataclass(frozen=True, kw_only=True)
36  """Description of binary sensor."""
37 
38  field: FieldEnum
39 
40 
41 BINARY_SENSORS: tuple[RensonBinarySensorEntityDescription, ...] = (
43  translation_key="frost_protection_active",
44  key="FROST_PROTECTION_FIELD",
45  field=FROST_PROTECTION_FIELD,
46  entity_category=EntityCategory.DIAGNOSTIC,
47  ),
49  key="BREEZE_ENABLE_FIELD",
50  translation_key="breeze",
51  field=BREEZE_ENABLE_FIELD,
52  entity_category=EntityCategory.DIAGNOSTIC,
53  ),
55  key="BREEZE_MET_FIELD",
56  translation_key="breeze_conditions_met",
57  field=BREEZE_MET_FIELD,
58  ),
60  key="HUMIDITY_CONTROL_FIELD",
61  translation_key="humidity_control",
62  field=HUMIDITY_CONTROL_FIELD,
63  entity_category=EntityCategory.DIAGNOSTIC,
64  ),
66  key="AIR_QUALITY_CONTROL_FIELD",
67  translation_key="air_quality_control",
68  field=AIR_QUALITY_CONTROL_FIELD,
69  entity_category=EntityCategory.DIAGNOSTIC,
70  ),
72  key="CO2_CONTROL_FIELD",
73  translation_key="co2_control",
74  field=CO2_CONTROL_FIELD,
75  entity_category=EntityCategory.DIAGNOSTIC,
76  ),
78  key="PREHEATER_FIELD",
79  translation_key="preheater",
80  field=PREHEATER_FIELD,
81  entity_category=EntityCategory.DIAGNOSTIC,
82  ),
83 )
84 
85 
87  hass: HomeAssistant,
88  config_entry: ConfigEntry,
89  async_add_entities: AddEntitiesCallback,
90 ) -> None:
91  """Call the Renson integration to setup."""
92 
93  api: RensonVentilation = hass.data[DOMAIN][config_entry.entry_id].api
94  coordinator: RensonCoordinator = hass.data[DOMAIN][
95  config_entry.entry_id
96  ].coordinator
97 
99  RensonBinarySensor(description, api, coordinator)
100  for description in BINARY_SENSORS
101  )
102 
103 
105  """Get sensor data from the Renson API and store it in the state of the class."""
106 
107  _attr_has_entity_name = True
108 
109  def __init__(
110  self,
111  description: RensonBinarySensorEntityDescription,
112  api: RensonVentilation,
113  coordinator: RensonCoordinator,
114  ) -> None:
115  """Initialize class."""
116  super().__init__(description.key, api, coordinator)
117 
118  self.fieldfield = description.field
119  self.entity_descriptionentity_description = description
120 
121  @callback
122  def _handle_coordinator_update(self) -> None:
123  """Handle updated data from the coordinator."""
124  all_data = self.coordinator.data
125 
126  value = self.apiapiapi.get_field_value(all_data, self.fieldfield.name)
127 
128  self._attr_is_on_attr_is_on = self.apiapiapi.parse_value(value, DataType.BOOLEAN)
129 
None __init__(self, RensonBinarySensorEntityDescription description, RensonVentilation api, RensonCoordinator coordinator)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)