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

@ -19,3 +19,6 @@ uv pip install --upgrade mypy-dev
# Workaround for aiodns/pycares compatibility issue
# See: https://github.com/aio-libs/aiodns/issues/214
uv pip install --upgrade aiodns
# README code dependencies
uv pip install markdown-code-runner==2.1.0 pandas tabulate

7
scripts/sync-strings Executable file
View file

@ -0,0 +1,7 @@
#!/usr/bin/env bash
set -ex
cd "$(dirname "$0")/.."
uv run markdown-code-runner --verbose README.md
uv run python ./scripts/update-services.py
uv run python ./scripts/update-strings.py

View file

@ -0,0 +1,27 @@
"""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)

67
scripts/update-strings.py Normal file
View file

@ -0,0 +1,67 @@
"""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")