2019-05-21 22:36:26 +00:00
|
|
|
"""Generate zeroconf file."""
|
2024-03-08 15:36:11 +00:00
|
|
|
|
2021-03-18 21:58:19 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-09-19 21:37:22 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
|
2023-02-16 02:44:11 +00:00
|
|
|
from homeassistant.loader import (
|
|
|
|
async_process_zeroconf_match_dict,
|
|
|
|
homekit_always_discover,
|
|
|
|
)
|
2021-12-19 08:09:21 +00:00
|
|
|
|
2019-12-09 15:24:03 +00:00
|
|
|
from .model import Config, Integration
|
2022-11-09 15:58:20 +00:00
|
|
|
from .serializer import format_python_namespace
|
2019-05-21 22:36:26 +00:00
|
|
|
|
|
|
|
|
2022-11-23 18:05:31 +00:00
|
|
|
def generate_and_validate(integrations: dict[str, Integration]) -> str:
|
2019-05-21 22:36:26 +00:00
|
|
|
"""Validate and generate zeroconf data."""
|
2019-05-31 18:58:48 +00:00
|
|
|
service_type_dict = defaultdict(list)
|
2023-02-16 02:44:11 +00:00
|
|
|
homekit_dict: dict[str, dict[str, str]] = {}
|
2019-05-21 22:36:26 +00:00
|
|
|
|
|
|
|
for domain in sorted(integrations):
|
|
|
|
integration = integrations[domain]
|
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
|
|
|
|
2020-06-15 11:38:38 +00:00
|
|
|
if not (service_types or homekit_models):
|
2019-05-27 02:48:27 +00:00
|
|
|
continue
|
|
|
|
|
2020-09-11 10:19:21 +00:00
|
|
|
for entry in service_types:
|
|
|
|
data = {"domain": domain}
|
|
|
|
if isinstance(entry, dict):
|
|
|
|
typ = entry["type"]
|
2021-12-19 08:09:21 +00:00
|
|
|
data.update(async_process_zeroconf_match_dict(entry))
|
2020-09-11 10:19:21 +00:00
|
|
|
else:
|
|
|
|
typ = entry
|
|
|
|
|
|
|
|
service_type_dict[typ].append(data)
|
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",
|
2020-04-05 15:48:55 +00:00
|
|
|
f"Integrations {domain} and {homekit_dict[model]} "
|
|
|
|
"have overlapping HomeKit models",
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-05-31 18:58:48 +00:00
|
|
|
break
|
2019-05-21 22:36:26 +00:00
|
|
|
|
2023-02-16 02:44:11 +00:00
|
|
|
homekit_dict[model] = {
|
|
|
|
"domain": domain,
|
|
|
|
"always_discover": homekit_always_discover(
|
|
|
|
integration.manifest["iot_class"]
|
|
|
|
),
|
|
|
|
}
|
2019-05-31 18:58:48 +00:00
|
|
|
|
|
|
|
# HomeKit models are matched on starting string, make sure none overlap.
|
|
|
|
warned = set()
|
2024-11-22 15:53:26 +00:00
|
|
|
for key, value in homekit_dict.items():
|
2019-05-31 18:58:48 +00:00
|
|
|
if key in warned:
|
|
|
|
continue
|
2019-05-23 21:41:57 +00:00
|
|
|
|
2019-05-31 18:58:48 +00:00
|
|
|
# n^2 yoooo
|
2024-11-22 15:53:26 +00:00
|
|
|
for key_2, value_2 in homekit_dict.items():
|
2019-05-31 18:58:48 +00:00
|
|
|
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",
|
2024-11-22 15:53:26 +00:00
|
|
|
f"Integrations {value} and {value_2} "
|
2020-04-05 15:48:55 +00:00
|
|
|
"have overlapping HomeKit models",
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-05-31 18:58:48 +00:00
|
|
|
warned.add(key)
|
|
|
|
warned.add(key_2)
|
|
|
|
break
|
|
|
|
|
2022-11-09 15:58:20 +00:00
|
|
|
return format_python_namespace(
|
|
|
|
{
|
|
|
|
"HOMEKIT": {key: homekit_dict[key] for key in homekit_dict},
|
|
|
|
"ZEROCONF": {key: service_type_dict[key] for key in service_type_dict},
|
|
|
|
}
|
2022-09-19 21:37:22 +00:00
|
|
|
)
|
2019-05-21 22:36:26 +00:00
|
|
|
|
|
|
|
|
2022-11-23 18:05:31 +00:00
|
|
|
def validate(integrations: dict[str, Integration], config: Config) -> None:
|
2019-05-21 22:36:26 +00:00
|
|
|
"""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
|
|
|
|
2020-04-16 16:00:04 +00:00
|
|
|
if config.specific_integrations:
|
|
|
|
return
|
|
|
|
|
2024-09-06 09:33:01 +00:00
|
|
|
if zeroconf_path.read_text() != content:
|
|
|
|
config.add_error(
|
|
|
|
"zeroconf",
|
|
|
|
"File zeroconf.py is not up to date. Run python3 -m script.hassfest",
|
|
|
|
fixable=True,
|
|
|
|
)
|
2019-05-21 22:36:26 +00:00
|
|
|
|
|
|
|
|
2022-11-23 18:05:31 +00:00
|
|
|
def generate(integrations: dict[str, Integration], config: Config) -> None:
|
2019-05-21 22:36:26 +00:00
|
|
|
"""Generate zeroconf file."""
|
2019-07-31 19:25:30 +00:00
|
|
|
zeroconf_path = config.root / "homeassistant/generated/zeroconf.py"
|
2024-09-06 09:33:01 +00:00
|
|
|
zeroconf_path.write_text(f"{config.cache['zeroconf']}")
|