Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """The read-only binary sensors for APsystems local API integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 from APsystemsEZ1 import ReturnAlarmInfo
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 from homeassistant.helpers.update_coordinator import CoordinatorEntity
19 
20 from . import ApSystemsConfigEntry, ApSystemsData
21 from .coordinator import ApSystemsDataCoordinator
22 from .entity import ApSystemsEntity
23 
24 
25 @dataclass(frozen=True, kw_only=True)
27  """Describes Apsystens Inverter binary sensor entity."""
28 
29  is_on: Callable[[ReturnAlarmInfo], bool | None]
30 
31 
32 BINARY_SENSORS: tuple[ApsystemsLocalApiBinarySensorDescription, ...] = (
34  key="off_grid_status",
35  translation_key="off_grid_status",
36  device_class=BinarySensorDeviceClass.PROBLEM,
37  entity_category=EntityCategory.DIAGNOSTIC,
38  is_on=lambda c: c.offgrid,
39  ),
41  key="dc_1_short_circuit_error_status",
42  translation_key="dc_1_short_circuit_error_status",
43  device_class=BinarySensorDeviceClass.PROBLEM,
44  entity_category=EntityCategory.DIAGNOSTIC,
45  is_on=lambda c: c.shortcircuit_1,
46  ),
48  key="dc_2_short_circuit_error_status",
49  translation_key="dc_2_short_circuit_error_status",
50  device_class=BinarySensorDeviceClass.PROBLEM,
51  entity_category=EntityCategory.DIAGNOSTIC,
52  is_on=lambda c: c.shortcircuit_2,
53  ),
55  key="output_fault_status",
56  translation_key="output_fault_status",
57  device_class=BinarySensorDeviceClass.PROBLEM,
58  entity_category=EntityCategory.DIAGNOSTIC,
59  is_on=lambda c: not c.operating,
60  ),
61 )
62 
63 
65  hass: HomeAssistant,
66  config_entry: ApSystemsConfigEntry,
67  add_entities: AddEntitiesCallback,
68 ) -> None:
69  """Set up the binary sensor platform."""
70  config = config_entry.runtime_data
71 
74  data=config,
75  entity_description=desc,
76  )
77  for desc in BINARY_SENSORS
78  )
79 
80 
82  CoordinatorEntity[ApSystemsDataCoordinator], ApSystemsEntity, BinarySensorEntity
83 ):
84  """Base binary sensor to be used with description."""
85 
86  entity_description: ApsystemsLocalApiBinarySensorDescription
87 
88  def __init__(
89  self,
90  data: ApSystemsData,
91  entity_description: ApsystemsLocalApiBinarySensorDescription,
92  ) -> None:
93  """Initialize the sensor."""
94  super().__init__(data.coordinator)
95  ApSystemsEntity.__init__(self, data)
96  self.entity_descriptionentity_description = entity_description
97  self._attr_unique_id_attr_unique_id = f"{data.device_id}_{entity_description.key}"
98 
99  @property
100  def is_on(self) -> bool | None:
101  """Return value of sensor."""
102  return self.entity_descriptionentity_description.is_on(self.coordinator.data.alarm_info)
None __init__(self, ApSystemsData data, ApsystemsLocalApiBinarySensorDescription entity_description)
None async_setup_entry(HomeAssistant hass, ApSystemsConfigEntry config_entry, AddEntitiesCallback add_entities)
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)