-
-
Notifications
You must be signed in to change notification settings - Fork 569
/
Copy pathfan_miot.py
519 lines (446 loc) · 16.9 KB
/
fan_miot.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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
import enum
from typing import Any, Dict
import click
from miio import DeviceStatus, MiotDevice
from miio.click_common import EnumType, command, format_output
from miio.fan_common import FanException, MoveDirection, OperationMode
MODEL_FAN_P9 = "dmaker.fan.p9"
MODEL_FAN_P10 = "dmaker.fan.p10"
MODEL_FAN_P11 = "dmaker.fan.p11"
MODEL_FAN_P15 = "dmaker.fan.p15"
MODEL_FAN_P18 = "dmaker.fan.p18"
MODEL_FAN_P33 = "dmaker.fan.p33"
MODEL_FAN_1C = "dmaker.fan.1c"
MIOT_MAPPING = {
MODEL_FAN_P9: {
# Source https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:fan:0000A005:dmaker-p9:1
"power": {"siid": 2, "piid": 1},
"fan_level": {"siid": 2, "piid": 2},
"child_lock": {"siid": 3, "piid": 1},
"fan_speed": {"siid": 2, "piid": 11},
"swing_mode": {"siid": 2, "piid": 5},
"swing_mode_angle": {"siid": 2, "piid": 6},
"power_off_time": {"siid": 2, "piid": 8},
"buzzer": {"siid": 2, "piid": 7},
"light": {"siid": 2, "piid": 9},
"mode": {"siid": 2, "piid": 4},
"set_move": {"siid": 2, "piid": 10},
},
MODEL_FAN_P10: {
# Source https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:fan:0000A005:dmaker-p10:1
"power": {"siid": 2, "piid": 1},
"fan_level": {"siid": 2, "piid": 2},
"child_lock": {"siid": 3, "piid": 1},
"fan_speed": {"siid": 2, "piid": 10},
"swing_mode": {"siid": 2, "piid": 4},
"swing_mode_angle": {"siid": 2, "piid": 5},
"power_off_time": {"siid": 2, "piid": 6},
"buzzer": {"siid": 2, "piid": 8},
"light": {"siid": 2, "piid": 7},
"mode": {"siid": 2, "piid": 3},
"set_move": {"siid": 2, "piid": 9},
},
MODEL_FAN_P11: {
# Source https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:fan:0000A005:dmaker-p11:1
"power": {"siid": 2, "piid": 1},
"fan_level": {"siid": 2, "piid": 2},
"mode": {"siid": 2, "piid": 3},
"swing_mode": {"siid": 2, "piid": 4},
"swing_mode_angle": {"siid": 2, "piid": 5},
"fan_speed": {"siid": 2, "piid": 6},
"light": {"siid": 4, "piid": 1},
"buzzer": {"siid": 5, "piid": 1},
# "device_fault": {"siid": 6, "piid": 2},
"child_lock": {"siid": 7, "piid": 1},
"power_off_time": {"siid": 3, "piid": 1},
"set_move": {"siid": 6, "piid": 1},
},
MODEL_FAN_P33: {
# Source https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:fan:0000A005:dmaker-p33:1
"power": {"siid": 2, "piid": 1},
"fan_level": {"siid": 2, "piid": 2},
"mode": {"siid": 2, "piid": 3},
"swing_mode": {"siid": 2, "piid": 4},
"swing_mode_angle": {"siid": 2, "piid": 5},
"fan_speed": {"siid": 2, "piid": 6},
"light": {"siid": 4, "piid": 1},
"buzzer": {"siid": 5, "piid": 1},
# "device_fault": {"siid": 6, "piid": 2},
"child_lock": {"siid": 7, "piid": 1},
"power_off_time": {"siid": 3, "piid": 1},
"set_move": {"siid": 6, "piid": 1},
},
}
# These mappings are based on user reports and may not cover all features
MIOT_MAPPING[MODEL_FAN_P15] = MIOT_MAPPING[MODEL_FAN_P11] # see #1354
MIOT_MAPPING[MODEL_FAN_P18] = MIOT_MAPPING[MODEL_FAN_P10] # see #1341
FAN1C_MAPPINGS = {
MODEL_FAN_1C: {
# https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:fan:0000A005:dmaker-1c:1
"power": {"siid": 2, "piid": 1},
"fan_level": {"siid": 2, "piid": 2},
"child_lock": {"siid": 3, "piid": 1},
"swing_mode": {"siid": 2, "piid": 3},
"power_off_time": {"siid": 2, "piid": 10},
"buzzer": {"siid": 2, "piid": 11},
"light": {"siid": 2, "piid": 12},
"mode": {"siid": 2, "piid": 7},
}
}
SUPPORTED_ANGLES = {
MODEL_FAN_P9: [30, 60, 90, 120, 150],
MODEL_FAN_P10: [30, 60, 90, 120, 140],
MODEL_FAN_P11: [30, 60, 90, 120, 140],
MODEL_FAN_P33: [30, 60, 90, 120, 140],
}
class OperationModeMiot(enum.Enum):
Normal = 0
Nature = 1
class FanStatusMiot(DeviceStatus):
"""Container for status reports for Xiaomi Mi Smart Pedestal Fan DMaker P9/P10."""
def __init__(self, data: Dict[str, Any]) -> None:
"""
Response of a FanMiot (dmaker.fan.p10):
{
'id': 1,
'result': [
{'did': 'power', 'siid': 2, 'piid': 1, 'code': 0, 'value': False},
{'did': 'fan_level', 'siid': 2, 'piid': 2, 'code': 0, 'value': 2},
{'did': 'child_lock', 'siid': 3, 'piid': 1, 'code': 0, 'value': False},
{'did': 'fan_speed', 'siid': 2, 'piid': 10, 'code': 0, 'value': 54},
{'did': 'swing_mode', 'siid': 2, 'piid': 4, 'code': 0, 'value': False},
{'did': 'swing_mode_angle', 'siid': 2, 'piid': 5, 'code': 0, 'value': 30},
{'did': 'power_off_time', 'siid': 2, 'piid': 6, 'code': 0, 'value': 0},
{'did': 'buzzer', 'siid': 2, 'piid': 8, 'code': 0, 'value': False},
{'did': 'light', 'siid': 2, 'piid': 7, 'code': 0, 'value': True},
{'did': 'mode', 'siid': 2, 'piid': 3, 'code': 0, 'value': 0},
{'did': 'set_move', 'siid': 2, 'piid': 9, 'code': -4003}
],
'exe_time': 280
}
"""
self.data = data
@property
def power(self) -> str:
"""Power state."""
return "on" if self.data["power"] else "off"
@property
def is_on(self) -> bool:
"""True if device is currently on."""
return self.data["power"]
@property
def mode(self) -> OperationMode:
"""Operation mode."""
return OperationMode[OperationModeMiot(self.data["mode"]).name]
@property
def speed(self) -> int:
"""Speed of the motor."""
return self.data["fan_speed"]
@property
def oscillate(self) -> bool:
"""True if oscillation is enabled."""
return self.data["swing_mode"]
@property
def angle(self) -> int:
"""Oscillation angle."""
return self.data["swing_mode_angle"]
@property
def delay_off_countdown(self) -> int:
"""Countdown until turning off in minutes."""
return self.data["power_off_time"]
@property
def led(self) -> bool:
"""True if LED is turned on, if available."""
return self.data["light"]
@property
def buzzer(self) -> bool:
"""True if buzzer is turned on."""
return self.data["buzzer"]
@property
def child_lock(self) -> bool:
"""True if child lock is on."""
return self.data["child_lock"]
class FanStatus1C(DeviceStatus):
"""Container for status reports for Xiaomi Mi Smart Pedestal Fan DMaker 1C."""
def __init__(self, data: Dict[str, Any]) -> None:
"""Response of a Fan1C (dmaker.fan.1c):
{
'id': 1,
'result': [
{'did': 'power', 'siid': 2, 'piid': 1, 'code': 0, 'value': True},
{'did': 'fan_level', 'siid': 2, 'piid': 2, 'code': 0, 'value': 2},
{'did': 'child_lock', 'siid': 3, 'piid': 1, 'code': 0, 'value': False},
{'did': 'swing_mode', 'siid': 2, 'piid': 3, 'code': 0, 'value': False},
{'did': 'power_off_time', 'siid': 2, 'piid': 10, 'code': 0, 'value': 0},
{'did': 'buzzer', 'siid': 2, 'piid': 11, 'code': 0, 'value': False},
{'did': 'light', 'siid': 2, 'piid': 12, 'code': 0, 'value': True},
{'did': 'mode', 'siid': 2, 'piid': 7, 'code': 0, 'value': 0},
],
'exe_time': 280
}
"""
self.data = data
@property
def power(self) -> str:
"""Power state."""
return "on" if self.data["power"] else "off"
@property
def is_on(self) -> bool:
"""True if device is currently on."""
return self.data["power"]
@property
def mode(self) -> OperationMode:
"""Operation mode."""
return OperationMode[OperationModeMiot(self.data["mode"]).name]
@property
def speed(self) -> int:
"""Speed of the motor."""
return self.data["fan_level"]
@property
def oscillate(self) -> bool:
"""True if oscillation is enabled."""
return self.data["swing_mode"]
@property
def delay_off_countdown(self) -> int:
"""Countdown until turning off in minutes."""
return self.data["power_off_time"]
@property
def led(self) -> bool:
"""True if LED is turned on."""
return self.data["light"]
@property
def buzzer(self) -> bool:
"""True if buzzer is turned on."""
return self.data["buzzer"]
@property
def child_lock(self) -> bool:
"""True if child lock is on."""
return self.data["child_lock"]
class FanMiot(MiotDevice):
_mappings = MIOT_MAPPING
@command(
default_output=format_output(
"",
"Power: {result.power}\n"
"Operation mode: {result.mode}\n"
"Speed: {result.speed}\n"
"Oscillate: {result.oscillate}\n"
"Angle: {result.angle}\n"
"LED: {result.led}\n"
"Buzzer: {result.buzzer}\n"
"Child lock: {result.child_lock}\n"
"Power-off time: {result.delay_off_countdown}\n",
)
)
def status(self) -> FanStatusMiot:
"""Retrieve properties."""
return FanStatusMiot(
{
prop["did"]: prop["value"] if prop["code"] == 0 else None
for prop in self.get_properties_for_mapping()
}
)
@command(default_output=format_output("Powering on"))
def on(self):
"""Power on."""
return self.set_property("power", True)
@command(default_output=format_output("Powering off"))
def off(self):
"""Power off."""
return self.set_property("power", False)
@command(
click.argument("mode", type=EnumType(OperationMode)),
default_output=format_output("Setting mode to '{mode.value}'"),
)
def set_mode(self, mode: OperationMode):
"""Set mode."""
return self.set_property("mode", OperationModeMiot[mode.name].value)
@command(
click.argument("speed", type=int),
default_output=format_output("Setting speed to {speed}"),
)
def set_speed(self, speed: int):
"""Set speed."""
if speed < 0 or speed > 100:
raise FanException("Invalid speed: %s" % speed)
return self.set_property("fan_speed", speed)
@command(
click.argument("angle", type=int),
default_output=format_output("Setting angle to {angle}"),
)
def set_angle(self, angle: int):
"""Set the oscillation angle."""
if angle not in SUPPORTED_ANGLES[self.model]:
raise FanException(
"Unsupported angle. Supported values: "
+ ", ".join(f"{i}" for i in SUPPORTED_ANGLES[self.model])
)
return self.set_property("swing_mode_angle", angle)
@command(
click.argument("oscillate", type=bool),
default_output=format_output(
lambda oscillate: "Turning on oscillate"
if oscillate
else "Turning off oscillate"
),
)
def set_oscillate(self, oscillate: bool):
"""Set oscillate on/off."""
return self.set_property("swing_mode", oscillate)
@command(
click.argument("led", type=bool),
default_output=format_output(
lambda led: "Turning on LED" if led else "Turning off LED"
),
)
def set_led(self, led: bool):
"""Turn led on/off."""
return self.set_property("light", led)
@command(
click.argument("buzzer", type=bool),
default_output=format_output(
lambda buzzer: "Turning on buzzer" if buzzer else "Turning off buzzer"
),
)
def set_buzzer(self, buzzer: bool):
"""Set buzzer on/off."""
return self.set_property("buzzer", buzzer)
@command(
click.argument("lock", type=bool),
default_output=format_output(
lambda lock: "Turning on child lock" if lock else "Turning off child lock"
),
)
def set_child_lock(self, lock: bool):
"""Set child lock on/off."""
return self.set_property("child_lock", lock)
@command(
click.argument("minutes", type=int),
default_output=format_output("Setting delayed turn off to {minutes} minutes"),
)
def delay_off(self, minutes: int):
"""Set delay off minutes."""
if minutes < 0 or minutes > 480:
raise FanException("Invalid value for a delayed turn off: %s" % minutes)
return self.set_property("power_off_time", minutes)
@command(
click.argument("direction", type=EnumType(MoveDirection)),
default_output=format_output("Rotating the fan to the {direction}"),
)
def set_rotate(self, direction: MoveDirection):
"""Rotate fan to given direction."""
# Values for: P9,P10,P11,P15,P18,...
# { "value": 0, "description": "NONE" },
# { "value": 1, "description": "LEFT" },
# { "value": 2, "description": "RIGHT" }
value = 0
if direction == MoveDirection.Left:
value = 1
elif direction == MoveDirection.Right:
value = 2
return self.set_property("set_move", value)
class Fan1C(MiotDevice):
# TODO Fan1C should be merged to FanMiot, or moved into its separate file
_mappings = FAN1C_MAPPINGS
def __init__(
self,
ip: str = None,
token: str = None,
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = True,
model: str = MODEL_FAN_1C,
) -> None:
super().__init__(ip, token, start_id, debug, lazy_discover, model=model)
@command(
default_output=format_output(
"",
"Power: {result.power}\n"
"Operation mode: {result.mode}\n"
"Speed: {result.speed}\n"
"Oscillate: {result.oscillate}\n"
"LED: {result.led}\n"
"Buzzer: {result.buzzer}\n"
"Child lock: {result.child_lock}\n"
"Power-off time: {result.delay_off_countdown}\n",
)
)
def status(self) -> FanStatus1C:
"""Retrieve properties."""
return FanStatus1C(
{
prop["did"]: prop["value"] if prop["code"] == 0 else None
for prop in self.get_properties_for_mapping()
}
)
@command(default_output=format_output("Powering on"))
def on(self):
"""Power on."""
return self.set_property("power", True)
@command(default_output=format_output("Powering off"))
def off(self):
"""Power off."""
return self.set_property("power", False)
@command(
click.argument("mode", type=EnumType(OperationMode)),
default_output=format_output("Setting mode to '{mode.value}'"),
)
def set_mode(self, mode: OperationMode):
"""Set mode."""
return self.set_property("mode", OperationModeMiot[mode.name].value)
@command(
click.argument("speed", type=int),
default_output=format_output("Setting speed to {speed}"),
)
def set_speed(self, speed: int):
"""Set speed."""
if speed not in (1, 2, 3):
raise FanException("Invalid speed: %s" % speed)
return self.set_property("fan_level", speed)
@command(
click.argument("oscillate", type=bool),
default_output=format_output(
lambda oscillate: "Turning on oscillate"
if oscillate
else "Turning off oscillate"
),
)
def set_oscillate(self, oscillate: bool):
"""Set oscillate on/off."""
return self.set_property("swing_mode", oscillate)
@command(
click.argument("led", type=bool),
default_output=format_output(
lambda led: "Turning on LED" if led else "Turning off LED"
),
)
def set_led(self, led: bool):
"""Turn led on/off."""
return self.set_property("light", led)
@command(
click.argument("buzzer", type=bool),
default_output=format_output(
lambda buzzer: "Turning on buzzer" if buzzer else "Turning off buzzer"
),
)
def set_buzzer(self, buzzer: bool):
"""Set buzzer on/off."""
return self.set_property("buzzer", buzzer)
@command(
click.argument("lock", type=bool),
default_output=format_output(
lambda lock: "Turning on child lock" if lock else "Turning off child lock"
),
)
def set_child_lock(self, lock: bool):
"""Set child lock on/off."""
return self.set_property("child_lock", lock)
@command(
click.argument("minutes", type=int),
default_output=format_output("Setting delayed turn off to {minutes} minutes"),
)
def delay_off(self, minutes: int):
"""Set delay off minutes."""
if minutes < 0 or minutes > 480:
raise FanException("Invalid value for a delayed turn off: %s" % minutes)
return self.set_property("power_off_time", minutes)