Skip to content

Commit f189b70

Browse files
committed
refactor regenerate_device_list()
1 parent 90a8c52 commit f189b70

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

.github/scripts/library_doc/update_library_files.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,11 @@ def regenerate_device_list() -> None:
3333
"""Generate static file containing the device library."""
3434

3535
# Load the existing JSON library file
36-
with open("library/library.json", encoding="UTF-8") as f:
37-
devices_json = json.loads(f.read())
36+
with open("library/library.json", encoding="UTF-8") as file:
37+
devices_json = json.loads(file.read())
3838
devices = devices_json.get("devices")
3939

40-
toc_links: list[str] = []
41-
tables_output: str = ""
4240
rows = []
43-
44-
num_devices = len(devices)
45-
46-
writer = MarkdownTableWriter()
47-
headers = [
48-
"Manufacturer",
49-
"Model",
50-
"Battery Type",
51-
"Model ID (optional)",
52-
"HW Version (optional)",
53-
]
54-
55-
writer.header_list = headers
56-
5741
for device in devices:
5842
if device.get("battery_quantity", 1) > 1:
5943
battery_type_qty = f"{device['battery_quantity']}× {device['battery_type']}"
@@ -78,14 +62,25 @@ def regenerate_device_list() -> None:
7862
]
7963
rows.append(row)
8064

65+
writer = MarkdownTableWriter()
66+
writer.header_list = [
67+
"Manufacturer",
68+
"Model",
69+
"Battery Type",
70+
"Model ID (optional)",
71+
"HW Version (optional)",
72+
]
8173
writer.value_matrix = rows
82-
tables_output += f"## {num_devices} Devices in library\n\n"
83-
tables_output += "This file is auto generated, do not modify\n\n"
84-
tables_output += "Request new devices to be added to the library [here](https://github.com/andrew-codechimp/HA-Battery-Notes/issues/new?template=new_device_request.yml&title=%5BDevice%5D%3A+)\n\n"
85-
tables_output += writer.dumps()
74+
75+
tables_output = [f"## {len(devices)} Devices in library\n\n"]
76+
tables_output.append("This file is auto generated, do not modify\n\n")
77+
tables_output.append(
78+
"Request new devices to be added to the library [here](https://github.com/andrew-codechimp/HA-Battery-Notes/issues/new?template=new_device_request.yml&title=%5BDevice%5D%3A+)\n\n"
79+
)
80+
tables_output.append(writer.dumps())
8681

8782
with open("library.md", "w", encoding="UTF-8") as md_file:
88-
md_file.write("".join(toc_links) + tables_output)
83+
md_file.write("".join(tables_output))
8984
md_file.close()
9085

9186

0 commit comments

Comments
 (0)