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
|
|
|
|
|
2019-12-09 16:24:03 +01:00
|
|
|
from .model import Config, Integration
|
2022-11-09 17:58:20 +02:00
|
|
|
from .serializer import format_python_namespace
|
2019-05-26 19:48:27 -07:00
|
|
|
|
|
|
|
|
2022-11-23 20:05:31 +02:00
|
|
|
def generate_and_validate(integrations: dict[str, Integration]) -> str:
|
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):
|
2022-11-28 16:33:14 +02:00
|
|
|
ssdp = integrations[domain].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:
|
2022-11-09 17:58:20 +02:00
|
|
|
data[domain].append(matcher)
|
2019-05-26 19:48:27 -07:00
|
|
|
|
2022-11-09 17:58:20 +02:00
|
|
|
return format_python_namespace({"SSDP": data})
|
2019-05-26 19:48:27 -07:00
|
|
|
|
|
|
|
|
2022-11-23 20:05:31 +02:00
|
|
|
def validate(integrations: dict[str, Integration], config: Config) -> None:
|
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
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-11-23 20:05:31 +02:00
|
|
|
def generate(integrations: dict[str, Integration], config: Config) -> None:
|
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']}")
|