FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-kasa/devtools/generate_supported.py at 0.11.0.1 · python-kasa/python-kasa · GitHub
python-kasa
python-kasa
Repository navigation
Code
Issues
54
(54)
Pull requests
42
(42)
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-kasa
/
devtools
/
generate_supported.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
236 lines (199 loc) · 7.89 KB
Breadcrumbs
python-kasa
/
devtools
/
generate_supported.py
Copy path
File metadata and controls
executable file
·
236 lines (199 loc) · 7.89 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
#!/usr/bin/env python
"""Script that checks supported devices and updates README.md and SUPPORTED.md."""
from
__future__
import
annotations
import
json
import
os
import
sys
from
pathlib
import
Path
from
string
import
Template
from
typing
import
Any
,
NamedTuple
from
kasa
.
device_type
import
DeviceType
from
kasa
.
iot
import
IotDevice
from
kasa
.
smart
import
SmartDevice
from
kasa
.
smartcam
import
SmartCamChild
,
SmartCamDevice
class
SupportedVersion
(
NamedTuple
):
"""Supported version."""
region
:
str
|
None
hw
:
str
fw
:
str
auth
:
bool
# The order of devices in this dict drives the display order
DEVICE_TYPE_TO_PRODUCT_GROUP
=
{
DeviceType
.
Plug
:
"Plugs"
,
DeviceType
.
Strip
:
"Power Strips"
,
DeviceType
.
StripSocket
:
"Power Strips"
,
DeviceType
.
Dimmer
:
"Wall Switches"
,
DeviceType
.
WallSwitch
:
"Wall Switches"
,
DeviceType
.
Fan
:
"Wall Switches"
,
DeviceType
.
Bulb
:
"Bulbs"
,
DeviceType
.
LightStrip
:
"Light Strips"
,
DeviceType
.
Camera
:
"Cameras"
,
DeviceType
.
Doorbell
:
"Doorbells and chimes"
,
DeviceType
.
Chime
:
"Doorbells and chimes"
,
DeviceType
.
Vacuum
:
"Vacuums"
,
DeviceType
.
Hub
:
"Hubs"
,
DeviceType
.
Sensor
:
"Hub-Connected Devices"
,
DeviceType
.
Thermostat
:
"Hub-Connected Devices"
,
}
SUPPORTED_FILENAME
=
"SUPPORTED.md"
README_FILENAME
=
"README.md"
IOT_FOLDER
=
"tests/fixtures/iot/"
SMART_FOLDER
=
"tests/fixtures/smart/"
SMART_CHILD_FOLDER
=
"tests/fixtures/smart/child"
SMARTCAM_FOLDER
=
"tests/fixtures/smartcam/"
SMARTCAM_CHILD_FOLDER
=
"tests/fixtures/smartcam/child"
def
generate_supported
(
args
):
"""Generate the SUPPORTED.md from the fixtures."""
print_diffs
=
"--print-diffs"
in
args
running_in_ci
=
"CI"
in
os
.
environ
print
(
"Generating supported devices"
)
if
running_in_ci
:
print_diffs
=
True
print
(
"Detected running in CI"
)
supported
=
{
"kasa"
: {},
"tapo"
: {}}
_get_supported_devices
(
supported
,
IOT_FOLDER
,
IotDevice
)
_get_supported_devices
(
supported
,
SMART_FOLDER
,
SmartDevice
)
_get_supported_devices
(
supported
,
SMART_CHILD_FOLDER
,
SmartDevice
)
_get_supported_devices
(
supported
,
SMARTCAM_FOLDER
,
SmartCamDevice
)
_get_supported_devices
(
supported
,
SMARTCAM_CHILD_FOLDER
,
SmartCamChild
)
readme_updated
=
_update_supported_file
(
README_FILENAME
,
_supported_summary
(
supported
),
print_diffs
)
supported_updated
=
_update_supported_file
(
SUPPORTED_FILENAME
,
_supported_detail
(
supported
),
print_diffs
)
if
not
readme_updated
and
not
supported_updated
:
print
(
"Supported devices unchanged."
)
def
_update_supported_file
(
filename
,
supported_text
,
print_diffs
)
->
bool
:
with
open
(
filename
)
as
f
:
contents
=
f
.
readlines
()
start_index
=
end_index
=
None
for
index
,
line
in
enumerate
(
contents
):
if
line
==
"<!--SUPPORTED_START-->
\n
"
:
start_index
=
index
+
1
if
line
==
"<!--SUPPORTED_END-->
\n
"
:
end_index
=
index
current_text
=
""
.
join
(
contents
[
start_index
:
end_index
])
if
current_text
!=
supported_text
:
print
(
f"
{
filename
}
has been modified with updated "
+
"supported devices, add file to commit."
)
if
print_diffs
:
print
(
"##CURRENT##"
)
print
(
current_text
)
print
(
"##NEW##"
)
print
(
supported_text
)
new_contents
=
contents
[:
start_index
]
end_contents
=
contents
[
end_index
:]
new_contents
.
append
(
supported_text
)
new_contents
.
extend
(
end_contents
)
with
open
(
filename
,
"w"
)
as
f
:
new_contents_text
=
""
.
join
(
new_contents
)
f
.
write
(
new_contents_text
)
return
True
return
False
def
_supported_summary
(
supported
):
return
_supported_text
(
supported
,
"### Supported $brand$auth devices
\n
\n
$types
\n
"
,
"- **$type_$type_asterix**: $models
\n
"
,
)
def
_supported_detail
(
supported
):
return
_supported_text
(
supported
,
"## $brand devices
\n
\n
$preamble
\n
\n
$types
\n
"
,
"### $type_
\n
\n
$models
\n
"
,
"- **$model**
\n
$versions"
,
" - Hardware: $hw$region / Firmware: $fw$auth_flag
\n
"
,
)
def
_supported_text
(
supported
,
brand_template
,
types_template
,
model_template
=
""
,
version_template
=
""
):
brandt
=
Template
(
brand_template
)
typest
=
Template
(
types_template
)
modelt
=
Template
(
model_template
)
versst
=
Template
(
version_template
)
brands
=
""
version
:
SupportedVersion
for
brand
,
types
in
supported
.
items
():
preamble_text
=
(
"Some newer Kasa devices require authentication. "
+
"These are marked with [^1] in the list below."
if
brand
==
"kasa"
else
"All Tapo devices require authentication."
)
preamble_text
+=
(
"<br>Hub-Connected Devices may work across TAPO/KASA branded "
+
"hubs even if they don't work across the native apps."
)
brand_text
=
brand
.
capitalize
()
brand_auth
=
r"[^1]"
if
brand
==
"tapo"
else
""
types_text
=
""
for
supported_type
,
models
in
sorted
(
# Sort by device type order in the enum
types
.
items
(),
key
=
lambda
st
:
list
(
DEVICE_TYPE_TO_PRODUCT_GROUP
.
values
()).
index
(
st
[
0
]),
):
models_list
=
[]
models_text
=
""
for
model
,
versions
in
sorted
(
models
.
items
()):
auth_count
=
0
versions_text
=
""
for
version
in
sorted
(
versions
):
region_text
=
f" (
{
version
.
region
}
)"
if
version
.
region
else
""
auth_count
+=
1
if
version
.
auth
else
0
vauth_flag
=
r"[^1]"
if
version
.
auth
and
brand
==
"kasa"
else
""
if
version_template
:
versions_text
+=
versst
.
substitute
(
hw
=
version
.
hw
,
fw
=
version
.
fw
,
region
=
region_text
,
auth_flag
=
vauth_flag
,
)
if
brand
==
"kasa"
and
auth_count
>
0
:
auth_flag
=
r"[^1]"
if
auth_count
==
len
(
versions
)
else
r"[^2]"
else
:
auth_flag
=
""
if
model_template
:
models_text
+=
modelt
.
substitute
(
model
=
model
,
versions
=
versions_text
,
auth_flag
=
auth_flag
)
else
:
models_list
.
append
(
f"
{
model
}
{
auth_flag
}
"
)
models_text
=
models_text
if
models_text
else
", "
.
join
(
models_list
)
type_asterix
=
r"[^3]"
if
supported_type
==
"Hub-Connected Devices"
else
""
types_text
+=
typest
.
substitute
(
type_
=
supported_type
,
type_asterix
=
type_asterix
,
models
=
models_text
)
brands
+=
brandt
.
substitute
(
brand
=
brand_text
,
types
=
types_text
,
auth
=
brand_auth
,
preamble
=
preamble_text
)
return
brands
def
_get_supported_devices
(
supported
:
dict
[
str
,
Any
],
fixture_location
:
str
,
device_cls
:
type
[
IotDevice
|
SmartDevice
|
SmartCamDevice
],
):
for
file
in
Path
(
fixture_location
).
glob
(
"*.json"
):
with
file
.
open
()
as
f
:
fixture_data
=
json
.
load
(
f
)
model_info
=
device_cls
.
_get_device_info
(
fixture_data
,
fixture_data
.
get
(
"discovery_result"
, {}).
get
(
"result"
)
)
supported_type
=
DEVICE_TYPE_TO_PRODUCT_GROUP
[
model_info
.
device_type
]
stype
=
supported
[
model_info
.
brand
].
setdefault
(
supported_type
, {})
smodel
=
stype
.
setdefault
(
model_info
.
long_name
, [])
smodel
.
append
(
SupportedVersion
(
region
=
model_info
.
region
if
model_info
.
region
else
""
,
hw
=
model_info
.
hardware_version
,
fw
=
model_info
.
firmware_version
,
auth
=
model_info
.
requires_auth
,
)
)
def
main
():
"""Entry point to module."""
generate_supported
(
sys
.
argv
[
1
:])
if
__name__
==
"__main__"
:
generate_supported
(
sys
.
argv
[
1
:])
Back
|
FazBrowse Home
|
New Git URL