core/script/hassfest/dhcp.py

51 lines
1.5 KiB
Python
Raw Normal View History

"""Generate dhcp file."""
2021-03-18 21:58:19 +00:00
from __future__ import annotations
from .model import Config, Integration
2022-11-09 15:58:20 +00:00
from .serializer import format_python_namespace
2022-11-23 18:05:31 +00:00
def generate_and_validate(integrations: dict[str, Integration]) -> str:
"""Validate and generate dhcp data."""
match_list = []
for domain in sorted(integrations):
match_types = integrations[domain].manifest.get("dhcp", [])
if not match_types:
continue
for entry in match_types:
match_list.append({"domain": domain, **entry})
2022-11-09 15:58:20 +00:00
return format_python_namespace(
{"DHCP": match_list},
annotations={"DHCP": "list[dict[str, str | bool]]"},
)
2022-11-23 18:05:31 +00:00
def validate(integrations: dict[str, Integration], config: Config) -> None:
"""Validate dhcp file."""
dhcp_path = config.root / "homeassistant/generated/dhcp.py"
config.cache["dhcp"] = content = generate_and_validate(integrations)
if config.specific_integrations:
return
with open(str(dhcp_path)) as fp:
current = fp.read()
if current != content:
config.add_error(
"dhcp",
"File dhcp.py is not up to date. Run python3 -m script.hassfest",
fixable=True,
)
return
2022-11-23 18:05:31 +00:00
def generate(integrations: dict[str, Integration], config: Config) -> None:
"""Generate dhcp file."""
dhcp_path = config.root / "homeassistant/generated/dhcp.py"
with open(str(dhcp_path), "w") as fp:
fp.write(f"{config.cache['dhcp']}")