core/script/hassfest/ssdp.py

90 lines
2.5 KiB
Python
Raw Normal View History

"""Generate ssdp file."""
from collections import OrderedDict, defaultdict
import json
from typing import Dict
from .model import Integration, Config
BASE = """
\"\"\"Automatically generated by hassfest.
To update, run python3 -m script.hassfest
\"\"\"
# fmt: off
SSDP = {}
""".strip()
def sort_dict(value):
"""Sort a dictionary."""
2019-07-31 19:25:30 +00:00
return OrderedDict((key, value[key]) for key in sorted(value))
def generate_and_validate(integrations: Dict[str, Integration]):
"""Validate and generate ssdp data."""
data = {
2019-07-31 19:25:30 +00:00
"st": defaultdict(list),
"manufacturer": defaultdict(list),
"device_type": defaultdict(list),
}
for domain in sorted(integrations):
integration = integrations[domain]
if not integration.manifest:
continue
2019-07-31 19:25:30 +00:00
ssdp = integration.manifest.get("ssdp")
if not ssdp:
continue
try:
with open(str(integration.path / "config_flow.py")) as fp:
content = fp.read()
2019-07-31 19:25:30 +00:00
if (
" async_step_ssdp" not in content
and "register_discovery_flow" not in content
):
integration.add_error("ssdp", "Config flow has no async_step_ssdp")
continue
except FileNotFoundError:
integration.add_error(
2019-07-31 19:25:30 +00:00
"ssdp", "SSDP info in a manifest requires a config flow to exist"
)
continue
2019-07-31 19:25:30 +00:00
for key in "st", "manufacturer", "device_type":
if key not in ssdp:
continue
for value in ssdp[key]:
data[key][value].append(domain)
data = sort_dict({key: sort_dict(value) for key, value in data.items()})
return BASE.format(json.dumps(data, indent=4))
def validate(integrations: Dict[str, Integration], config: Config):
"""Validate ssdp file."""
2019-07-31 19:25:30 +00:00
ssdp_path = config.root / "homeassistant/generated/ssdp.py"
config.cache["ssdp"] = content = generate_and_validate(integrations)
2019-07-31 19:25:30 +00:00
with open(str(ssdp_path), "r") as fp:
if fp.read().strip() != content:
config.add_error(
"ssdp",
2019-07-31 19:25:30 +00:00
"File ssdp.py is not up to date. " "Run python3 -m script.hassfest",
fixable=True,
)
return
def generate(integrations: Dict[str, Integration], config: Config):
"""Generate ssdp file."""
2019-07-31 19:25:30 +00:00
ssdp_path = config.root / "homeassistant/generated/ssdp.py"
with open(str(ssdp_path), "w") as fp:
fp.write(config.cache["ssdp"] + "\n")