FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-kasa/devtools/dump_devinfo.py at intint · IDavinciCode/python-kasa · GitHub
IDavinciCode
python-kasa
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-kasa
/
devtools
/
dump_devinfo.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
437 lines (386 loc) · 14.4 KB
Breadcrumbs
python-kasa
/
devtools
/
dump_devinfo.py
Copy path
File metadata and controls
437 lines (386 loc) · 14.4 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
"""This script generates devinfo files for the test suite.
If you have new, yet unsupported device or a device with no devinfo file under
kasa/tests/fixtures, feel free to run this script and create a PR to add the file
to the repository.
Executing this script will several modules and methods one by one,
and finally execute a query to query all of them at once.
"""
import
base64
import
collections
.
abc
import
json
import
logging
import
re
from
collections
import
defaultdict
,
namedtuple
from
pathlib
import
Path
from
pprint
import
pprint
from
typing
import
Dict
,
List
,
Union
import
asyncclick
as
click
from
devtools
.
helpers
.
smartrequests
import
COMPONENT_REQUESTS
,
SmartRequest
from
kasa
import
(
AuthenticationException
,
Credentials
,
Discover
,
SmartDevice
,
SmartDeviceException
,
TimeoutException
,
)
from
kasa
.
discover
import
DiscoveryResult
from
kasa
.
exceptions
import
SmartErrorCode
from
kasa
.
tapo
.
tapodevice
import
TapoDevice
Call
=
namedtuple
(
"Call"
,
"module method"
)
SmartCall
=
namedtuple
(
"SmartCall"
,
"module request should_succeed"
)
def
scrub
(
res
):
"""Remove identifiers from the given dict."""
keys_to_scrub
=
[
"deviceId"
,
"fwId"
,
"hwId"
,
"oemId"
,
"mac"
,
"mic_mac"
,
"latitude_i"
,
"longitude_i"
,
"latitude"
,
"longitude"
,
"owner"
,
"device_id"
,
"ip"
,
"ssid"
,
"hw_id"
,
"fw_id"
,
"oem_id"
,
"nickname"
,
"alias"
,
"bssid"
,
"channel"
,
]
for
k
,
v
in
res
.
items
():
if
isinstance
(
v
,
collections
.
abc
.
Mapping
):
res
[
k
]
=
scrub
(
res
.
get
(
k
))
elif
(
isinstance
(
v
,
list
)
and
len
(
v
)
>
0
and
isinstance
(
v
[
0
],
collections
.
abc
.
Mapping
)
):
res
[
k
]
=
[
scrub
(
vi
)
for
vi
in
v
]
else
:
if
k
in
keys_to_scrub
:
if
k
in
[
"mac"
,
"mic_mac"
]:
# Some macs have : or - as a separator and others do not
if
len
(
v
)
==
12
:
v
=
f"
{
v
[:
6
]
}
000000"
else
:
delim
=
":"
if
":"
in
v
else
"-"
rest
=
delim
.
join
(
format
(
s
,
"02x"
)
for
s
in
bytes
.
fromhex
(
"000000"
)
)
v
=
f"
{
v
[:
8
]
}
{
delim
}
{
rest
}
"
elif
k
in
[
"latitude"
,
"latitude_i"
,
"longitude"
,
"longitude_i"
]:
v
=
0
elif
k
in
[
"ip"
]:
v
=
"127.0.0.123"
elif
k
in
[
"ssid"
]:
# Need a valid base64 value here
v
=
base64
.
b64encode
(
b"#MASKED_SSID#"
).
decode
()
elif
k
in
[
"nickname"
]:
v
=
base64
.
b64encode
(
b"#MASKED_NAME#"
).
decode
()
elif
k
in
[
"alias"
]:
v
=
"#MASKED_NAME#"
elif
isinstance
(
res
[
k
],
int
):
v
=
0
else
:
v
=
re
.
sub
(
r"\w"
,
"0"
,
v
)
res
[
k
]
=
v
return
res
def
default_to_regular
(
d
):
"""Convert nested defaultdicts to regular ones.
From https://stackoverflow.com/a/26496899
"""
if
isinstance
(
d
,
defaultdict
):
d
=
{
k
:
default_to_regular
(
v
)
for
k
,
v
in
d
.
items
()}
return
d
async
def
handle_device
(
basedir
,
autosave
,
device
:
SmartDevice
,
batch_size
:
int
):
"""Create a fixture for a single device instance."""
if
isinstance
(
device
,
TapoDevice
):
filename
,
copy_folder
,
final
=
await
get_smart_fixture
(
device
,
batch_size
)
else
:
filename
,
copy_folder
,
final
=
await
get_legacy_fixture
(
device
)
save_filename
=
Path
(
basedir
)
/
copy_folder
/
filename
pprint
(
scrub
(
final
))
if
autosave
:
save
=
"y"
else
:
save
=
click
.
prompt
(
f"Do you want to save the above content to
{
save_filename
}
(y/n)"
)
if
save
==
"y"
:
click
.
echo
(
f"Saving info to
{
save_filename
}
"
)
with
open
(
save_filename
,
"w"
)
as
f
:
json
.
dump
(
final
,
f
,
sort_keys
=
True
,
indent
=
4
)
f
.
write
(
"
\n
"
)
else
:
click
.
echo
(
"Not saving."
)
@
click
.
command
()
@
click
.
option
(
"--host"
,
required
=
False
,
help
=
"Target host."
)
@
click
.
option
(
"--target"
,
required
=
False
,
default
=
"255.255.255.255"
,
help
=
"Target network for discovery."
,
)
@
click
.
option
(
"--username"
,
default
=
""
,
required
=
False
,
envvar
=
"KASA_USERNAME"
,
help
=
"Username/email address to authenticate to device."
,
)
@
click
.
option
(
"--password"
,
default
=
""
,
required
=
False
,
envvar
=
"KASA_PASSWORD"
,
help
=
"Password to use to authenticate to device."
,
)
@
click
.
option
(
"--basedir"
,
help
=
"Base directory for the git repository"
,
default
=
"."
)
@
click
.
option
(
"--autosave"
,
is_flag
=
True
,
default
=
False
,
help
=
"Save without prompting"
)
@
click
.
option
(
"--batch-size"
,
default
=
5
,
help
=
"Number of batched requests to send at once"
)
@
click
.
option
(
"-d"
,
"--debug"
,
is_flag
=
True
)
async
def
cli
(
host
,
target
,
basedir
,
autosave
,
debug
,
username
,
password
,
batch_size
):
"""Generate devinfo files for devices.
Use --host (for a single device) or --target (for a complete network).
"""
if
debug
:
logging
.
basicConfig
(
level
=
logging
.
DEBUG
)
credentials
=
Credentials
(
username
=
username
,
password
=
password
)
if
host
is
not
None
:
click
.
echo
(
"Host given, performing discovery on %s."
%
host
)
device
=
await
Discover
.
discover_single
(
host
,
credentials
=
credentials
)
await
handle_device
(
basedir
,
autosave
,
device
,
batch_size
)
else
:
click
.
echo
(
"No --host given, performing discovery on %s. Use --target to override."
%
target
)
devices
=
await
Discover
.
discover
(
target
=
target
,
credentials
=
credentials
)
click
.
echo
(
"Detected %s devices"
%
len
(
devices
))
for
dev
in
devices
.
values
():
await
handle_device
(
basedir
,
autosave
,
dev
,
batch_size
)
async
def
get_legacy_fixture
(
device
):
"""Get fixture for legacy IOT style protocol."""
items
=
[
Call
(
module
=
"system"
,
method
=
"get_sysinfo"
),
Call
(
module
=
"emeter"
,
method
=
"get_realtime"
),
Call
(
module
=
"smartlife.iot.dimmer"
,
method
=
"get_dimmer_parameters"
),
Call
(
module
=
"smartlife.iot.common.emeter"
,
method
=
"get_realtime"
),
Call
(
module
=
"smartlife.iot.smartbulb.lightingservice"
,
method
=
"get_light_state"
),
Call
(
module
=
"smartlife.iot.LAS"
,
method
=
"get_config"
),
Call
(
module
=
"smartlife.iot.PIR"
,
method
=
"get_config"
),
]
successes
=
[]
for
test_call
in
items
:
try
:
click
.
echo
(
f"Testing
{
test_call
}
.."
,
nl
=
False
)
info
=
await
device
.
protocol
.
query
(
{
test_call
.
module
: {
test_call
.
method
:
None
}}
)
resp
=
info
[
test_call
.
module
]
except
Exception
as
ex
:
click
.
echo
(
click
.
style
(
f"FAIL
{
ex
}
"
,
fg
=
"red"
))
else
:
if
"err_msg"
in
resp
:
click
.
echo
(
click
.
style
(
f"FAIL
{
resp
[
'err_msg'
]
}
"
,
fg
=
"red"
))
else
:
click
.
echo
(
click
.
style
(
"OK"
,
fg
=
"green"
))
successes
.
append
((
test_call
,
info
))
final_query
=
defaultdict
(
defaultdict
)
final
=
defaultdict
(
defaultdict
)
for
succ
,
resp
in
successes
:
final_query
[
succ
.
module
][
succ
.
method
]
=
None
final
[
succ
.
module
][
succ
.
method
]
=
resp
final
=
default_to_regular
(
final
)
try
:
final
=
await
device
.
protocol
.
query
(
final_query
)
except
Exception
as
ex
:
_echo_error
(
f"Unable to query all successes at once:
{
ex
}
"
,
bold
=
True
,
fg
=
"red"
)
if
device
.
_discovery_info
and
not
device
.
_discovery_info
.
get
(
"system"
):
# Need to recreate a DiscoverResult here because we don't want the aliases
# in the fixture, we want the actual field names as returned by the device.
dr
=
DiscoveryResult
(
**
device
.
_discovery_info
)
final
[
"discovery_result"
]
=
dr
.
dict
(
by_alias
=
False
,
exclude_unset
=
True
,
exclude_none
=
True
,
exclude_defaults
=
True
)
click
.
echo
(
"Got %s successes"
%
len
(
successes
))
click
.
echo
(
click
.
style
(
"## device info file ##"
,
bold
=
True
))
sysinfo
=
final
[
"system"
][
"get_sysinfo"
]
model
=
sysinfo
[
"model"
]
hw_version
=
sysinfo
[
"hw_ver"
]
sw_version
=
sysinfo
[
"sw_ver"
]
sw_version
=
sw_version
.
split
(
" "
,
maxsplit
=
1
)[
0
]
save_filename
=
f"
{
model
}
_
{
hw_version
}
_
{
sw_version
}
.json"
copy_folder
=
"kasa/tests/fixtures/"
return
save_filename
,
copy_folder
,
final
def
_echo_error
(
msg
:
str
):
click
.
echo
(
click
.
style
(
msg
,
bold
=
True
,
fg
=
"red"
,
)
)
async
def
_make_requests_or_exit
(
device
:
SmartDevice
,
requests
:
List
[
SmartRequest
],
name
:
str
,
batch_size
:
int
,
)
->
Dict
[
str
,
Dict
]:
final
=
{}
try
:
end
=
len
(
requests
)
step
=
batch_size
# Break the requests down as there seems to be a size limit
for
i
in
range
(
0
,
end
,
step
):
x
=
i
requests_step
=
requests
[
x
:
x
+
step
]
request
:
Union
[
List
[
SmartRequest
],
SmartRequest
]
=
(
requests_step
[
0
]
if
len
(
requests_step
)
==
1
else
requests_step
)
responses
=
await
device
.
protocol
.
query
(
SmartRequest
.
_create_request_dict
(
request
)
)
for
method
,
result
in
responses
.
items
():
final
[
method
]
=
result
return
final
except
AuthenticationException
as
ex
:
_echo_error
(
f"Unable to query the device due to an authentication error:
{
ex
}
"
,
)
exit
(
1
)
except
SmartDeviceException
as
ex
:
_echo_error
(
f"Unable to query
{
name
}
at once:
{
ex
}
"
,
)
if
(
isinstance
(
ex
,
TimeoutException
)
or
ex
.
error_code
==
SmartErrorCode
.
SESSION_TIMEOUT_ERROR
):
_echo_error
(
"Timeout, try reducing the batch size via --batch-size option."
,
)
exit
(
1
)
except
Exception
as
ex
:
_echo_error
(
f"Unexpected exception querying
{
name
}
at once:
{
ex
}
"
,
)
exit
(
1
)
async
def
get_smart_fixture
(
device
:
TapoDevice
,
batch_size
:
int
):
"""Get fixture for new TAPO style protocol."""
extra_test_calls
=
[
SmartCall
(
module
=
"temp_humidity_records"
,
request
=
SmartRequest
.
get_raw_request
(
"get_temp_humidity_records"
),
should_succeed
=
False
,
),
SmartCall
(
module
=
"child_device_list"
,
request
=
SmartRequest
.
get_raw_request
(
"get_child_device_list"
),
should_succeed
=
False
,
),
SmartCall
(
module
=
"child_device_component_list"
,
request
=
SmartRequest
.
get_raw_request
(
"get_child_device_component_list"
),
should_succeed
=
False
,
),
SmartCall
(
module
=
"trigger_logs"
,
request
=
SmartRequest
.
get_raw_request
(
"get_trigger_logs"
,
SmartRequest
.
GetTriggerLogsParams
(
5
,
0
)
),
should_succeed
=
False
,
),
]
successes
=
[]
click
.
echo
(
"Testing component_nego call .."
,
nl
=
False
)
responses
=
await
_make_requests_or_exit
(
device
, [
SmartRequest
.
component_nego
()],
"component_nego call"
,
batch_size
)
component_info_response
=
responses
[
"component_nego"
]
click
.
echo
(
click
.
style
(
"OK"
,
fg
=
"green"
))
successes
.
append
(
SmartCall
(
module
=
"component_nego"
,
request
=
SmartRequest
(
"component_nego"
),
should_succeed
=
True
,
)
)
test_calls
=
[]
should_succeed
=
[]
for
item
in
component_info_response
[
"component_list"
]:
component_id
=
item
[
"id"
]
if
requests
:=
COMPONENT_REQUESTS
.
get
(
component_id
):
component_test_calls
=
[
SmartCall
(
module
=
component_id
,
request
=
request
,
should_succeed
=
True
)
for
request
in
requests
]
test_calls
.
extend
(
component_test_calls
)
should_succeed
.
extend
(
component_test_calls
)
elif
component_id
not
in
COMPONENT_REQUESTS
:
click
.
echo
(
f"Skipping
{
component_id
}
.."
,
nl
=
False
)
click
.
echo
(
click
.
style
(
"UNSUPPORTED"
,
fg
=
"yellow"
))
test_calls
.
extend
(
extra_test_calls
)
for
test_call
in
test_calls
:
click
.
echo
(
f"Testing
{
test_call
.
module
}
.."
,
nl
=
False
)
try
:
click
.
echo
(
f"Testing
{
test_call
}
.."
,
nl
=
False
)
response
=
await
device
.
protocol
.
query
(
SmartRequest
.
_create_request_dict
(
test_call
.
request
)
)
except
AuthenticationException
as
ex
:
_echo_error
(
f"Unable to query the device due to an authentication error:
{
ex
}
"
,
)
exit
(
1
)
except
Exception
as
ex
:
if
(
not
test_call
.
should_succeed
and
hasattr
(
ex
,
"error_code"
)
and
ex
.
error_code
==
SmartErrorCode
.
UNKNOWN_METHOD_ERROR
):
click
.
echo
(
click
.
style
(
"FAIL - EXPECTED"
,
fg
=
"green"
))
else
:
click
.
echo
(
click
.
style
(
f"FAIL
{
ex
}
"
,
fg
=
"red"
))
else
:
if
not
response
:
click
.
echo
(
click
.
style
(
"FAIL no response"
,
fg
=
"red"
))
else
:
if
not
test_call
.
should_succeed
:
click
.
echo
(
click
.
style
(
"OK - EXPECTED FAIL"
,
fg
=
"red"
))
else
:
click
.
echo
(
click
.
style
(
"OK"
,
fg
=
"green"
))
successes
.
append
(
test_call
)
requests
=
[]
for
succ
in
successes
:
requests
.
append
(
succ
.
request
)
final
=
await
_make_requests_or_exit
(
device
,
requests
,
"all successes at once"
,
batch_size
)
# Need to recreate a DiscoverResult here because we don't want the aliases
# in the fixture, we want the actual field names as returned by the device.
dr
=
DiscoveryResult
(
**
device
.
_discovery_info
)
# type: ignore
final
[
"discovery_result"
]
=
dr
.
dict
(
by_alias
=
False
,
exclude_unset
=
True
,
exclude_none
=
True
,
exclude_defaults
=
True
)
click
.
echo
(
"Got %s successes"
%
len
(
successes
))
click
.
echo
(
click
.
style
(
"## device info file ##"
,
bold
=
True
))
hw_version
=
final
[
"get_device_info"
][
"hw_ver"
]
sw_version
=
final
[
"get_device_info"
][
"fw_ver"
]
model
=
final
[
"discovery_result"
][
"device_model"
]
sw_version
=
sw_version
.
split
(
" "
,
maxsplit
=
1
)[
0
]
save_filename
=
f"
{
model
}
_
{
hw_version
}
_
{
sw_version
}
.json"
copy_folder
=
"kasa/tests/fixtures/smart/"
return
save_filename
,
copy_folder
,
final
if
__name__
==
"__main__"
:
cli
()
Back
|
FazBrowse Home
|
New Git URL