FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-kasa/tests/test_cli.py at patch · IDavinciCode/python-kasa · GitHub
IDavinciCode
python-kasa
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-kasa
/
tests
/
test_cli.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
1307 lines (1080 loc) · 39.3 KB
Breadcrumbs
python-kasa
/
tests
/
test_cli.py
Copy path
File metadata and controls
1307 lines (1080 loc) · 39.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
json
import
os
import
re
from
datetime
import
datetime
from
unittest
.
mock
import
ANY
,
PropertyMock
,
patch
from
zoneinfo
import
ZoneInfo
import
asyncclick
as
click
import
pytest
from
asyncclick
.
testing
import
CliRunner
from
pytest_mock
import
MockerFixture
from
kasa
import
(
AuthenticationError
,
Credentials
,
Device
,
DeviceError
,
DeviceType
,
EmeterStatus
,
KasaException
,
Module
,
)
from
kasa
.
cli
.
device
import
(
alias
,
factory_reset
,
led
,
reboot
,
state
,
sysinfo
,
toggle
,
update_credentials
,
)
from
kasa
.
cli
.
light
import
(
brightness
,
effect
,
hsv
,
presets
,
presets_modify
,
temperature
,
)
from
kasa
.
cli
.
main
import
TYPES
,
_legacy_type_to_class
,
cli
,
cmd_command
,
raw_command
from
kasa
.
cli
.
time
import
time
from
kasa
.
cli
.
usage
import
energy
from
kasa
.
cli
.
wifi
import
wifi
from
kasa
.
discover
import
Discover
,
DiscoveryResult
from
kasa
.
iot
import
IotDevice
from
kasa
.
smart
import
SmartDevice
from
kasa
.
smartcam
import
SmartCamDevice
from
.
conftest
import
(
device_smart
,
get_device_for_fixture_protocol
,
handle_turn_on
,
new_discovery
,
turn_on
,
)
# The cli tests should be testing the cli logic rather than a physical device
# so mark the whole file for skipping with real devices.
pytestmark
=
[
pytest
.
mark
.
requires_dummy
]
@
pytest
.
fixture
def
runner
():
"""Runner fixture that unsets the KASA_ environment variables for tests."""
KASA_VARS
=
{
k
:
None
for
k
,
v
in
os
.
environ
.
items
()
if
k
.
startswith
(
"KASA_"
)}
runner
=
CliRunner
(
env
=
KASA_VARS
)
return
runner
async
def
test_help
(
runner
):
"""Test that all the lazy modules are correctly names."""
res
=
await
runner
.
invoke
(
cli
, [
"--help"
])
assert
res
.
exit_code
==
0
,
"--help failed, check lazy module names"
@
pytest
.
mark
.
parametrize
(
(
"device_family"
,
"encrypt_type"
),
[
pytest
.
param
(
None
,
None
,
id
=
"No connect params"
),
pytest
.
param
(
"SMART.TAPOPLUG"
,
None
,
id
=
"Only device_family"
),
],
)
async
def
test_update_called_by_cli
(
dev
,
mocker
,
runner
,
device_family
,
encrypt_type
):
"""Test that device update is called on main."""
update
=
mocker
.
patch
.
object
(
dev
,
"update"
)
# These will mock the features to avoid accessing non-existing
mocker
.
patch
(
"kasa.device.Device.features"
,
return_value
=
{})
mocker
.
patch
(
"kasa.iot.iotdevice.IotDevice.features"
,
return_value
=
{})
mocker
.
patch
(
"kasa.discover.Discover.discover_single"
,
return_value
=
dev
)
res
=
await
runner
.
invoke
(
cli
,
[
"--host"
,
"127.0.0.1"
,
"--username"
,
"foo"
,
"--password"
,
"bar"
,
"--device-family"
,
device_family
,
"--encrypt-type"
,
encrypt_type
,
],
catch_exceptions
=
False
,
)
assert
res
.
exit_code
==
0
update
.
assert_called
()
async
def
test_list_devices
(
discovery_mock
,
runner
):
"""Test that device update is called on main."""
res
=
await
runner
.
invoke
(
cli
,
[
"--username"
,
"foo"
,
"--password"
,
"bar"
,
"discover"
,
"list"
],
catch_exceptions
=
False
,
)
assert
res
.
exit_code
==
0
header
=
f"
{
'HOST'
:<15
}
{
'DEVICE FAMILY'
:<20
}
{
'ENCRYPT'
:<7
}
{
'ALIAS'
}
"
row
=
f"
{
discovery_mock
.
ip
:<15
}
{
discovery_mock
.
device_type
:<20
}
{
discovery_mock
.
encrypt_type
:<7
}
"
assert
header
in
res
.
output
assert
row
in
res
.
output
@
new_discovery
async
def
test_list_auth_failed
(
discovery_mock
,
mocker
,
runner
):
"""Test that device update is called on main."""
device_class
=
Discover
.
_get_device_class
(
discovery_mock
.
discovery_data
)
mocker
.
patch
.
object
(
device_class
,
"update"
,
side_effect
=
AuthenticationError
(
"Failed to authenticate"
),
)
res
=
await
runner
.
invoke
(
cli
,
[
"--username"
,
"foo"
,
"--password"
,
"bar"
,
"discover"
,
"list"
],
catch_exceptions
=
False
,
)
assert
res
.
exit_code
==
0
header
=
f"
{
'HOST'
:<15
}
{
'DEVICE FAMILY'
:<20
}
{
'ENCRYPT'
:<7
}
{
'ALIAS'
}
"
row
=
f"
{
discovery_mock
.
ip
:<15
}
{
discovery_mock
.
device_type
:<20
}
{
discovery_mock
.
encrypt_type
:<7
}
- Authentication failed"
assert
header
in
res
.
output
assert
row
in
res
.
output
async
def
test_list_unsupported
(
unsupported_device_info
,
runner
):
"""Test that device update is called on main."""
res
=
await
runner
.
invoke
(
cli
,
[
"--username"
,
"foo"
,
"--password"
,
"bar"
,
"discover"
,
"list"
],
catch_exceptions
=
False
,
)
assert
res
.
exit_code
==
0
header
=
f"
{
'HOST'
:<15
}
{
'DEVICE FAMILY'
:<20
}
{
'ENCRYPT'
:<7
}
{
'ALIAS'
}
"
row
=
f"
{
'127.0.0.1'
:<15
}
UNSUPPORTED DEVICE"
assert
header
in
res
.
output
assert
row
in
res
.
output
async
def
test_sysinfo
(
dev
:
Device
,
runner
):
res
=
await
runner
.
invoke
(
sysinfo
,
obj
=
dev
)
assert
"System info"
in
res
.
output
assert
dev
.
model
in
res
.
output
@
turn_on
async
def
test_state
(
dev
,
turn_on
,
runner
):
await
handle_turn_on
(
dev
,
turn_on
)
await
dev
.
update
()
res
=
await
runner
.
invoke
(
state
,
obj
=
dev
)
if
dev
.
is_on
:
assert
"Device state: True"
in
res
.
output
else
:
assert
"Device state: False"
in
res
.
output
@
turn_on
async
def
test_toggle
(
dev
,
turn_on
,
runner
):
if
isinstance
(
dev
,
SmartCamDevice
)
and
dev
.
device_type
==
DeviceType
.
Hub
:
pytest
.
skip
(
reason
=
"Hub cannot toggle state"
)
await
handle_turn_on
(
dev
,
turn_on
)
await
dev
.
update
()
assert
dev
.
is_on
==
turn_on
await
runner
.
invoke
(
toggle
,
obj
=
dev
)
await
dev
.
update
()
assert
dev
.
is_on
!=
turn_on
async
def
test_alias
(
dev
,
runner
):
res
=
await
runner
.
invoke
(
alias
,
obj
=
dev
)
assert
f"Alias:
{
dev
.
alias
}
"
in
res
.
output
old_alias
=
dev
.
alias
new_alias
=
"new alias"
res
=
await
runner
.
invoke
(
alias
, [
new_alias
],
obj
=
dev
)
assert
f"Setting alias to
{
new_alias
}
"
in
res
.
output
await
dev
.
update
()
res
=
await
runner
.
invoke
(
alias
,
obj
=
dev
)
assert
f"Alias:
{
new_alias
}
"
in
res
.
output
await
dev
.
set_alias
(
old_alias
)
async
def
test_raw_command
(
dev
,
mocker
,
runner
):
update
=
mocker
.
patch
.
object
(
dev
,
"update"
)
from
kasa
.
smart
import
SmartDevice
if
isinstance
(
dev
,
SmartCamDevice
):
params
=
[
"na"
,
"getDeviceInfo"
]
elif
isinstance
(
dev
,
SmartDevice
):
params
=
[
"na"
,
"get_device_info"
]
else
:
params
=
[
"system"
,
"get_sysinfo"
]
res
=
await
runner
.
invoke
(
raw_command
,
params
,
obj
=
dev
)
# Make sure that update was not called for wifi
with
pytest
.
raises
(
AssertionError
):
update
.
assert_called
()
assert
res
.
exit_code
==
0
assert
dev
.
model
in
res
.
output
res
=
await
runner
.
invoke
(
raw_command
,
obj
=
dev
)
assert
res
.
exit_code
!=
0
assert
"Usage"
in
res
.
output
async
def
test_command_with_child
(
dev
,
mocker
,
runner
):
"""Test 'command' command with --child."""
update_mock
=
mocker
.
patch
.
object
(
dev
,
"update"
)
# create_autospec for device slows tests way too much, so we use a dummy here
class
DummyDevice
(
dev
.
__class__
):
def
__init__
(
self
):
super
().
__init__
(
"127.0.0.1"
)
# device_type and _info initialised for repr
self
.
_device_type
=
Device
.
Type
.
StripSocket
self
.
_info
=
{}
async
def
_query_helper
(
*
_
,
**
__
):
return
{
"dummy"
:
"response"
}
dummy_child
=
DummyDevice
()
mocker
.
patch
.
object
(
dev
,
"_children"
, {
"XYZ"
: [
dummy_child
]})
mocker
.
patch
.
object
(
dev
,
"get_child_device"
,
return_value
=
dummy_child
)
res
=
await
runner
.
invoke
(
cmd_command
,
[
"--child"
,
"XYZ"
,
"command"
,
"'params'"
],
obj
=
dev
,
catch_exceptions
=
False
,
)
update_mock
.
assert_called
()
assert
'{"dummy": "response"}'
in
res
.
output
assert
res
.
exit_code
==
0
@
device_smart
async
def
test_reboot
(
dev
,
mocker
,
runner
):
"""Test that reboot works on SMART devices."""
query_mock
=
mocker
.
patch
.
object
(
dev
.
protocol
,
"query"
)
res
=
await
runner
.
invoke
(
reboot
,
obj
=
dev
,
)
query_mock
.
assert_called
()
assert
res
.
exit_code
==
0
@
device_smart
async
def
test_factory_reset
(
dev
,
mocker
,
runner
):
"""Test that factory reset works on SMART devices."""
query_mock
=
mocker
.
patch
.
object
(
dev
.
protocol
,
"query"
)
res
=
await
runner
.
invoke
(
factory_reset
,
obj
=
dev
,
input
=
"y
\n
"
,
)
query_mock
.
assert_called
()
assert
res
.
exit_code
==
0
@
device_smart
async
def
test_wifi_scan
(
dev
,
runner
):
res
=
await
runner
.
invoke
(
wifi
, [
"scan"
],
obj
=
dev
)
assert
res
.
exit_code
==
0
assert
re
.
search
(
r"Found [\d]+ wifi networks!"
,
res
.
output
)
@
device_smart
async
def
test_wifi_join
(
dev
,
mocker
,
runner
):
update
=
mocker
.
patch
.
object
(
dev
,
"update"
)
res
=
await
runner
.
invoke
(
wifi
,
[
"join"
,
"FOOBAR"
,
"--keytype"
,
"wpa_psk"
,
"--password"
,
"foobar"
],
obj
=
dev
,
)
# Make sure that update was not called for wifi
with
pytest
.
raises
(
AssertionError
):
update
.
assert_called
()
assert
res
.
exit_code
==
0
assert
"Asking the device to connect to FOOBAR"
in
res
.
output
@
device_smart
async
def
test_wifi_join_no_creds
(
dev
,
runner
):
dev
.
protocol
.
_transport
.
_credentials
=
None
res
=
await
runner
.
invoke
(
wifi
,
[
"join"
,
"FOOBAR"
,
"--keytype"
,
"wpa_psk"
,
"--password"
,
"foobar"
],
obj
=
dev
,
)
assert
res
.
exit_code
!=
0
assert
isinstance
(
res
.
exception
,
AuthenticationError
)
@
device_smart
async
def
test_wifi_join_exception
(
dev
,
mocker
,
runner
):
mocker
.
patch
.
object
(
dev
.
protocol
,
"query"
,
side_effect
=
DeviceError
(
error_code
=
9999
))
res
=
await
runner
.
invoke
(
wifi
,
[
"join"
,
"FOOBAR"
,
"--keytype"
,
"wpa_psk"
,
"--password"
,
"foobar"
],
obj
=
dev
,
)
assert
res
.
exit_code
!=
0
assert
isinstance
(
res
.
exception
,
KasaException
)
@
device_smart
async
def
test_update_credentials
(
dev
,
runner
):
res
=
await
runner
.
invoke
(
update_credentials
,
[
"--username"
,
"foo"
,
"--password"
,
"bar"
],
input
=
"y
\n
"
,
obj
=
dev
,
)
assert
res
.
exit_code
==
0
assert
(
"Do you really want to replace the existing credentials? [y/N]: y
\n
"
in
res
.
output
)
async
def
test_time_get
(
dev
,
runner
):
"""Test time get command."""
res
=
await
runner
.
invoke
(
time
,
obj
=
dev
,
)
assert
res
.
exit_code
==
0
assert
"Current time: "
in
res
.
output
async
def
test_time_sync
(
dev
,
mocker
,
runner
):
"""Test time sync command."""
update
=
mocker
.
patch
.
object
(
dev
,
"update"
)
set_time_mock
=
mocker
.
spy
(
dev
.
modules
[
Module
.
Time
],
"set_time"
)
res
=
await
runner
.
invoke
(
time
,
[
"sync"
],
obj
=
dev
,
)
set_time_mock
.
assert_called
()
update
.
assert_called
()
assert
res
.
exit_code
==
0
assert
"Old time: "
in
res
.
output
assert
"New time: "
in
res
.
output
async
def
test_time_set
(
dev
:
Device
,
mocker
,
runner
):
"""Test time set command."""
time_mod
=
dev
.
modules
[
Module
.
Time
]
set_time_mock
=
mocker
.
spy
(
time_mod
,
"set_time"
)
dt
=
datetime
(
2024
,
10
,
15
,
8
,
15
)
res
=
await
runner
.
invoke
(
time
,
[
"set"
,
str
(
dt
.
year
),
str
(
dt
.
month
),
str
(
dt
.
day
),
str
(
dt
.
hour
),
str
(
dt
.
minute
)],
obj
=
dev
,
)
set_time_mock
.
assert_called
()
assert
time_mod
.
time
==
dt
.
replace
(
tzinfo
=
time_mod
.
timezone
)
assert
res
.
exit_code
==
0
assert
"Old time: "
in
res
.
output
assert
"New time: "
in
res
.
output
zone
=
ZoneInfo
(
"Europe/Berlin"
)
dt
=
dt
.
replace
(
tzinfo
=
zone
)
res
=
await
runner
.
invoke
(
time
,
[
"set"
,
str
(
dt
.
year
),
str
(
dt
.
month
),
str
(
dt
.
day
),
str
(
dt
.
hour
),
str
(
dt
.
minute
),
"--timezone"
,
zone
.
key
,
],
input
=
"y
\n
"
,
obj
=
dev
,
)
assert
time_mod
.
time
==
dt
assert
res
.
exit_code
==
0
assert
"Old time: "
in
res
.
output
assert
"New time: "
in
res
.
output
async
def
test_emeter
(
dev
:
Device
,
mocker
,
runner
):
mocker
.
patch
(
"kasa.Discover.discover_single"
,
return_value
=
dev
)
base_cmd
=
[
"--host"
,
"dummy"
,
"energy"
]
res
=
await
runner
.
invoke
(
cli
,
base_cmd
,
obj
=
dev
)
if
not
(
energy
:=
dev
.
modules
.
get
(
Module
.
Energy
)):
assert
"Device has no energy module."
in
res
.
output
return
assert
"== Energy =="
in
res
.
output
if
dev
.
device_type
is
not
DeviceType
.
Strip
:
res
=
await
runner
.
invoke
(
cli
, [
*
base_cmd
,
"--index"
,
"0"
],
obj
=
dev
)
assert
f"Device:
{
dev
.
host
}
does not have children"
in
res
.
output
res
=
await
runner
.
invoke
(
cli
, [
*
base_cmd
,
"--name"
,
"mock"
],
obj
=
dev
)
assert
f"Device:
{
dev
.
host
}
does not have children"
in
res
.
output
if
dev
.
device_type
is
DeviceType
.
Strip
and
len
(
dev
.
children
)
>
0
:
child_energy
=
dev
.
children
[
0
].
modules
.
get
(
Module
.
Energy
)
assert
child_energy
with
patch
.
object
(
type
(
child_energy
),
"status"
,
new_callable
=
PropertyMock
)
as
child_status
:
child_status
.
return_value
=
EmeterStatus
({
"voltage_mv"
:
122066
})
res
=
await
runner
.
invoke
(
cli
, [
*
base_cmd
,
"--index"
,
"0"
],
obj
=
dev
)
assert
"Voltage: 122.066 V"
in
res
.
output
child_status
.
assert_called
()
assert
child_status
.
call_count
==
1
res
=
await
runner
.
invoke
(
cli
, [
*
base_cmd
,
"--name"
,
dev
.
children
[
0
].
alias
],
obj
=
dev
)
assert
"Voltage: 122.066 V"
in
res
.
output
assert
child_status
.
call_count
==
2
if
isinstance
(
dev
,
IotDevice
):
monthly
=
mocker
.
patch
.
object
(
energy
,
"get_monthly_stats"
)
monthly
.
return_value
=
{
1
:
1234
}
res
=
await
runner
.
invoke
(
cli
, [
*
base_cmd
,
"--year"
,
"1900"
],
obj
=
dev
)
if
not
isinstance
(
dev
,
IotDevice
):
assert
"Device does not support historical statistics"
in
res
.
output
return
assert
"For year"
in
res
.
output
assert
"1, 1234"
in
res
.
output
monthly
.
assert_called_with
(
year
=
1900
)
if
isinstance
(
dev
,
IotDevice
):
daily
=
mocker
.
patch
.
object
(
energy
,
"get_daily_stats"
)
daily
.
return_value
=
{
1
:
1234
}
res
=
await
runner
.
invoke
(
cli
, [
*
base_cmd
,
"--month"
,
"1900-12"
],
obj
=
dev
)
if
not
isinstance
(
dev
,
IotDevice
):
assert
"Device has no historical statistics"
in
res
.
output
return
assert
"For month"
in
res
.
output
assert
"1, 1234"
in
res
.
output
daily
.
assert_called_with
(
year
=
1900
,
month
=
12
)
async
def
test_brightness
(
dev
:
Device
,
runner
):
res
=
await
runner
.
invoke
(
brightness
,
obj
=
dev
)
if
not
(
light
:=
dev
.
modules
.
get
(
Module
.
Light
))
or
not
light
.
is_dimmable
:
assert
"This device does not support brightness."
in
res
.
output
return
res
=
await
runner
.
invoke
(
brightness
,
obj
=
dev
)
assert
f"Brightness:
{
light
.
brightness
}
"
in
res
.
output
res
=
await
runner
.
invoke
(
brightness
, [
"12"
],
obj
=
dev
)
assert
"Setting brightness"
in
res
.
output
await
dev
.
update
()
res
=
await
runner
.
invoke
(
brightness
,
obj
=
dev
)
assert
"Brightness: 12"
in
res
.
output
async
def
test_color_temperature
(
dev
:
Device
,
runner
):
res
=
await
runner
.
invoke
(
temperature
,
obj
=
dev
)
if
not
(
light
:=
dev
.
modules
.
get
(
Module
.
Light
))
or
not
light
.
is_variable_color_temp
:
assert
"Device does not support color temperature"
in
res
.
output
return
res
=
await
runner
.
invoke
(
temperature
,
obj
=
dev
)
assert
f"Color temperature:
{
light
.
color_temp
}
"
in
res
.
output
valid_range
=
light
.
valid_temperature_range
assert
f"(min:
{
valid_range
.
min
}
, max:
{
valid_range
.
max
}
)"
in
res
.
output
val
=
int
((
valid_range
.
min
+
valid_range
.
max
)
/
2
)
res
=
await
runner
.
invoke
(
temperature
, [
str
(
val
)],
obj
=
dev
)
assert
"Setting color temperature to "
in
res
.
output
await
dev
.
update
()
res
=
await
runner
.
invoke
(
temperature
,
obj
=
dev
)
assert
f"Color temperature:
{
val
}
"
in
res
.
output
assert
res
.
exit_code
==
0
invalid_max
=
valid_range
.
max
+
100
# Lights that support the maximum range will not get past the click cli range check
# So can't be tested for the internal range check.
if
invalid_max
<
9000
:
res
=
await
runner
.
invoke
(
temperature
, [
str
(
invalid_max
)],
obj
=
dev
)
assert
res
.
exit_code
==
1
assert
isinstance
(
res
.
exception
,
ValueError
)
res
=
await
runner
.
invoke
(
temperature
, [
str
(
9100
)],
obj
=
dev
)
assert
res
.
exit_code
==
2
async
def
test_color_hsv
(
dev
:
Device
,
runner
:
CliRunner
):
res
=
await
runner
.
invoke
(
hsv
,
obj
=
dev
)
if
not
(
light
:=
dev
.
modules
.
get
(
Module
.
Light
))
or
not
light
.
is_color
:
assert
"Device does not support colors"
in
res
.
output
return
res
=
await
runner
.
invoke
(
hsv
,
obj
=
dev
)
assert
f"Current HSV:
{
light
.
hsv
}
"
in
res
.
output
res
=
await
runner
.
invoke
(
hsv
, [
"180"
,
"50"
,
"50"
],
obj
=
dev
)
assert
"Setting HSV: 180 50 50"
in
res
.
output
assert
res
.
exit_code
==
0
await
dev
.
update
()
res
=
await
runner
.
invoke
(
hsv
, [
"180"
,
"50"
],
obj
=
dev
)
assert
"Setting a color requires 3 values."
in
res
.
output
assert
res
.
exit_code
==
2
async
def
test_light_effect
(
dev
:
Device
,
runner
:
CliRunner
):
res
=
await
runner
.
invoke
(
effect
,
obj
=
dev
)
if
not
(
light_effect
:=
dev
.
modules
.
get
(
Module
.
LightEffect
)):
assert
"Device does not support effects"
in
res
.
output
return
# Start off with a known state of off
await
light_effect
.
set_effect
(
light_effect
.
LIGHT_EFFECTS_OFF
)
await
dev
.
update
()
assert
light_effect
.
effect
==
light_effect
.
LIGHT_EFFECTS_OFF
res
=
await
runner
.
invoke
(
effect
,
obj
=
dev
)
assert
f"Light effect:
{
light_effect
.
effect
}
"
in
res
.
output
assert
res
.
exit_code
==
0
res
=
await
runner
.
invoke
(
effect
, [
light_effect
.
effect_list
[
1
]],
obj
=
dev
)
assert
f"Setting Effect:
{
light_effect
.
effect_list
[
1
]
}
"
in
res
.
output
assert
res
.
exit_code
==
0
await
dev
.
update
()
assert
light_effect
.
effect
==
light_effect
.
effect_list
[
1
]
res
=
await
runner
.
invoke
(
effect
, [
"foobar"
],
obj
=
dev
)
assert
f"Effect must be one of:
{
light_effect
.
effect_list
}
"
in
res
.
output
assert
res
.
exit_code
==
2
async
def
test_light_preset
(
dev
:
Device
,
runner
:
CliRunner
):
res
=
await
runner
.
invoke
(
presets
,
obj
=
dev
)
if
not
(
light_preset
:=
dev
.
modules
.
get
(
Module
.
LightPreset
)):
assert
"Device does not support light presets"
in
res
.
output
return
if
len
(
light_preset
.
preset_states_list
)
==
0
:
pytest
.
skip
(
"Some fixtures do not have presets and"
" the api doesn'tsupport creating them"
)
# Start off with a known state
first_name
=
light_preset
.
preset_list
[
1
]
await
light_preset
.
set_preset
(
first_name
)
await
dev
.
update
()
assert
light_preset
.
preset
==
first_name
res
=
await
runner
.
invoke
(
presets
,
obj
=
dev
)
assert
"Brightness"
in
res
.
output
assert
res
.
exit_code
==
0
res
=
await
runner
.
invoke
(
presets_modify
,
[
"0"
,
"--brightness"
,
"12"
,
],
obj
=
dev
,
)
await
dev
.
update
()
assert
light_preset
.
preset_states_list
[
0
].
brightness
==
12
res
=
await
runner
.
invoke
(
presets_modify
,
[
"0"
,
],
obj
=
dev
,
)
await
dev
.
update
()
assert
"Need to supply at least one option to modify."
in
res
.
output
async
def
test_led
(
dev
:
Device
,
runner
:
CliRunner
):
res
=
await
runner
.
invoke
(
led
,
obj
=
dev
)
if
not
(
led_module
:=
dev
.
modules
.
get
(
Module
.
Led
)):
assert
"Device does not support led"
in
res
.
output
return
res
=
await
runner
.
invoke
(
led
,
obj
=
dev
)
assert
f"LED state:
{
led_module
.
led
}
"
in
res
.
output
assert
res
.
exit_code
==
0
res
=
await
runner
.
invoke
(
led
, [
"on"
],
obj
=
dev
)
assert
"Turning led to True"
in
res
.
output
assert
res
.
exit_code
==
0
await
dev
.
update
()
assert
led_module
.
led
is
True
res
=
await
runner
.
invoke
(
led
, [
"off"
],
obj
=
dev
)
assert
"Turning led to False"
in
res
.
output
assert
res
.
exit_code
==
0
await
dev
.
update
()
assert
led_module
.
led
is
False
async
def
test_json_output
(
dev
:
Device
,
mocker
,
runner
):
"""Test that the json output produces correct output."""
mocker
.
patch
(
"kasa.Discover.discover_single"
,
return_value
=
dev
)
# These will mock the features to avoid accessing non-existing ones
mocker
.
patch
(
"kasa.device.Device.features"
,
return_value
=
{})
mocker
.
patch
(
"kasa.iot.iotdevice.IotDevice.features"
,
return_value
=
{})
res
=
await
runner
.
invoke
(
cli
, [
"--host"
,
"127.0.0.1"
,
"--json"
,
"state"
],
obj
=
dev
)
assert
res
.
exit_code
==
0
assert
json
.
loads
(
res
.
output
)
==
dev
.
internal_state
@
new_discovery
async
def
test_credentials
(
discovery_mock
,
mocker
,
runner
):
"""Test credentials are passed correctly from cli to device."""
# Patch state to echo username and password
pass_dev
=
click
.
make_pass_decorator
(
Device
)
@
pass_dev
async
def
_state
(
dev
:
Device
):
if
dev
.
credentials
:
click
.
echo
(
f"Username:
{
dev
.
credentials
.
username
}
Password:
{
dev
.
credentials
.
password
}
"
)
mocker
.
patch
(
"kasa.cli.device.state"
,
new
=
_state
)
dr
=
DiscoveryResult
.
from_dict
(
discovery_mock
.
discovery_data
[
"result"
])
res
=
await
runner
.
invoke
(
cli
,
[
"--host"
,
"127.0.0.123"
,
"--username"
,
"foo"
,
"--password"
,
"bar"
,
"--device-family"
,
dr
.
device_type
,
"--encrypt-type"
,
dr
.
mgt_encrypt_schm
.
encrypt_type
,
],
)
assert
res
.
exit_code
==
0
assert
"Username:foo Password:bar
\n
"
in
res
.
output
async
def
test_without_device_type
(
dev
,
mocker
,
runner
):
"""Test connecting without the device type."""
discovery_mock
=
mocker
.
patch
(
"kasa.discover.Discover.discover_single"
,
return_value
=
dev
)
# These will mock the features to avoid accessing non-existing
mocker
.
patch
(
"kasa.device.Device.features"
,
return_value
=
{})
mocker
.
patch
(
"kasa.iot.iotdevice.IotDevice.features"
,
return_value
=
{})
res
=
await
runner
.
invoke
(
cli
,
[
"--host"
,
"127.0.0.1"
,
"--username"
,
"foo"
,
"--password"
,
"bar"
,
"--discovery-timeout"
,
"7"
,
],
)
assert
res
.
exit_code
==
0
discovery_mock
.
assert_called_once_with
(
"127.0.0.1"
,
port
=
None
,
credentials
=
Credentials
(
"foo"
,
"bar"
),
timeout
=
5
,
discovery_timeout
=
7
,
on_unsupported
=
ANY
,
)
@
pytest
.
mark
.
parametrize
(
"auth_param"
, [
"--username"
,
"--password"
])
async
def
test_invalid_credential_params
(
auth_param
,
runner
):
"""Test for handling only one of username or password supplied."""
res
=
await
runner
.
invoke
(
cli
,
[
"--host"
,
"127.0.0.1"
,
"--type"
,
"plug"
,
auth_param
,
"foo"
,
],
)
assert
res
.
exit_code
==
2
assert
(
"Error: Using authentication requires both --username and --password"
in
res
.
output
)
async
def
test_duplicate_target_device
(
runner
):
"""Test that defining both --host or --alias gives an error."""
res
=
await
runner
.
invoke
(
cli
,
[
"--host"
,
"127.0.0.1"
,
"--alias"
,
"foo"
,
],
)
assert
res
.
exit_code
==
2
assert
"Error: Use either --alias or --host, not both."
in
res
.
output
async
def
test_discover
(
discovery_mock
,
mocker
,
runner
):
"""Test discovery output."""
# These will mock the features to avoid accessing non-existing
mocker
.
patch
(
"kasa.device.Device.features"
,
return_value
=
{})
mocker
.
patch
(
"kasa.iot.iotdevice.IotDevice.features"
,
return_value
=
{})
res
=
await
runner
.
invoke
(
cli
,
[
"--discovery-timeout"
,
0
,
"--username"
,
"foo"
,
"--password"
,
"bar"
,
"--verbose"
,
"discover"
,
],
)
assert
res
.
exit_code
==
0
async
def
test_discover_host
(
discovery_mock
,
mocker
,
runner
):
"""Test discovery output."""
# These will mock the features to avoid accessing non-existing
mocker
.
patch
(
"kasa.device.Device.features"
,
return_value
=
{})
mocker
.
patch
(
"kasa.iot.iotdevice.IotDevice.features"
,
return_value
=
{})
res
=
await
runner
.
invoke
(
cli
,
[
"--discovery-timeout"
,
0
,
"--host"
,
"127.0.0.123"
,
"--username"
,
"foo"
,
"--password"
,
"bar"
,
"--verbose"
,
],
)
assert
res
.
exit_code
==
0
async
def
test_discover_unsupported
(
unsupported_device_info
,
runner
):
"""Test discovery output."""
res
=
await
runner
.
invoke
(
cli
,
[
"--discovery-timeout"
,
0
,
"--username"
,
"foo"
,
"--password"
,
"bar"
,
"--verbose"
,
"discover"
,
],
)
assert
res
.
exit_code
==
0
assert
"== Unsupported device =="
in
res
.
output
async
def
test_host_unsupported
(
unsupported_device_info
,
runner
):
"""Test discovery output."""
host
=
"127.0.0.1"
res
=
await
runner
.
invoke
(
cli
,
[
"--host"
,
host
,
"--username"
,
"foo"
,
"--password"
,
"bar"
,
"--debug"
,
],
)
assert
res
.
exit_code
!=
0
assert
"== Unsupported device =="
in
res
.
output
@
new_discovery
async
def
test_discover_auth_failed
(
discovery_mock
,
mocker
,
runner
):
"""Test discovery output."""
host
=
"127.0.0.1"
discovery_mock
.
ip
=
host
device_class
=
Discover
.
_get_device_class
(
discovery_mock
.
discovery_data
)
mocker
.
patch
.
object
(
device_class
,
"update"
,
side_effect
=
AuthenticationError
(
"Failed to authenticate"
),
)
res
=
await
runner
.
invoke
(
cli
,
[
"--discovery-timeout"
,
0
,
"--username"
,
"foo"
,
"--password"
,
"bar"
,
"--verbose"
,
"discover"
,
],
)
assert
res
.
exit_code
==
0
assert
"== Authentication failed for device =="
in
res
.
output
assert
"== Discovery Result =="
in
res
.
output
@
new_discovery
async
def
test_host_auth_failed
(
discovery_mock
,
mocker
,
runner
):
"""Test discovery output."""
host
=
"127.0.0.1"
discovery_mock
.
ip
=
host
device_class
=
Discover
.
_get_device_class
(
discovery_mock
.
discovery_data
)
mocker
.
patch
.
object
(
device_class
,
"update"
,
side_effect
=
AuthenticationError
(
"Failed to authenticate"
),
)
res
=
await
runner
.
invoke
(
cli
,
[
"--host"
,
host
,
"--username"
,
"foo"
,
"--password"
,
"bar"
,
"--debug"
,
],
)
assert
res
.
exit_code
!=
0
assert
isinstance
(
res
.
exception
,
AuthenticationError
)
@
pytest
.
mark
.
parametrize
(
"device_type"
,
TYPES
)
async
def
test_type_param
(
device_type
,
mocker
,
runner
):
"""Test for handling only one of username or password supplied."""
result_device
=
FileNotFoundError
pass_dev
=
click
.
make_pass_decorator
(
Device
)
@
pass_dev
async
def
_state
(
dev
:
Device
):
nonlocal
result_device
result_device
=
dev
mocker
.
patch
(
"kasa.cli.device.state"
,
new
=
_state
)
if
device_type
==
"camera"
:
expected_type
=
SmartCamDevice
elif
device_type
==
"smart"
:
expected_type
=
SmartDevice
else
:
expected_type
=
_legacy_type_to_class
(
device_type
)
mocker
.
patch
.
object
(
expected_type
,
"update"
)
res
=
await
runner
.
invoke
(
cli
,
[
"--type"
,
device_type
,
"--host"
,
"127.0.0.1"
],
)
assert
res
.
exit_code
==
0
assert
isinstance
(
result_device
,
expected_type
)
@
pytest
.
mark
.
skip
(
"Skip until pytest-asyncio supports pytest 8.0, https://github.com/pytest-dev/pytest-asyncio/issues/737"
)
async
def
test_shell
(
dev
:
Device
,
mocker
,
runner
):
"""Test that the shell commands tries to embed a shell."""
mocker
.
patch
(
"kasa.Discover.discover"
,
return_value
=
[
dev
])
# repl = mocker.patch("ptpython.repl")
mocker
.
patch
.
dict
(
"sys.modules"
,
{
"ptpython"
:
mocker
.
MagicMock
(),
"ptpython.repl"
:
mocker
.
MagicMock
()},
)
embed
=
mocker
.
patch
(
"ptpython.repl.embed"
)
res
=
await
runner
.
invoke
(
cli
, [
"shell"
],
obj
=
dev
)
assert
res
.
exit_code
==
0
embed
.
assert_called
()
async
def
test_errors
(
mocker
,
runner
):
err
=
KasaException
(
"Foobar"
)
# Test masking
mocker
.
patch
(
"kasa.Discover.discover"
,
side_effect
=
err
)
res
=
await
runner
.
invoke
(
cli
,
[
"--username"
,
"foo"
,
"--password"
,
"bar"
],
)
assert
res
.
exit_code
==
1
assert
"Raised error: Foobar"
in
res
.
output
assert
"Run with --debug enabled to see stacktrace"
in
res
.
output
assert
isinstance
(
res
.
exception
,
SystemExit
)
# Test --debug
res
=
await
runner
.
invoke
(
cli
,
[
"--debug"
],
)
assert
res
.
exit_code
==
1
assert
"Raised error: Foobar"
in
res
.
output
assert
res
.
exception
==
err
# Test no device passed to subcommand
mocker
.
patch
(
"kasa.Discover.discover"
,
return_value
=
{})
res
=
await
runner
.
invoke
(
cli
,
[
"sysinfo"
],
)
assert
res
.
exit_code
==
1
assert
(
"Only discover is available without --host or --alias"
in
res
.
output
.
replace
(
"
\n
"
,
""
)
# Remove newlines from rich formatting
)
assert
isinstance
(
res
.
exception
,
SystemExit
)
# Test click error
res
=
await
runner
.
invoke
(
cli
,
[
"--foobar"
],
)
assert
res
.
exit_code
==
2
assert
"Raised error:"
not
in
res
.
output
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL