Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Lektrico binary sensors entities."""
2 
3 from collections.abc import Callable
4 from dataclasses import dataclass
5 from typing import Any
6 
8  BinarySensorDeviceClass,
9  BinarySensorEntity,
10  BinarySensorEntityDescription,
11 )
12 from homeassistant.const import ATTR_SERIAL_NUMBER, CONF_TYPE, EntityCategory
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from . import LektricoConfigEntry, LektricoDeviceDataUpdateCoordinator
17 from .entity import LektricoEntity
18 
19 
20 @dataclass(frozen=True, kw_only=True)
22  """Describes Lektrico binary sensor entity."""
23 
24  value_fn: Callable[[dict[str, Any]], bool]
25 
26 
27 BINARY_SENSORS: tuple[LektricoBinarySensorEntityDescription, ...] = (
29  key="state_e_activated",
30  translation_key="state_e_activated",
31  entity_category=EntityCategory.DIAGNOSTIC,
32  device_class=BinarySensorDeviceClass.PROBLEM,
33  value_fn=lambda data: bool(data["state_e_activated"]),
34  ),
36  key="overtemp",
37  translation_key="overtemp",
38  entity_category=EntityCategory.DIAGNOSTIC,
39  device_class=BinarySensorDeviceClass.PROBLEM,
40  value_fn=lambda data: bool(data["overtemp"]),
41  ),
43  key="critical_temp",
44  translation_key="critical_temp",
45  entity_category=EntityCategory.DIAGNOSTIC,
46  device_class=BinarySensorDeviceClass.PROBLEM,
47  value_fn=lambda data: bool(data["critical_temp"]),
48  ),
50  key="overcurrent",
51  translation_key="overcurrent",
52  entity_category=EntityCategory.DIAGNOSTIC,
53  device_class=BinarySensorDeviceClass.PROBLEM,
54  value_fn=lambda data: bool(data["overcurrent"]),
55  ),
57  key="meter_fault",
58  translation_key="meter_fault",
59  entity_category=EntityCategory.DIAGNOSTIC,
60  device_class=BinarySensorDeviceClass.PROBLEM,
61  value_fn=lambda data: bool(data["meter_fault"]),
62  ),
64  key="undervoltage",
65  translation_key="undervoltage",
66  entity_category=EntityCategory.DIAGNOSTIC,
67  device_class=BinarySensorDeviceClass.PROBLEM,
68  value_fn=lambda data: bool(data["undervoltage_error"]),
69  ),
71  key="overvoltage",
72  translation_key="overvoltage",
73  entity_category=EntityCategory.DIAGNOSTIC,
74  device_class=BinarySensorDeviceClass.PROBLEM,
75  value_fn=lambda data: bool(data["overvoltage_error"]),
76  ),
78  key="rcd_error",
79  translation_key="rcd_error",
80  entity_category=EntityCategory.DIAGNOSTIC,
81  device_class=BinarySensorDeviceClass.PROBLEM,
82  value_fn=lambda data: bool(data["rcd_error"]),
83  ),
85  key="cp_diode_failure",
86  translation_key="cp_diode_failure",
87  entity_category=EntityCategory.DIAGNOSTIC,
88  device_class=BinarySensorDeviceClass.PROBLEM,
89  value_fn=lambda data: bool(data["cp_diode_failure"]),
90  ),
92  key="contactor_failure",
93  translation_key="contactor_failure",
94  entity_category=EntityCategory.DIAGNOSTIC,
95  device_class=BinarySensorDeviceClass.PROBLEM,
96  value_fn=lambda data: bool(data["contactor_failure"]),
97  ),
98 )
99 
100 
102  hass: HomeAssistant,
103  entry: LektricoConfigEntry,
104  async_add_entities: AddEntitiesCallback,
105 ) -> None:
106  """Set up Lektrico binary sensor entities based on a config entry."""
107  coordinator = entry.runtime_data
108 
111  description,
112  coordinator,
113  f"{entry.data[CONF_TYPE]}_{entry.data[ATTR_SERIAL_NUMBER]}",
114  )
115  for description in BINARY_SENSORS
116  )
117 
118 
120  """Defines a Lektrico binary sensor entity."""
121 
122  entity_description: LektricoBinarySensorEntityDescription
123 
124  def __init__(
125  self,
126  description: LektricoBinarySensorEntityDescription,
127  coordinator: LektricoDeviceDataUpdateCoordinator,
128  device_name: str,
129  ) -> None:
130  """Initialize Lektrico binary sensor."""
131  super().__init__(coordinator, device_name)
132  self.entity_descriptionentity_description = description
133  self._coordinator_coordinator = coordinator
134  self._attr_unique_id_attr_unique_id = f"{coordinator.serial_number}_{description.key}"
135 
136  @property
137  def is_on(self) -> bool:
138  """Return the state of the binary sensor."""
139  return self.entity_descriptionentity_description.value_fn(self.coordinator.data)
None __init__(self, LektricoBinarySensorEntityDescription description, LektricoDeviceDataUpdateCoordinator coordinator, str device_name)
None async_setup_entry(HomeAssistant hass, LektricoConfigEntry entry, AddEntitiesCallback async_add_entities)