2019-05-21 22:36:26 +00:00
|
|
|
"""Generate zeroconf file."""
|
2019-05-31 18:58:48 +00:00
|
|
|
from collections import OrderedDict, defaultdict
|
2019-05-21 22:36:26 +00:00
|
|
|
import json
|
|
|
|
from typing import Dict
|
|
|
|
|
|
|
|
from .model import Integration, Config
|
|
|
|
|
|
|
|
BASE = """
|
|
|
|
\"\"\"Automatically generated by hassfest.
|
|
|
|
|
2019-05-30 16:41:30 +00:00
|
|
|
To update, run python3 -m script.hassfest
|
2019-05-21 22:36:26 +00:00
|
|
|
\"\"\"
|
|
|
|
|
|
|
|
|
2019-05-29 21:20:06 +00:00
|
|
|
ZEROCONF = {}
|
2019-05-31 18:58:48 +00:00
|
|
|
|
|
|
|
HOMEKIT = {}
|
2019-05-21 22:36:26 +00:00
|
|
|
""".strip()
|
|
|
|
|
|
|
|
|
|
|
|
def generate_and_validate(integrations: Dict[str, Integration]):
|
|
|
|
"""Validate and generate zeroconf data."""
|
2019-05-31 18:58:48 +00:00
|
|
|
service_type_dict = defaultdict(list)
|
|
|
|
homekit_dict = {}
|
2019-05-21 22:36:26 +00:00
|
|
|
|
|
|
|
for domain in sorted(integrations):
|
|
|
|
integration = integrations[domain]
|
|
|
|
|
|
|
|
if not integration.manifest:
|
|
|
|
continue
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
service_types = integration.manifest.get("zeroconf", [])
|
|
|
|
homekit = integration.manifest.get("homekit", {})
|
|
|
|
homekit_models = homekit.get("models", [])
|
2019-05-21 22:36:26 +00:00
|
|
|
|
2019-05-31 18:58:48 +00:00
|
|
|
if not service_types and not homekit_models:
|
2019-05-21 22:36:26 +00:00
|
|
|
continue
|
|
|
|
|
2019-05-27 02:48:27 +00:00
|
|
|
try:
|
|
|
|
with open(str(integration.path / "config_flow.py")) as fp:
|
2019-05-31 18:58:48 +00:00
|
|
|
content = fp.read()
|
2019-07-31 19:25:30 +00:00
|
|
|
uses_discovery_flow = "register_discovery_flow" in content
|
2019-05-31 18:58:48 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if (
|
|
|
|
service_types
|
|
|
|
and not uses_discovery_flow
|
|
|
|
and " async_step_zeroconf" not in content
|
|
|
|
):
|
2019-05-27 02:48:27 +00:00
|
|
|
integration.add_error(
|
2019-07-31 19:25:30 +00:00
|
|
|
"zeroconf", "Config flow has no async_step_zeroconf"
|
|
|
|
)
|
2019-05-27 02:48:27 +00:00
|
|
|
continue
|
2019-05-31 18:58:48 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if (
|
|
|
|
homekit_models
|
|
|
|
and not uses_discovery_flow
|
|
|
|
and " async_step_homekit" not in content
|
|
|
|
):
|
2019-05-31 18:58:48 +00:00
|
|
|
integration.add_error(
|
2019-07-31 19:25:30 +00:00
|
|
|
"zeroconf", "Config flow has no async_step_homekit"
|
|
|
|
)
|
2019-05-31 18:58:48 +00:00
|
|
|
continue
|
|
|
|
|
2019-05-27 02:48:27 +00:00
|
|
|
except FileNotFoundError:
|
|
|
|
integration.add_error(
|
2019-07-31 19:25:30 +00:00
|
|
|
"zeroconf",
|
|
|
|
"Zeroconf info in a manifest requires a config flow to exist",
|
2019-05-27 02:48:27 +00:00
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
2019-05-21 22:36:26 +00:00
|
|
|
for service_type in service_types:
|
2019-05-31 18:58:48 +00:00
|
|
|
service_type_dict[service_type].append(domain)
|
2019-05-21 22:36:26 +00:00
|
|
|
|
2019-05-31 18:58:48 +00:00
|
|
|
for model in homekit_models:
|
|
|
|
if model in homekit_dict:
|
|
|
|
integration.add_error(
|
2019-07-31 19:25:30 +00:00
|
|
|
"zeroconf",
|
|
|
|
"Integrations {} and {} have overlapping HomeKit "
|
|
|
|
"models".format(domain, homekit_dict[model]),
|
|
|
|
)
|
2019-05-31 18:58:48 +00:00
|
|
|
break
|
2019-05-21 22:36:26 +00:00
|
|
|
|
2019-05-31 18:58:48 +00:00
|
|
|
homekit_dict[model] = domain
|
|
|
|
|
|
|
|
# HomeKit models are matched on starting string, make sure none overlap.
|
|
|
|
warned = set()
|
|
|
|
for key in homekit_dict:
|
|
|
|
if key in warned:
|
|
|
|
continue
|
2019-05-23 21:41:57 +00:00
|
|
|
|
2019-05-31 18:58:48 +00:00
|
|
|
# n^2 yoooo
|
|
|
|
for key_2 in homekit_dict:
|
|
|
|
if key == key_2 or key_2 in warned:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if key.startswith(key_2) or key_2.startswith(key):
|
|
|
|
integration.add_error(
|
2019-07-31 19:25:30 +00:00
|
|
|
"zeroconf",
|
|
|
|
"Integrations {} and {} have overlapping HomeKit "
|
|
|
|
"models".format(homekit_dict[key], homekit_dict[key_2]),
|
|
|
|
)
|
2019-05-31 18:58:48 +00:00
|
|
|
warned.add(key)
|
|
|
|
warned.add(key_2)
|
|
|
|
break
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
zeroconf = OrderedDict(
|
|
|
|
(key, service_type_dict[key]) for key in sorted(service_type_dict)
|
2019-05-31 18:58:48 +00:00
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
homekit = OrderedDict((key, homekit_dict[key]) for key in sorted(homekit_dict))
|
|
|
|
|
|
|
|
return BASE.format(json.dumps(zeroconf, indent=4), json.dumps(homekit, indent=4))
|
2019-05-21 22:36:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def validate(integrations: Dict[str, Integration], config: Config):
|
|
|
|
"""Validate zeroconf file."""
|
2019-07-31 19:25:30 +00:00
|
|
|
zeroconf_path = config.root / "homeassistant/generated/zeroconf.py"
|
|
|
|
config.cache["zeroconf"] = content = generate_and_validate(integrations)
|
2019-05-21 22:36:26 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with open(str(zeroconf_path), "r") as fp:
|
2019-05-29 18:19:50 +00:00
|
|
|
current = fp.read().strip()
|
|
|
|
if current != content:
|
2019-05-21 22:36:26 +00:00
|
|
|
config.add_error(
|
|
|
|
"zeroconf",
|
2019-07-31 19:25:30 +00:00
|
|
|
"File zeroconf.py is not up to date. " "Run python3 -m script.hassfest",
|
|
|
|
fixable=True,
|
2019-05-21 22:36:26 +00:00
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
def generate(integrations: Dict[str, Integration], config: Config):
|
|
|
|
"""Generate zeroconf file."""
|
2019-07-31 19:25:30 +00:00
|
|
|
zeroconf_path = config.root / "homeassistant/generated/zeroconf.py"
|
|
|
|
with open(str(zeroconf_path), "w") as fp:
|
|
|
|
fp.write(config.cache["zeroconf"] + "\n")
|