1
0
mirror of https://github.com/home-assistant/core.git synced 2025-08-18 09:50:04 +00:00
Files
.devcontainer
.github
.vscode
docs
homeassistant
machine
pylint
rootfs
script
hassfest
__init__.py
__main__.py
application_credentials.py
bluetooth.py
brand.py
codeowners.py
config_flow.py
coverage.py
dependencies.py
dhcp.py
json.py
manifest.py
metadata.py
model.py
mqtt.py
mypy_config.py
requirements.py
serializer.py
services.py
ssdp.py
translations.py
usb.py
zeroconf.py
scaffold
translations
__init__.py
bootstrap
check_dirty
check_format
countries.py
currencies.py
gen_requirements_all.py
inspect_schemas.py
languages.py
lint
lint_and_test.py
monkeytype
pip_check
run-in-env.sh
server
setup
update
version_bump.py
tests
.core_files.yaml
.coveragerc
.dockerignore
.gitattributes
.gitignore
.hadolint.yaml
.pre-commit-config.yaml
.prettierignore
.readthedocs.yml
.strict-typing
.yamllint
CLA.md
CODEOWNERS
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile
Dockerfile.dev
LICENSE.md
MANIFEST.in
README.rst
build.yaml
codecov.yml
mypy.ini
pyproject.toml
requirements.txt
requirements_all.txt
requirements_docs.txt
requirements_test.txt
requirements_test_all.txt
requirements_test_pre_commit.txt
setup.cfg
core/script/hassfest/zeroconf.py

100 lines
3.2 KiB
Python

"""Generate zeroconf file."""
from __future__ import annotations
from collections import defaultdict
from homeassistant.loader import async_process_zeroconf_match_dict
from .model import Config, Integration
from .serializer import format_python_namespace
def generate_and_validate(integrations: dict[str, Integration]) -> str:
"""Validate and generate zeroconf data."""
service_type_dict = defaultdict(list)
homekit_dict: dict[str, str] = {}
for domain in sorted(integrations):
integration = integrations[domain]
service_types = integration.manifest.get("zeroconf", [])
homekit = integration.manifest.get("homekit", {})
homekit_models = homekit.get("models", [])
if not (service_types or homekit_models):
continue
for entry in service_types:
data = {"domain": domain}
if isinstance(entry, dict):
typ = entry["type"]
data.update(async_process_zeroconf_match_dict(entry))
else:
typ = entry
service_type_dict[typ].append(data)
for model in homekit_models:
if model in homekit_dict:
integration.add_error(
"zeroconf",
f"Integrations {domain} and {homekit_dict[model]} "
"have overlapping HomeKit models",
)
break
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
# 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(
"zeroconf",
f"Integrations {homekit_dict[key]} and {homekit_dict[key_2]} "
"have overlapping HomeKit models",
)
warned.add(key)
warned.add(key_2)
break
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},
}
)
def validate(integrations: dict[str, Integration], config: Config) -> None:
"""Validate zeroconf file."""
zeroconf_path = config.root / "homeassistant/generated/zeroconf.py"
config.cache["zeroconf"] = content = generate_and_validate(integrations)
if config.specific_integrations:
return
with open(str(zeroconf_path)) as fp:
current = fp.read()
if current != content:
config.add_error(
"zeroconf",
"File zeroconf.py is not up to date. Run python3 -m script.hassfest",
fixable=True,
)
return
def generate(integrations: dict[str, Integration], config: Config) -> None:
"""Generate zeroconf file."""
zeroconf_path = config.root / "homeassistant/generated/zeroconf.py"
with open(str(zeroconf_path), "w") as fp:
fp.write(f"{config.cache['zeroconf']}")