-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
/
Copy pathbinary_sensor.py
100 lines (81 loc) · 3.44 KB
/
binary_sensor.py
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
"""Binary sensor platform for Weheat integration."""
from collections.abc import Callable
from dataclasses import dataclass
from weheat.abstractions.heat_pump import HeatPump
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from . import WeheatConfigEntry
from .coordinator import WeheatDataUpdateCoordinator
from .entity import WeheatEntity
@dataclass(frozen=True, kw_only=True)
class WeHeatBinarySensorEntityDescription(BinarySensorEntityDescription):
"""Describes Weheat binary sensor entity."""
value_fn: Callable[[HeatPump], StateType]
BINARY_SENSORS = [
WeHeatBinarySensorEntityDescription(
translation_key="indoor_unit_water_pump_state",
key="indoor_unit_water_pump_state",
device_class=BinarySensorDeviceClass.RUNNING,
value_fn=lambda status: status.indoor_unit_water_pump_state,
),
WeHeatBinarySensorEntityDescription(
translation_key="indoor_unit_auxiliary_pump_state",
key="indoor_unit_auxiliary_pump_state",
device_class=BinarySensorDeviceClass.RUNNING,
value_fn=lambda status: status.indoor_unit_auxiliary_pump_state,
),
WeHeatBinarySensorEntityDescription(
translation_key="indoor_unit_dhw_valve_or_pump_state",
key="indoor_unit_dhw_valve_or_pump_state",
device_class=BinarySensorDeviceClass.RUNNING,
value_fn=lambda status: status.indoor_unit_dhw_valve_or_pump_state,
),
WeHeatBinarySensorEntityDescription(
translation_key="indoor_unit_gas_boiler_state",
key="indoor_unit_gas_boiler_state",
value_fn=lambda status: status.indoor_unit_gas_boiler_state,
),
WeHeatBinarySensorEntityDescription(
translation_key="indoor_unit_electric_heater_state",
key="indoor_unit_electric_heater_state",
device_class=BinarySensorDeviceClass.RUNNING,
value_fn=lambda status: status.indoor_unit_electric_heater_state,
),
]
async def async_setup_entry(
hass: HomeAssistant,
entry: WeheatConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the sensors for weheat heat pump."""
entities = [
WeheatHeatPumpBinarySensor(coordinator, entity_description)
for entity_description in BINARY_SENSORS
for coordinator in entry.runtime_data
if entity_description.value_fn(coordinator.data) is not None
]
async_add_entities(entities)
class WeheatHeatPumpBinarySensor(WeheatEntity, BinarySensorEntity):
"""Defines a Weheat heat pump binary sensor."""
coordinator: WeheatDataUpdateCoordinator
entity_description: WeHeatBinarySensorEntityDescription
def __init__(
self,
coordinator: WeheatDataUpdateCoordinator,
entity_description: WeHeatBinarySensorEntityDescription,
) -> None:
"""Pass coordinator to CoordinatorEntity."""
super().__init__(coordinator)
self.entity_description = entity_description
self._attr_unique_id = f"{coordinator.heatpump_id}_{entity_description.key}"
@property
def is_on(self) -> bool | None:
"""Return True if the binary sensor is on."""
value = self.entity_description.value_fn(self.coordinator.data)
return bool(value) if value is not None else None