-
Notifications
You must be signed in to change notification settings - Fork 2
/
sensor.py
executable file
·236 lines (216 loc) · 7.09 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
"""Support for Nexia / Trane XL Thermostats."""
from __future__ import annotations
from .nexiaux360.const import UNIT_CELSIUS
from .nexiaux360.thermostat import NexiaThermostat
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorStateClass,
)
from homeassistant.const import PERCENTAGE, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .entity import NexiaThermostatEntity, NexiaThermostatZoneEntity
from .types import NexiaConfigEntry
from .util import percent_conv
async def async_setup_entry(
hass: HomeAssistant,
config_entry: NexiaConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up sensors for a Nexia device."""
coordinator = config_entry.runtime_data
nexia_home = coordinator.nexia_home
entities: list[NexiaThermostatEntity] = []
# Thermostat / System Sensors
for thermostat_id in nexia_home.get_thermostat_ids():
thermostat: NexiaThermostat = nexia_home.get_thermostat_by_id(thermostat_id)
entities.append(
NexiaThermostatSensor(
coordinator,
thermostat,
"get_system_status",
"system_status",
None,
None,
None,
)
)
# Air cleaner
entities.append(
NexiaThermostatSensor(
coordinator,
thermostat,
"get_air_cleaner_mode",
"air_cleaner_mode",
None,
None,
None,
)
)
# Compressor Speed
if thermostat.has_variable_speed_compressor():
entities.append(
NexiaThermostatSensor(
coordinator,
thermostat,
"get_current_compressor_speed",
"current_compressor_speed",
None,
PERCENTAGE,
SensorStateClass.MEASUREMENT,
percent_conv,
)
)
entities.append(
NexiaThermostatSensor(
coordinator,
thermostat,
"get_requested_compressor_speed",
"requested_compressor_speed",
None,
PERCENTAGE,
SensorStateClass.MEASUREMENT,
percent_conv,
)
)
# Outdoor Temperature
if thermostat.has_outdoor_temperature():
if thermostat.get_unit() == UNIT_CELSIUS:
unit = UnitOfTemperature.CELSIUS
else:
unit = UnitOfTemperature.FAHRENHEIT
entities.append(
NexiaThermostatSensor(
coordinator,
thermostat,
"get_outdoor_temperature",
"outdoor_temperature",
SensorDeviceClass.TEMPERATURE,
unit,
SensorStateClass.MEASUREMENT,
)
)
# Relative Humidity
if thermostat.has_relative_humidity():
entities.append(
NexiaThermostatSensor(
coordinator,
thermostat,
"get_relative_humidity",
None,
SensorDeviceClass.HUMIDITY,
PERCENTAGE,
SensorStateClass.MEASUREMENT,
percent_conv,
)
)
# Zone Sensors
for zone_id in thermostat.get_zone_ids():
zone = thermostat.get_zone_by_id(zone_id)
if thermostat.get_unit() == UNIT_CELSIUS:
unit = UnitOfTemperature.CELSIUS
else:
unit = UnitOfTemperature.FAHRENHEIT
# Temperature
entities.append(
NexiaThermostatZoneSensor(
coordinator,
zone,
"get_temperature",
None,
SensorDeviceClass.TEMPERATURE,
unit,
SensorStateClass.MEASUREMENT,
None,
)
)
# Zone Status
entities.append(
NexiaThermostatZoneSensor(
coordinator, zone, "get_status", "zone_status", None, None, None
)
)
# Setpoint Status
entities.append(
NexiaThermostatZoneSensor(
coordinator,
zone,
"get_setpoint_status",
"zone_setpoint_status",
None,
None,
None,
)
)
async_add_entities(entities)
class NexiaThermostatSensor(NexiaThermostatEntity, SensorEntity):
"""Provides Nexia thermostat sensor support."""
def __init__(
self,
coordinator,
thermostat,
sensor_call,
translation_key,
sensor_class,
sensor_unit,
state_class,
modifier=None,
):
"""Initialize the sensor."""
super().__init__(
coordinator,
thermostat,
unique_id=f"{thermostat.thermostat_id}_{sensor_call}",
)
self._call = sensor_call
self._modifier = modifier
self._attr_device_class = sensor_class
self._attr_native_unit_of_measurement = sensor_unit
self._attr_state_class = state_class
if translation_key is not None:
self._attr_translation_key = translation_key
@property
def native_value(self):
"""Return the state of the sensor."""
val = getattr(self._thermostat, self._call)()
if self._modifier:
val = self._modifier(val)
if isinstance(val, float):
val = round(val, 1)
return val
class NexiaThermostatZoneSensor(NexiaThermostatZoneEntity, SensorEntity):
"""Nexia Zone Sensor Support."""
def __init__(
self,
coordinator,
zone,
sensor_call,
translation_key,
sensor_class,
sensor_unit,
state_class,
modifier=None,
):
"""Create a zone sensor."""
super().__init__(
coordinator,
zone,
unique_id=f"{zone.thermostat_id}_{zone.zone_id}_{sensor_call}",
)
self._call = sensor_call
self._modifier = modifier
self._attr_device_class = sensor_class
self._attr_native_unit_of_measurement = sensor_unit
self._attr_state_class = state_class
if translation_key is not None:
self._attr_translation_key = translation_key
@property
def native_value(self):
"""Return the state of the sensor."""
val = getattr(self._zone, self._call)()
if self._modifier:
val = self._modifier(val)
if isinstance(val, float):
val = round(val, 1)
return val