-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsensor.py
275 lines (248 loc) · 9.32 KB
/
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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"""Support for MelCloud device sensors."""
from __future__ import annotations
from dataclasses import dataclass
import logging
from typing import Any, Callable
from pymelcloud import DEVICE_TYPE_ATA, DEVICE_TYPE_ATW
from pymelcloud.atw_device import Zone
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
UnitOfEnergy,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import MelCloudDevice
from .const import DOMAIN, MEL_DEVICES
@dataclass
class MelcloudRequiredKeysMixin:
"""Mixin for required keys."""
value_fn: Callable[[Any], float]
enabled: Callable[[Any], bool]
@dataclass
class MelcloudSensorEntityDescription(
SensorEntityDescription, MelcloudRequiredKeysMixin
):
"""Describes Melcloud sensor entity."""
ATA_SENSORS: tuple[MelcloudSensorEntityDescription, ...] = (
MelcloudSensorEntityDescription(
key="wifi_signal",
name="WiFi Signal",
icon="mdi:signal",
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda x: x.wifi_signal,
enabled=lambda x: True,
entity_registry_enabled_default=False,
),
MelcloudSensorEntityDescription(
key="room_temperature",
name="Room Temperature",
icon="mdi:thermometer",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda x: x.device.room_temperature,
enabled=lambda x: True,
entity_registry_enabled_default=False,
),
MelcloudSensorEntityDescription(
key="energy",
name="Energy",
icon="mdi:factory",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda x: x.device.total_energy_consumed,
enabled=lambda x: x.device.has_energy_consumed_meter,
entity_registry_enabled_default=False,
),
)
ATW_SENSORS: tuple[MelcloudSensorEntityDescription, ...] = (
MelcloudSensorEntityDescription(
key="daily_heating_energy_consumed",
name="Daily Heating Energy Consumed",
icon="mdi:heat-pump",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
value_fn=lambda x: x.daily_heating_energy_consumed,
enabled=lambda x: True,
entity_registry_enabled_default=False,
),
MelcloudSensorEntityDescription(
key="daily_cooling_energy_consumed",
name="Daily Cooling Energy Consumed",
icon="mdi:sun-snowflake",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
value_fn=lambda x: x.daily_cooling_energy_consumed,
enabled=lambda x: True,
entity_registry_enabled_default=False,
),
MelcloudSensorEntityDescription(
key="daily_heating_energy_produced",
name="Daily Heating Energy Produced",
icon="mdi:factory",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
value_fn=lambda x: x.daily_heating_energy_produced,
enabled=lambda x: True,
entity_registry_enabled_default=False,
),
MelcloudSensorEntityDescription(
key="daily_cooling_energy_produced",
name="Daily Cooling Energy Produced",
icon="mdi:factory",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
value_fn=lambda x: x.daily_cooling_energy_produced,
enabled=lambda x: True,
entity_registry_enabled_default=False,
),
MelcloudSensorEntityDescription(
key="wifi_signal",
name="WiFi Signal",
icon="mdi:signal",
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
value_fn=lambda x: x.wifi_signal,
enabled=lambda x: True,
entity_registry_enabled_default=False,
),
MelcloudSensorEntityDescription(
key="outside_temperature",
name="Outside Temperature",
icon="mdi:thermometer",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda x: x.device.outside_temperature,
enabled=lambda x: True,
entity_registry_enabled_default=False,
),
MelcloudSensorEntityDescription(
key="tank_temperature",
name="Tank Temperature",
icon="mdi:thermometer",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda x: x.device.tank_temperature,
enabled=lambda x: x.device_info.get("HasHotWaterTank", False),
),
)
ATW_ZONE_SENSORS: tuple[MelcloudSensorEntityDescription, ...] = (
MelcloudSensorEntityDescription(
key="room_temperature",
name="Room Temperature",
icon="mdi:thermometer",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda zone: zone.room_temperature,
enabled=lambda x: True,
entity_registry_enabled_default=False,
),
MelcloudSensorEntityDescription(
key="flow_temperature",
name="Flow Temperature",
icon="mdi:thermometer",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda zone: zone.flow_temperature,
enabled=lambda x: True,
entity_registry_enabled_default=False,
),
MelcloudSensorEntityDescription(
key="return_temperature",
name="Flow Return Temperature",
icon="mdi:thermometer",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda zone: zone.return_temperature,
enabled=lambda x: True,
entity_registry_enabled_default=False,
),
)
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
):
"""Set up MELCloud device sensors based on config_entry."""
entry_config = hass.data[DOMAIN][entry.entry_id]
mel_devices = entry_config.get(MEL_DEVICES)
entities = []
entities.extend(
[
MelDeviceSensor(mel_device, description)
for description in ATA_SENSORS
for mel_device in mel_devices[DEVICE_TYPE_ATA]
if description.enabled(mel_device)
]
+ [
MelDeviceSensor(mel_device, description)
for description in ATW_SENSORS
for mel_device in mel_devices[DEVICE_TYPE_ATW]
if description.enabled(mel_device)
]
)
entities.extend(
[
AtwZoneSensor(mel_device, zone, description)
for mel_device in mel_devices[DEVICE_TYPE_ATW]
for zone in mel_device.device.zones
for description in ATW_ZONE_SENSORS
if description.enabled(zone)
]
)
async_add_entities(entities, False)
class MelDeviceSensor(CoordinatorEntity, SensorEntity):
"""Representation of a Sensor."""
entity_description: MelcloudSensorEntityDescription
_attr_has_entity_name = True
def __init__(
self,
api: MelCloudDevice,
description: MelcloudSensorEntityDescription,
) -> None:
"""Initialize the sensor."""
super().__init__(api.coordinator)
self._api = api
self.entity_description = description
self._attr_unique_id = f"{api.device.serial}-{api.device.mac}-{description.key}"
self._attr_device_info = api.device_info
self._attr_extra_state_attributes = api.extra_attributes
@property
def native_value(self):
"""Return the state of the sensor."""
return self.entity_description.value_fn(self._api)
class AtwZoneSensor(MelDeviceSensor):
"""Air-to-Air device sensor."""
def __init__(
self,
api: MelCloudDevice,
zone: Zone,
description: MelcloudSensorEntityDescription,
) -> None:
"""Initialize the sensor."""
if zone.zone_index != 1:
description.key = f"{description.key}-zone-{zone.zone_index}"
super().__init__(api, description)
self._attr_device_info = api.zone_device_info(zone)
self._zone = zone
@property
def native_value(self):
"""Return zone based state."""
return self.entity_description.value_fn(self._zone)