forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_sensor.py
More file actions
156 lines (131 loc) · 5.39 KB
/
binary_sensor.py
File metadata and controls
156 lines (131 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""Binary sensor platform for Indevolt integration."""
from dataclasses import dataclass, field
from typing import Final
from indevolt_api import IndevoltBattery, IndevoltGrid, IndevoltSystem
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import IndevoltConfigEntry
from .coordinator import IndevoltCoordinator
from .entity import IndevoltEntity
PARALLEL_UPDATES = 0
@dataclass(frozen=True, kw_only=True)
class IndevoltBinarySensorEntityDescription(BinarySensorEntityDescription):
"""Custom entity description class for Indevolt binary sensors."""
on_value: int = 1
off_value: int = 0
generation: list[int] = field(default_factory=lambda: [1, 2])
BINARY_SENSORS: Final = (
# Electricity Meter Status
IndevoltBinarySensorEntityDescription(
key=IndevoltGrid.METER_CONNECTED,
translation_key="meter_connected",
on_value=1000,
off_value=1001,
device_class=BinarySensorDeviceClass.CONNECTIVITY,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
),
# Electric Heating States
IndevoltBinarySensorEntityDescription(
key=IndevoltSystem.HEATING_STATE,
generation=[1],
translation_key="electric_heating_state",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
),
IndevoltBinarySensorEntityDescription(
key=IndevoltBattery.MAIN_HEATING_STATE,
generation=[2],
translation_key="main_electric_heating_state",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
),
IndevoltBinarySensorEntityDescription(
key=IndevoltBattery.PACK_1_HEATING_STATE,
generation=[2],
translation_key="battery_pack_1_electric_heating_state",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
),
IndevoltBinarySensorEntityDescription(
key=IndevoltBattery.PACK_2_HEATING_STATE,
generation=[2],
translation_key="battery_pack_2_electric_heating_state",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
),
IndevoltBinarySensorEntityDescription(
key=IndevoltBattery.PACK_3_HEATING_STATE,
generation=[2],
translation_key="battery_pack_3_electric_heating_state",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
),
IndevoltBinarySensorEntityDescription(
key=IndevoltBattery.PACK_4_HEATING_STATE,
generation=[2],
translation_key="battery_pack_4_electric_heating_state",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
),
IndevoltBinarySensorEntityDescription(
key=IndevoltBattery.PACK_5_HEATING_STATE,
generation=[2],
translation_key="battery_pack_5_electric_heating_state",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
),
)
# Sensor per battery pack: (serial_number_key, heating_state_key)
BATTERY_PACK_SENSOR_KEYS = [
(IndevoltBattery.PACK_1_SERIAL_NUMBER, IndevoltBattery.PACK_1_HEATING_STATE),
(IndevoltBattery.PACK_2_SERIAL_NUMBER, IndevoltBattery.PACK_2_HEATING_STATE),
(IndevoltBattery.PACK_3_SERIAL_NUMBER, IndevoltBattery.PACK_3_HEATING_STATE),
(IndevoltBattery.PACK_4_SERIAL_NUMBER, IndevoltBattery.PACK_4_HEATING_STATE),
(IndevoltBattery.PACK_5_SERIAL_NUMBER, IndevoltBattery.PACK_5_HEATING_STATE),
]
async def async_setup_entry(
hass: HomeAssistant,
entry: IndevoltConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up the binary sensor platform for Indevolt."""
coordinator = entry.runtime_data
device_gen = coordinator.generation
excluded_keys: set[str] = set()
for pack_keys in BATTERY_PACK_SENSOR_KEYS:
sn_key = pack_keys[0]
if not coordinator.data.get(sn_key):
excluded_keys.update(pack_keys)
async_add_entities(
IndevoltBinarySensorEntity(coordinator, description)
for description in BINARY_SENSORS
if device_gen in description.generation and description.key not in excluded_keys
)
class IndevoltBinarySensorEntity(IndevoltEntity, BinarySensorEntity):
"""Represents a binary sensor entity for Indevolt devices."""
entity_description: IndevoltBinarySensorEntityDescription
def __init__(
self,
coordinator: IndevoltCoordinator,
description: IndevoltBinarySensorEntityDescription,
) -> None:
"""Initialize the Indevolt binary sensor entity."""
super().__init__(coordinator)
self.entity_description = description
self._attr_unique_id = f"{self.serial_number}_{description.key}"
@property
def is_on(self) -> bool | None:
"""Return on/active state of the binary sensor."""
raw_value = self.coordinator.data.get(self.entity_description.key)
if raw_value == self.entity_description.on_value:
return True
if raw_value == self.entity_description.off_value:
return False
return None