/
fan.py
1064 lines (913 loc) · 31.3 KB
/
fan.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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import enum
import logging
from typing import Any, Dict, Optional
import click
from .click_common import EnumType, command, format_output
from .device import Device
from .exceptions import DeviceException
from .miot_device import MiotDevice
_LOGGER = logging.getLogger(__name__)
MODEL_FAN_V2 = "zhimi.fan.v2"
MODEL_FAN_V3 = "zhimi.fan.v3"
MODEL_FAN_SA1 = "zhimi.fan.sa1"
MODEL_FAN_ZA1 = "zhimi.fan.za1"
MODEL_FAN_ZA3 = "zhimi.fan.za3"
MODEL_FAN_ZA4 = "zhimi.fan.za4"
MODEL_FAN_P5 = "dmaker.fan.p5"
MODEL_FAN_P9 = "dmaker.fan.p9"
MODEL_FAN_P10 = "dmaker.fan.p10"
AVAILABLE_PROPERTIES_COMMON = [
"angle",
"speed",
"poweroff_time",
"power",
"ac_power",
"angle_enable",
"speed_level",
"natural_level",
"child_lock",
"buzzer",
"led_b",
"use_time",
]
AVAILABLE_PROPERTIES_COMMON_V2_V3 = [
"temp_dec",
"humidity",
"battery",
"bat_charge",
"button_pressed",
] + AVAILABLE_PROPERTIES_COMMON
AVAILABLE_PROPERTIES_P5 = [
"power",
"mode",
"speed",
"roll_enable",
"roll_angle",
"time_off",
"light",
"beep_sound",
"child_lock",
]
AVAILABLE_PROPERTIES = {
MODEL_FAN_V2: ["led", "bat_state"] + AVAILABLE_PROPERTIES_COMMON_V2_V3,
MODEL_FAN_V3: AVAILABLE_PROPERTIES_COMMON_V2_V3,
MODEL_FAN_SA1: AVAILABLE_PROPERTIES_COMMON,
MODEL_FAN_ZA1: AVAILABLE_PROPERTIES_COMMON,
MODEL_FAN_ZA3: AVAILABLE_PROPERTIES_COMMON,
MODEL_FAN_ZA4: AVAILABLE_PROPERTIES_COMMON,
MODEL_FAN_P5: AVAILABLE_PROPERTIES_P5,
}
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},
}
}
class FanException(DeviceException):
pass
class OperationMode(enum.Enum):
Normal = "normal"
Nature = "nature"
class OperationModeMiot(enum.Enum):
Normal = 0
Nature = 1
class LedBrightness(enum.Enum):
Bright = 0
Dim = 1
Off = 2
class MoveDirection(enum.Enum):
Left = "left"
Right = "right"
class FanStatus:
"""Container for status reports from the Xiaomi Mi Smart Pedestal Fan."""
def __init__(self, data: Dict[str, Any]) -> None:
"""
Response of a Fan (zhimi.fan.v3):
{'temp_dec': 232, 'humidity': 46, 'angle': 118, 'speed': 298,
'poweroff_time': 0, 'power': 'on', 'ac_power': 'off', 'battery': 98,
'angle_enable': 'off', 'speed_level': 1, 'natural_level': 0,
'child_lock': 'off', 'buzzer': 'on', 'led_b': 1, 'led': None,
'natural_enable': None, 'use_time': 0, 'bat_charge': 'complete',
'bat_state': None, 'button_pressed':'speed'}
Response of a Fan (zhimi.fan.sa1):
{'angle': 120, 'speed': 277, 'poweroff_time': 0, 'power': 'on',
'ac_power': 'on', 'angle_enable': 'off', 'speed_level': 1, 'natural_level': 2,
'child_lock': 'off', 'buzzer': 0, 'led_b': 0, 'use_time': 2318}
Response of a Fan (zhimi.fan.sa4):
{'angle': 120, 'speed': 327, 'poweroff_time': 0, 'power': 'on',
'ac_power': 'on', 'angle_enable': 'off', 'speed_level': 1, 'natural_level': 0,
'child_lock': 'off', 'buzzer': 2, 'led_b': 0, 'use_time': 85}
"""
self.data = data
@property
def power(self) -> str:
"""Power state."""
return self.data["power"]
@property
def is_on(self) -> bool:
"""True if device is currently on."""
return self.power == "on"
@property
def humidity(self) -> Optional[int]:
"""Current humidity."""
if "humidity" in self.data and self.data["humidity"] is not None:
return self.data["humidity"]
return None
@property
def temperature(self) -> Optional[float]:
"""Current temperature, if available."""
if "temp_dec" in self.data and self.data["temp_dec"] is not None:
return self.data["temp_dec"] / 10.0
return None
@property
def led(self) -> Optional[bool]:
"""True if LED is turned on, if available."""
if "led" in self.data and self.data["led"] is not None:
return self.data["led"] == "on"
return None
@property
def led_brightness(self) -> Optional[LedBrightness]:
"""LED brightness, if available."""
if self.data["led_b"] is not None:
return LedBrightness(self.data["led_b"])
return None
@property
def buzzer(self) -> bool:
"""True if buzzer is turned on."""
return self.data["buzzer"] in ["on", 1, 2]
@property
def child_lock(self) -> bool:
"""True if child lock is on."""
return self.data["child_lock"] == "on"
@property
def natural_speed(self) -> Optional[int]:
"""Speed level in natural mode."""
if "natural_level" in self.data and self.data["natural_level"] is not None:
return self.data["natural_level"]
@property
def direct_speed(self) -> Optional[int]:
"""Speed level in direct mode."""
if "speed_level" in self.data and self.data["speed_level"] is not None:
return self.data["speed_level"]
@property
def oscillate(self) -> bool:
"""True if oscillation is enabled."""
return self.data["angle_enable"] == "on"
@property
def battery(self) -> Optional[int]:
"""Current battery level."""
if "battery" in self.data and self.data["battery"] is not None:
return self.data["battery"]
@property
def battery_charge(self) -> Optional[str]:
"""State of the battery charger, if available."""
if "bat_charge" in self.data and self.data["bat_charge"] is not None:
return self.data["bat_charge"]
return None
@property
def battery_state(self) -> Optional[str]:
"""State of the battery, if available."""
if "bat_state" in self.data and self.data["bat_state"] is not None:
return self.data["bat_state"]
return None
@property
def ac_power(self) -> bool:
"""True if powered by AC."""
return self.data["ac_power"] == "on"
@property
def delay_off_countdown(self) -> int:
"""Countdown until turning off in seconds."""
return self.data["poweroff_time"]
@property
def speed(self) -> int:
"""Speed of the motor."""
return self.data["speed"]
@property
def angle(self) -> int:
"""Current angle."""
return self.data["angle"]
@property
def use_time(self) -> int:
"""How long the device has been active in seconds."""
return self.data["use_time"]
@property
def button_pressed(self) -> Optional[str]:
"""Last pressed button."""
if "button_pressed" in self.data and self.data["button_pressed"] is not None:
return self.data["button_pressed"]
return None
def __repr__(self) -> str:
s = (
"<FanStatus power=%s, "
"temperature=%s, "
"humidity=%s, "
"led=%s, "
"led_brightness=%s, "
"buzzer=%s, "
"child_lock=%s, "
"natural_speed=%s, "
"direct_speed=%s, "
"speed=%s, "
"oscillate=%s, "
"angle=%s, "
"ac_power=%s, "
"battery=%s, "
"battery_charge=%s, "
"battery_state=%s, "
"use_time=%s, "
"delay_off_countdown=%s, "
"button_pressed=%s>"
% (
self.power,
self.temperature,
self.humidity,
self.led,
self.led_brightness,
self.buzzer,
self.child_lock,
self.natural_speed,
self.direct_speed,
self.speed,
self.oscillate,
self.angle,
self.ac_power,
self.battery,
self.battery_charge,
self.battery_state,
self.use_time,
self.delay_off_countdown,
self.button_pressed,
)
)
return s
def __json__(self):
return self.data
class FanStatusP5:
"""Container for status reports from the Xiaomi Mi Smart Pedestal Fan DMaker P5."""
def __init__(self, data: Dict[str, Any]) -> None:
"""
Response of a Fan (dmaker.fan.p5):
{'power': False, 'mode': 'normal', 'speed': 35, 'roll_enable': False,
'roll_angle': 140, 'time_off': 0, 'light': True, 'beep_sound': False,
'child_lock': False}
"""
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(self.data["mode"])
@property
def speed(self) -> int:
"""Speed of the motor."""
return self.data["speed"]
@property
def oscillate(self) -> bool:
"""True if oscillation is enabled."""
return self.data["roll_enable"]
@property
def angle(self) -> int:
"""Oscillation angle."""
return self.data["roll_angle"]
@property
def delay_off_countdown(self) -> int:
"""Countdown until turning off in seconds."""
return self.data["time_off"]
@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["beep_sound"]
@property
def child_lock(self) -> bool:
"""True if child lock is on."""
return self.data["child_lock"]
def __repr__(self) -> str:
s = (
"<FanStatus power=%s, "
"mode=%s, "
"speed=%s, "
"oscillate=%s, "
"angle=%s, "
"led=%s, "
"buzzer=%s, "
"child_lock=%s, "
"delay_off_countdown=%s>"
% (
self.power,
self.mode,
self.speed,
self.oscillate,
self.angle,
self.led,
self.buzzer,
self.child_lock,
self.delay_off_countdown,
)
)
return s
def __json__(self):
return self.data
class FanStatusMiot:
"""Container for status reports from the Xiaomi Mi Smart Pedestal Fan DMaker P9/P10."""
def __init__(self, data: Dict[str, Any]) -> None:
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) -> OperationModeMiot:
"""Operation mode."""
return OperationModeMiot(self.data["mode"])
@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 seconds."""
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"]
def __repr__(self) -> str:
s = (
"<FanStatus power=%s, "
"mode=%s, "
"speed=%s, "
"oscillate=%s, "
"angle=%s, "
"led=%s, "
"buzzer=%s, "
"child_lock=%s, "
"delay_off_countdown=%s>"
% (
self.power,
self.mode,
self.speed,
self.oscillate,
self.angle,
self.led,
self.buzzer,
self.child_lock,
self.delay_off_countdown,
)
)
return s
def __json__(self):
return self.data
class Fan(Device):
"""Main class representing the Xiaomi Mi Smart Pedestal Fan."""
def __init__(
self,
ip: str = None,
token: str = None,
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = True,
model: str = MODEL_FAN_V3,
) -> None:
super().__init__(ip, token, start_id, debug, lazy_discover)
if model in AVAILABLE_PROPERTIES:
self.model = model
else:
self.model = MODEL_FAN_V3
@command(
default_output=format_output(
"",
"Power: {result.power}\n"
"Battery: {result.battery} %\n"
"AC power: {result.ac_power}\n"
"Temperature: {result.temperature} °C\n"
"Humidity: {result.humidity} %\n"
"LED: {result.led}\n"
"LED brightness: {result.led_brightness}\n"
"Buzzer: {result.buzzer}\n"
"Child lock: {result.child_lock}\n"
"Speed: {result.speed}\n"
"Natural speed: {result.natural_speed}\n"
"Direct speed: {result.direct_speed}\n"
"Oscillate: {result.oscillate}\n"
"Power-off time: {result.delay_off_countdown}\n"
"Angle: {result.angle}\n",
)
)
def status(self) -> FanStatus:
"""Retrieve properties."""
properties = AVAILABLE_PROPERTIES[self.model]
# A single request is limited to 16 properties. Therefore the
# properties are divided into multiple requests
_props_per_request = 15
# The SA1, ZA1, ZA3 and ZA4 is limited to a single property per request
if self.model in [MODEL_FAN_SA1, MODEL_FAN_ZA1, MODEL_FAN_ZA3, MODEL_FAN_ZA4]:
_props_per_request = 1
values = self.get_properties(properties, max_properties=_props_per_request)
return FanStatus(dict(zip(properties, values)))
@command(default_output=format_output("Powering on"))
def on(self):
"""Power on."""
return self.send("set_power", ["on"])
@command(default_output=format_output("Powering off"))
def off(self):
"""Power off."""
return self.send("set_power", ["off"])
@command(
click.argument("speed", type=int),
default_output=format_output("Setting speed of the natural mode to {speed}"),
)
def set_natural_speed(self, speed: int):
"""Set natural level."""
if speed < 0 or speed > 100:
raise FanException("Invalid speed: %s" % speed)
return self.send("set_natural_level", [speed])
@command(
click.argument("speed", type=int),
default_output=format_output("Setting speed of the direct mode to {speed}"),
)
def set_direct_speed(self, speed: int):
"""Set speed of the direct mode."""
if speed < 0 or speed > 100:
raise FanException("Invalid speed: %s" % speed)
return self.send("set_speed_level", [speed])
@command(
click.argument("direction", type=EnumType(MoveDirection)),
default_output=format_output("Rotating the fan to the {direction}"),
)
def set_rotate(self, direction: MoveDirection):
"""Rotate the fan by -5/+5 degrees left/right."""
return self.send("set_move", [direction.value])
@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 < 0 or angle > 120:
raise FanException("Invalid angle: %s" % angle)
return self.send("set_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."""
if oscillate:
return self.send("set_angle_enable", ["on"])
else:
return self.send("set_angle_enable", ["off"])
@command(
click.argument("brightness", type=EnumType(LedBrightness)),
default_output=format_output("Setting LED brightness to {brightness}"),
)
def set_led_brightness(self, brightness: LedBrightness):
"""Set led brightness."""
return self.send("set_led_b", [brightness.value])
@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. Not supported by model SA1."""
if led:
return self.send("set_led", ["on"])
else:
return self.send("set_led", ["off"])
@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."""
if self.model in [MODEL_FAN_SA1, MODEL_FAN_ZA1, MODEL_FAN_ZA3, MODEL_FAN_ZA4]:
if buzzer:
return self.send("set_buzzer", [2])
else:
return self.send("set_buzzer", [0])
if buzzer:
return self.send("set_buzzer", ["on"])
else:
return self.send("set_buzzer", ["off"])
@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."""
if lock:
return self.send("set_child_lock", ["on"])
else:
return self.send("set_child_lock", ["off"])
@command(
click.argument("seconds", type=int),
default_output=format_output("Setting delayed turn off to {seconds} seconds"),
)
def delay_off(self, seconds: int):
"""Set delay off seconds."""
if seconds < 0:
raise FanException("Invalid value for a delayed turn off: %s" % seconds)
return self.send("set_poweroff_time", [seconds])
class FanV2(Fan):
def __init__(
self,
ip: str = None,
token: str = None,
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = True,
) -> None:
super().__init__(ip, token, start_id, debug, lazy_discover, model=MODEL_FAN_V2)
class FanSA1(Fan):
def __init__(
self,
ip: str = None,
token: str = None,
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = True,
) -> None:
super().__init__(ip, token, start_id, debug, lazy_discover, model=MODEL_FAN_SA1)
class FanZA1(Fan):
def __init__(
self,
ip: str = None,
token: str = None,
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = True,
) -> None:
super().__init__(ip, token, start_id, debug, lazy_discover, model=MODEL_FAN_ZA1)
class FanZA3(Fan):
def __init__(
self,
ip: str = None,
token: str = None,
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = True,
) -> None:
super().__init__(ip, token, start_id, debug, lazy_discover, model=MODEL_FAN_ZA3)
class FanZA4(Fan):
def __init__(
self,
ip: str = None,
token: str = None,
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = True,
) -> None:
super().__init__(ip, token, start_id, debug, lazy_discover, model=MODEL_FAN_ZA4)
class FanP5(Device):
def __init__(
self,
ip: str = None,
token: str = None,
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = True,
model: str = MODEL_FAN_P5,
) -> None:
super().__init__(ip, token, start_id, debug, lazy_discover)
if model in AVAILABLE_PROPERTIES:
self.model = model
else:
self.model = MODEL_FAN_P5
@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) -> FanStatusP5:
"""Retrieve properties."""
properties = AVAILABLE_PROPERTIES[self.model]
values = self.get_properties(properties, max_properties=15)
return FanStatusP5(dict(zip(properties, values)))
@command(default_output=format_output("Powering on"))
def on(self):
"""Power on."""
return self.send("s_power", [True])
@command(default_output=format_output("Powering off"))
def off(self):
"""Power off."""
return self.send("s_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.send("s_mode", [mode.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.send("s_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 [30, 60, 90, 120, 140]:
raise FanException(
"Unsupported angle. Supported values: 30, 60, 90, 120, 140"
)
return self.send("s_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."""
if oscillate:
return self.send("s_roll", [True])
else:
return self.send("s_roll", [False])
@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."""
if led:
return self.send("s_light", [True])
else:
return self.send("s_light", [False])
@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."""
if buzzer:
return self.send("s_sound", [True])
else:
return self.send("s_sound", [False])
@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."""
if lock:
return self.send("s_lock", [True])
else:
return self.send("s_lock", [False])
@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:
raise FanException("Invalid value for a delayed turn off: %s" % minutes)
return self.send("s_t_off", [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 the fan by -5/+5 degrees left/right."""
return self.send("m_roll", [direction.value])
class FanMiot(MiotDevice):
def __init__(
self,
ip: str = None,
token: str = None,
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = True,
model: str = MODEL_FAN_P10,
) -> None:
super().__init__(MIOT_MAPPING[model], ip, token, start_id, debug, lazy_discover)
if model in MIOT_MAPPING:
self.model = model
else:
self.model = MODEL_FAN_P10
@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(OperationModeMiot)),
default_output=format_output("Setting mode to '{mode.value}'"),
)
def set_mode(self, mode: OperationModeMiot):
"""Set mode."""
return self.set_property("mode", mode.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 [30, 60, 90, 120, 140]:
raise FanException(
"Unsupported angle. Supported values: 30, 60, 90, 120, 140"
)
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."""
if oscillate:
return self.set_property("swing_mode", True)
else:
return self.set_property("swing_mode", False)
@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."""
if led:
return self.set_property("light", True)
else:
return self.set_property("light", False)