build: extract strings sync script for local use

This commit is contained in:
Mario Guggenberger 2026-01-10 12:50:31 +00:00
commit 11bd4d214e
No known key found for this signature in database
GPG key ID: 4BF213E6EF2D7EA1
7 changed files with 19 additions and 12 deletions

View file

@ -1,27 +0,0 @@
"""Creates a services.yaml file with the latest docs."""
import sys
from pathlib import Path
import yaml
sys.path.append(str(Path(__file__).parent.parent))
from custom_components.adaptive_lighting import const
services_filename = Path("custom_components") / "adaptive_lighting" / "services.yaml"
with open(services_filename) as f: # noqa: PTH123
services = yaml.safe_load(f)
for service_name, dct in services.items():
_docs = {"set_manual_control": const.DOCS_MANUAL_CONTROL, "apply": const.DOCS_APPLY}
alternative_docs = _docs.get(service_name, const.DOCS)
for field_name, field in dct["fields"].items():
description = alternative_docs.get(field_name, const.DOCS[field_name])
field["description"] = description
comment = "# This file is auto-generated by .github/update-services.py."
with services_filename.open("w") as f:
f.write(comment + "\n")
yaml.dump(services, f, sort_keys=False, width=1000, allow_unicode=True)

View file

@ -1,67 +0,0 @@
"""Update strings.json and en.json from const.py."""
import json
import sys
from pathlib import Path
import homeassistant.helpers.config_validation as cv
import yaml
sys.path.append(str(Path(__file__).parent.parent))
from custom_components.adaptive_lighting import const
folder = Path("custom_components") / "adaptive_lighting"
strings_fname = folder / "strings.json"
en_fname = folder / "translations" / "en.json"
with strings_fname.open() as f:
strings = json.load(f)
# Set "options"
data = {}
data_description = {}
for k, _, typ in const.VALIDATION_TUPLES:
desc = const.DOCS[k]
if len(desc) > 40 and typ not in (bool, cv.entity_ids):
data[k] = k
data_description[k] = desc
else:
data[k] = f"{k}: {desc}"
strings["options"]["step"]["init"]["data"] = data
strings["options"]["step"]["init"]["data_description"] = data_description
# Set "services"
services_filename = Path("custom_components") / "adaptive_lighting" / "services.yaml"
with open(services_filename) as f: # noqa: PTH123
services = yaml.safe_load(f)
services_json = {}
for service_name, dct in services.items():
services_json[service_name] = {
"name": service_name,
"description": dct["description"],
"fields": {},
}
for field_name, field in dct["fields"].items():
services_json[service_name]["fields"][field_name] = {
"description": field["description"],
"name": field_name,
}
strings["services"] = services_json
# Write changes to strings.json
with strings_fname.open("w") as f:
json.dump(strings, f, indent=2, ensure_ascii=False)
f.write("\n")
# Sync changes from strings.json to en.json
with en_fname.open() as f:
en = json.load(f)
en["config"]["step"]["user"] = strings["config"]["step"]["user"]
en["options"]["step"]["init"]["data"] = data
en["options"]["step"]["init"]["data_description"] = data_description
en["services"] = services_json
with en_fname.open("w") as f:
json.dump(en, f, indent=2, ensure_ascii=False)
f.write("\n")

View file

@ -22,18 +22,8 @@ jobs:
with:
python-version: "3.13"
- name: Install markdown-code-runner and README code dependencies
run: |
uv pip install markdown-code-runner==2.1.0 pandas tabulate
- name: Run markdown-code-runner
run: uv run markdown-code-runner --verbose README.md
- name: Run update services.yaml
run: uv run python .github/update-services.py
- name: Run update strings.json
run: uv run python .github/update-strings.py
- name: Run sync script
run: ./scripts/sync-strings
- name: Commit updated README.md, strings.json, and services.yaml
id: commit