forked from erwindouna/python-melcloud
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathata_device.py
More file actions
380 lines (304 loc) · 11.6 KB
/
Copy pathata_device.py
File metadata and controls
380 lines (304 loc) · 11.6 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
"""Air-To-Air (DeviceType=0) device definition."""
from datetime import timedelta
from typing import Any, Dict, List, Optional
from pymelcloud.client import Client
from pymelcloud.device import EFFECTIVE_FLAGS, Device
PROPERTY_TARGET_TEMPERATURE = "target_temperature"
PROPERTY_OPERATION_MODE = "operation_mode"
PROPERTY_FAN_SPEED = "fan_speed"
PROPERTY_VANE_HORIZONTAL = "vane_horizontal"
PROPERTY_VANE_VERTICAL = "vane_vertical"
FAN_SPEED_AUTO = "auto"
OPERATION_MODE_HEAT = "heat"
OPERATION_MODE_DRY = "dry"
OPERATION_MODE_COOL = "cool"
OPERATION_MODE_FAN_ONLY = "fan_only"
OPERATION_MODE_HEAT_COOL = "heat_cool"
OPERATION_MODE_UNDEFINED = "undefined"
_OPERATION_MODE_LOOKUP = {
1: OPERATION_MODE_HEAT,
2: OPERATION_MODE_DRY,
3: OPERATION_MODE_COOL,
7: OPERATION_MODE_FAN_ONLY,
8: OPERATION_MODE_HEAT_COOL,
}
_OPERATION_MODE_MIN_TEMP_LOOKUP = {
OPERATION_MODE_HEAT: "MinTempHeat",
OPERATION_MODE_DRY: "MinTempCoolDry",
OPERATION_MODE_COOL: "MinTempCoolDry",
OPERATION_MODE_FAN_ONLY: "MinTempHeat", # Fake it just in case.
OPERATION_MODE_HEAT_COOL: "MinTempAutomatic",
OPERATION_MODE_UNDEFINED: "MinTempHeat",
}
_OPERATION_MODE_MAX_TEMP_LOOKUP = {
OPERATION_MODE_HEAT: "MaxTempHeat",
OPERATION_MODE_DRY: "MaxTempCoolDry",
OPERATION_MODE_COOL: "MaxTempCoolDry",
OPERATION_MODE_FAN_ONLY: "MaxTempHeat", # Fake it just in case.
OPERATION_MODE_HEAT_COOL: "MaxTempAutomatic",
OPERATION_MODE_UNDEFINED: "MaxTempHeat",
}
V_VANE_POSITION_AUTO = "auto"
V_VANE_POSITION_1 = "1_up"
V_VANE_POSITION_2 = "2"
V_VANE_POSITION_3 = "3"
V_VANE_POSITION_4 = "4"
V_VANE_POSITION_5 = "5_down"
V_VANE_POSITION_SWING = "swing"
V_VANE_POSITION_UNDEFINED = "undefined"
H_VANE_POSITION_AUTO = "auto"
H_VANE_POSITION_1 = "1_left"
H_VANE_POSITION_2 = "2"
H_VANE_POSITION_3 = "3"
H_VANE_POSITION_4 = "4"
H_VANE_POSITION_5 = "5_right"
H_VANE_POSITION_SPLIT = "split"
H_VANE_POSITION_SWING = "swing"
H_VANE_POSITION_UNDEFINED = "undefined"
def _fan_speed_from(speed: int) -> str:
if speed == 0:
return FAN_SPEED_AUTO
return str(speed)
def _fan_speed_to(speed: str) -> int:
if speed == FAN_SPEED_AUTO:
return 0
return int(speed)
def _operation_mode_from(mode: int) -> str:
return _OPERATION_MODE_LOOKUP.get(mode, OPERATION_MODE_UNDEFINED)
def _operation_mode_to(mode: str) -> int:
for k, value in _OPERATION_MODE_LOOKUP.items():
if value == mode:
return k
raise ValueError(f"Invalid operation_mode [{mode}]")
_H_VANE_POSITION_LOOKUP = {
0: H_VANE_POSITION_AUTO,
1: H_VANE_POSITION_1,
2: H_VANE_POSITION_2,
3: H_VANE_POSITION_3,
4: H_VANE_POSITION_4,
5: H_VANE_POSITION_5,
8: H_VANE_POSITION_SPLIT,
12: H_VANE_POSITION_SWING,
}
def _horizontal_vane_from(position: int) -> str:
return _H_VANE_POSITION_LOOKUP.get(position, H_VANE_POSITION_UNDEFINED)
def _horizontal_vane_to(position: str) -> int:
for k, value in _H_VANE_POSITION_LOOKUP.items():
if value == position:
return k
raise ValueError(f"Invalid horizontal vane position [{position}]")
_V_VANE_POSITION_LOOKUP = {
0: V_VANE_POSITION_AUTO,
1: V_VANE_POSITION_1,
2: V_VANE_POSITION_2,
3: V_VANE_POSITION_3,
4: V_VANE_POSITION_4,
5: V_VANE_POSITION_5,
7: V_VANE_POSITION_SWING,
}
def _vertical_vane_from(position: int) -> str:
return _V_VANE_POSITION_LOOKUP.get(position, V_VANE_POSITION_UNDEFINED)
def _vertical_vane_to(position: str) -> int:
for k, value in _V_VANE_POSITION_LOOKUP.items():
if value == position:
return k
raise ValueError(f"Invalid vertical vane position [{position}]")
class AtaDevice(Device):
"""Air-to-Air device."""
def __init__(
self,
device_conf: Dict[str, Any],
client: Client,
set_debounce=timedelta(seconds=1),
):
"""Initialize an ATA device."""
super().__init__(device_conf, client, set_debounce)
self.last_energy_value = None
def apply_write(self, state: Dict[str, Any], key: str, value: Any):
"""Apply writes to state object.
Used for property validation, do not modify device state.
"""
flags = state.get(EFFECTIVE_FLAGS, 0)
if key == PROPERTY_TARGET_TEMPERATURE:
state["SetTemperature"] = self.round_temperature(value)
flags = flags | 0x04
elif key == PROPERTY_OPERATION_MODE:
state["OperationMode"] = _operation_mode_to(value)
flags = flags | 0x02
elif key == PROPERTY_FAN_SPEED:
state["SetFanSpeed"] = _fan_speed_to(value)
flags = flags | 0x08
elif key == PROPERTY_VANE_HORIZONTAL:
state["VaneHorizontal"] = _horizontal_vane_to(value)
flags = flags | 0x100
elif key == PROPERTY_VANE_VERTICAL:
state["VaneVertical"] = _vertical_vane_to(value)
flags = flags | 0x10
else:
raise ValueError(f"Cannot set {key}, invalid property")
state[EFFECTIVE_FLAGS] = flags
@property
def has_energy_consumed_meter(self) -> bool:
"""Return True if the device has an energy consumption meter."""
return self._device_conf.get("Device", {}).get("HasEnergyConsumedMeter", False)
@property
def total_energy_consumed(self) -> Optional[float]:
"""Return total consumed energy as kWh.
The update interval is extremely slow and inconsistent. Empirical evidence
suggests that it can vary between 1h 30min and 3h.
"""
if self._device_conf is None:
return None
device = self._device_conf.get("Device", {})
value = device.get("CurrentEnergyConsumed", None)
if value is None:
return None
if value == 0.0:
return self.last_energy_value
self.last_energy_value = value / 1000.0
return self.last_energy_value
@property
def room_temperature(self) -> Optional[float]:
"""Return room temperature reported by the device."""
if self._state is None:
return None
return self._state.get("RoomTemperature")
@property
def target_temperature(self) -> Optional[float]:
"""Return target temperature set for the device."""
if self._state is None:
return None
return self._state.get("SetTemperature")
@property
def target_temperature_step(self) -> float:
"""Return target temperature set precision."""
return self.temperature_increment
@property
def target_temperature_min(self) -> Optional[float]:
"""Return maximum target temperature for the currently active operation mode."""
if self._state is None:
return None
return self._device_conf.get("Device", {}).get(
_OPERATION_MODE_MIN_TEMP_LOOKUP.get(self.operation_mode), 10
)
@property
def target_temperature_max(self) -> Optional[float]:
"""Return maximum target temperature for the currently active operation mode."""
if self._state is None:
return None
return self._device_conf.get("Device", {}).get(
_OPERATION_MODE_MAX_TEMP_LOOKUP.get(self.operation_mode), 31
)
@property
def operation_mode(self) -> str:
"""Return currently active operation mode."""
if self._state is None:
return OPERATION_MODE_UNDEFINED
return _operation_mode_from(self._state.get("OperationMode", -1))
@property
def operation_modes(self) -> List[str]:
"""Return available operation modes."""
modes: List[str] = []
conf_dev = self._device_conf.get("Device", {})
if conf_dev.get("CanHeat", False):
modes.append(OPERATION_MODE_HEAT)
if conf_dev.get("CanDry", False):
modes.append(OPERATION_MODE_DRY)
if conf_dev.get("CanCool", False):
modes.append(OPERATION_MODE_COOL)
modes.append(OPERATION_MODE_FAN_ONLY)
if conf_dev.get("ModelSupportsAuto", False):
modes.append(OPERATION_MODE_HEAT_COOL)
return modes
@property
def fan_speed(self) -> Optional[str]:
"""Return currently active fan speed.
The argument must be on of the fan speeds returned by fan_speeds.
"""
if self._state is None:
return None
return _fan_speed_from(self._state.get("SetFanSpeed"))
@property
def fan_speeds(self) -> Optional[List[str]]:
"""Return available fan speeds.
The supported fan speeds vary from device to device. The available modes are
read from the Device capability attributes.
For example, a 5 speed device with auto fan speed would produce the following
list (formatted '"[pymelcloud]" -- "[device controls]"')
- "auto" -- "auto"
- "1" -- "silent"
- "2" -- "1"
- "3" -- "2"
- "4" -- "3"
- "5" -- "4"
MELCloud is not aware of the device type making it infeasible to match the
fan speed names with the device documentation.
"""
if self._state is None:
return None
speeds = []
if self._device_conf.get("Device", {}).get("HasAutomaticFanSpeed", False):
speeds.append(FAN_SPEED_AUTO)
num_fan_speeds = self._state.get("NumberOfFanSpeeds", 0)
for num in range(1, num_fan_speeds + 1):
speeds.append(_fan_speed_from(num))
return speeds
@property
def vane_horizontal(self) -> Optional[str]:
"""Return horizontal vane position."""
if self._state is None:
return None
return _horizontal_vane_from(self._state.get("VaneHorizontal"))
@property
def vane_horizontal_positions(self) -> Optional[List[str]]:
"""Return available horizontal vane positions."""
if self._device_conf.get("HideVaneControls", False):
return []
device = self._device_conf.get("Device", {})
if not device.get("ModelSupportsVaneHorizontal", False):
return []
positions = [
H_VANE_POSITION_AUTO, # ModelSupportsAuto could affect this.
H_VANE_POSITION_1,
H_VANE_POSITION_2,
H_VANE_POSITION_3,
H_VANE_POSITION_4,
H_VANE_POSITION_5,
H_VANE_POSITION_SPLIT,
]
if device.get("SwingFunction", False):
positions.append(H_VANE_POSITION_SWING)
return positions
@property
def vane_vertical(self) -> Optional[str]:
"""Return vertical vane position."""
if self._state is None:
return None
return _vertical_vane_from(self._state.get("VaneVertical"))
@property
def vane_vertical_positions(self) -> Optional[List[str]]:
"""Return available vertical vane positions."""
if self._device_conf.get("HideVaneControls", False):
return []
device = self._device_conf.get("Device", {})
if not device.get("ModelSupportsVaneVertical", False):
return []
positions = [
V_VANE_POSITION_AUTO, # ModelSupportsAuto could affect this.
V_VANE_POSITION_1,
V_VANE_POSITION_2,
V_VANE_POSITION_3,
V_VANE_POSITION_4,
V_VANE_POSITION_5,
]
if device.get("SwingFunction", False):
positions.append(V_VANE_POSITION_SWING)
return positions
@property
def actual_fan_speed(self) -> Optional[str]:
"""Return actual fan speed.
0 is stopped, not auto
"""
if self._state is None:
return None
return str(self._device_conf.get("Device", {}).get("ActualFanSpeed", -1))