2019-05-26 19:48:27 -07:00
|
|
|
"""Generate ssdp file."""
|
2021-03-18 22:58:19 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-09-19 23:37:22 +02:00
|
|
|
from collections import defaultdict
|
|
|
|
|
|
|
|
import black
|
2019-05-26 19:48:27 -07:00
|
|
|
|
2019-12-09 16:24:03 +01:00
|
|
|
from .model import Config, Integration
|
2022-09-19 23:37:22 +02:00
|
|
|
from .serializer import to_string
|
2019-05-26 19:48:27 -07:00
|
|
|
|
|
|
|
BASE = """
|
|
|
|
\"\"\"Automatically generated by hassfest.
|
|
|
|
|
2019-05-30 18:41:30 +02:00
|
|
|
To update, run python3 -m script.hassfest
|
2019-05-26 19:48:27 -07:00
|
|
|
\"\"\"
|
|
|
|
|
|
|
|
SSDP = {}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
|
|
|
|
def sort_dict(value):
|
|
|
|
"""Sort a dictionary."""
|
2022-09-19 23:37:22 +02:00
|
|
|
return {key: value[key] for key in sorted(value)}
|
2019-05-26 19:48:27 -07:00
|
|
|
|
|
|
|
|
2021-03-18 22:58:19 +01:00
|
|
|
def generate_and_validate(integrations: dict[str, Integration]):
|
2019-05-26 19:48:27 -07:00
|
|
|
"""Validate and generate ssdp data."""
|
2019-11-02 21:30:09 +02:00
|
|
|
|
|
|
|
data = defaultdict(list)
|
2019-05-26 19:48:27 -07:00
|
|
|
|
|
|
|
for domain in sorted(integrations):
|
|
|
|
integration = integrations[domain]
|
|
|
|
|
2021-08-16 07:28:26 -07:00
|
|
|
if not integration.manifest or not integration.config_flow:
|
2019-05-26 19:48:27 -07:00
|
|
|
continue
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
ssdp = integration.manifest.get("ssdp")
|
2019-05-26 19:48:27 -07:00
|
|
|
|
|
|
|
if not ssdp:
|
|
|
|
continue
|
|
|
|
|
2019-11-02 21:30:09 +02:00
|
|
|
for matcher in ssdp:
|
|
|
|
data[domain].append(sort_dict(matcher))
|
2019-05-26 19:48:27 -07:00
|
|
|
|
2022-09-19 23:37:22 +02:00
|
|
|
return black.format_str(BASE.format(to_string(data)), mode=black.Mode())
|
2019-05-26 19:48:27 -07:00
|
|
|
|
|
|
|
|
2021-03-18 22:58:19 +01:00
|
|
|
def validate(integrations: dict[str, Integration], config: Config):
|
2019-05-26 19:48:27 -07:00
|
|
|
"""Validate ssdp file."""
|
2019-07-31 12:25:30 -07:00
|
|
|
ssdp_path = config.root / "homeassistant/generated/ssdp.py"
|
|
|
|
config.cache["ssdp"] = content = generate_and_validate(integrations)
|
2019-05-26 19:48:27 -07:00
|
|
|
|
2020-04-16 09:00:04 -07:00
|
|
|
if config.specific_integrations:
|
|
|
|
return
|
|
|
|
|
2020-04-05 12:49:57 +02:00
|
|
|
with open(str(ssdp_path)) as fp:
|
2022-09-19 23:37:22 +02:00
|
|
|
if fp.read() != content:
|
2019-05-26 19:48:27 -07:00
|
|
|
config.add_error(
|
|
|
|
"ssdp",
|
2020-01-02 21:17:10 +02:00
|
|
|
"File ssdp.py is not up to date. Run python3 -m script.hassfest",
|
2019-07-31 12:25:30 -07:00
|
|
|
fixable=True,
|
2019-05-26 19:48:27 -07:00
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2021-03-18 22:58:19 +01:00
|
|
|
def generate(integrations: dict[str, Integration], config: Config):
|
2019-05-26 19:48:27 -07:00
|
|
|
"""Generate ssdp file."""
|
2019-07-31 12:25:30 -07:00
|
|
|
ssdp_path = config.root / "homeassistant/generated/ssdp.py"
|
|
|
|
with open(str(ssdp_path), "w") as fp:
|
2022-09-19 23:37:22 +02:00
|
|
|
fp.write(f"{config.cache['ssdp']}")
|