Skip to content

Commit 95f9122

Browse files
Library workflow refactor (#3928)
Co-authored-by: Andrew Jackson <[email protected]>
1 parent 455f495 commit 95f9122

24 files changed

+497
-154
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ updates:
1212
interval: "weekly"
1313
ignore:
1414
# Dependabot should not update Home Assistant as that should match the homeassistant key in hacs.json
15-
- dependency-name: "homeassistant"
15+
- dependency-name: "homeassistant"
16+
- dependency-name: "mkdocs-material"
17+
- dependency-name: "pytablewriter"

.github/scripts/library_doc/requirements.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,43 @@
11
"""Battery library document generator."""
22

3-
from __future__ import annotations
4-
53
import json
64

75
from pytablewriter import MarkdownTableWriter
86

97

10-
def generate_device_list():
11-
"""Generate static file containing the device library."""
12-
8+
def get_device_list() -> None:
9+
"""Sort the device library JSON file."""
1310
# Load the existing JSON library file
1411
with open(
15-
"library/library.json", encoding="UTF-8"
12+
file="library/library.json",
13+
encoding="UTF-8",
1614
) as f:
1715
devices_json = json.loads(f.read())
1816
devices = devices_json.get("devices")
1917

20-
toc_links: list[str] = []
21-
tables_output: str = ""
22-
rows = []
23-
24-
num_devices = len(devices)
25-
26-
writer = MarkdownTableWriter()
27-
headers = [
28-
"Manufacturer",
29-
"Model",
30-
"Battery Type",
31-
"Model ID (optional)",
32-
"HW Version (optional)",
33-
]
18+
# Sort the devices by manufacturer and model
19+
devices.sort(
20+
key=lambda k: (
21+
k["manufacturer"].lower(),
22+
k.get("model_match_method", "").lower(),
23+
k["model"].lower(),
24+
k.get("model_id", "").lower(),
25+
k.get("hw_version", "").lower(),
26+
)
27+
)
28+
with open("library/library.json", "w", encoding="UTF-8") as f:
29+
f.write(json.dumps(devices_json, indent=4))
30+
31+
32+
def regenerate_device_list() -> None:
33+
"""Generate static file containing the device library."""
3434

35-
writer.header_list = headers
35+
# Load the existing JSON library file
36+
with open("library/library.json", encoding="UTF-8") as file:
37+
devices_json = json.loads(file.read())
38+
devices = devices_json.get("devices")
3639

40+
rows = []
3741
for device in devices:
3842
if device.get("battery_quantity", 1) > 1:
3943
battery_type_qty = f"{device['battery_quantity']}× {device['battery_type']}"
@@ -58,15 +62,28 @@ def generate_device_list():
5862
]
5963
rows.append(row)
6064

65+
writer = MarkdownTableWriter()
66+
writer.header_list = [
67+
"Manufacturer",
68+
"Model",
69+
"Battery Type",
70+
"Model ID (optional)",
71+
"HW Version (optional)",
72+
]
6173
writer.value_matrix = rows
62-
tables_output += f"## {num_devices} Devices in library\n\n"
63-
tables_output += "This file is auto generated, do not modify\n\n"
64-
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"
65-
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())
6681

6782
with open("library.md", "w", encoding="UTF-8") as md_file:
68-
md_file.write("".join(toc_links) + tables_output)
83+
md_file.write("".join(tables_output))
6984
md_file.close()
7085

7186

72-
generate_device_list()
87+
if __name__ == "__main__":
88+
get_device_list()
89+
regenerate_device_list()

.github/workflows/deploy_docs.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Deploy docs
2+
on:
3+
push:
4+
paths:
5+
- 'mkdocs.yaml'
6+
- 'docs/**'
7+
branches:
8+
- main
9+
permissions:
10+
contents: write
11+
jobs:
12+
deploy_docs:
13+
name: Deploy docs
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v5
18+
19+
- uses: actions/cache@v4
20+
with:
21+
key: mkdocs-material-${{ runner.os }}-${{ hashFiles('mkdocs.yaml', 'pyproject.toml', 'uv.lock') }}
22+
path: .cache
23+
restore-keys: |
24+
mkdocs-material-
25+
26+
- name: Install uv
27+
uses: astral-sh/setup-uv@v7
28+
29+
- name: Deploy documentation
30+
run: |
31+
uv run --only-group docs mkdocs gh-deploy --force

0 commit comments

Comments
 (0)