2019-05-27 02:48:27 +00:00
|
|
|
"""Generate ssdp file."""
|
|
|
|
from collections import OrderedDict, defaultdict
|
|
|
|
import json
|
|
|
|
from typing import Dict
|
|
|
|
|
2019-12-09 15:24:03 +00:00
|
|
|
from .model import Config, Integration
|
2019-05-27 02:48:27 +00:00
|
|
|
|
|
|
|
BASE = """
|
|
|
|
\"\"\"Automatically generated by hassfest.
|
|
|
|
|
2019-05-30 16:41:30 +00:00
|
|
|
To update, run python3 -m script.hassfest
|
2019-05-27 02:48:27 +00:00
|
|
|
\"\"\"
|
|
|
|
|
2019-09-09 19:01:49 +00:00
|
|
|
# fmt: off
|
2019-05-27 02:48:27 +00:00
|
|
|
|
|
|
|
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))
|
2019-05-27 02:48:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def generate_and_validate(integrations: Dict[str, Integration]):
|
|
|
|
"""Validate and generate ssdp data."""
|
2019-11-02 19:30:09 +00:00
|
|
|
|
|
|
|
data = defaultdict(list)
|
2019-05-27 02:48:27 +00:00
|
|
|
|
|
|
|
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")
|
2019-05-27 02:48:27 +00:00
|
|
|
|
|
|
|
if not ssdp:
|
|
|
|
continue
|
|
|
|
|
2019-11-02 19:30:09 +00:00
|
|
|
for matcher in ssdp:
|
|
|
|
data[domain].append(sort_dict(matcher))
|
2019-05-27 02:48:27 +00:00
|
|
|
|
|
|
|
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-05-27 02:48:27 +00:00
|
|
|
|
2020-04-16 16:00:04 +00:00
|
|
|
if config.specific_integrations:
|
|
|
|
return
|
|
|
|
|
2020-04-05 10:49:57 +00:00
|
|
|
with open(str(ssdp_path)) as fp:
|
2019-05-27 02:48:27 +00:00
|
|
|
if fp.read().strip() != content:
|
|
|
|
config.add_error(
|
|
|
|
"ssdp",
|
2020-01-02 19:17:10 +00:00
|
|
|
"File ssdp.py is not up to date. Run python3 -m script.hassfest",
|
2019-07-31 19:25:30 +00:00
|
|
|
fixable=True,
|
2019-05-27 02:48:27 +00:00
|
|
|
)
|
|
|
|
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:
|
2020-04-04 18:17:11 +00:00
|
|
|
fp.write(f"{config.cache['ssdp']}\n")
|