mirror of
https://github.com/home-assistant/core.git
synced 2025-12-27 14:31:13 +00:00
Zeroconf discovery for config entries (#23919)
* Proof of concept * Follow comments * Fix line length and bad imports * Move imports to top * Exception handling for unicode decoding Create debug print for new service types Add empty test files * First try at a test * Add type and name to service info Fix static check * Add aiozeroconf to test dependencies
This commit is contained in:
committed by
Paulus Schoutsen
parent
e047e4dcff
commit
636077c74d
@@ -50,6 +50,7 @@ TEST_REQUIREMENTS = (
|
||||
'aiohue',
|
||||
'aiounifi',
|
||||
'aioswitcher',
|
||||
'aiozeroconf',
|
||||
'apns2',
|
||||
'av',
|
||||
'axis',
|
||||
|
||||
@@ -3,7 +3,8 @@ import pathlib
|
||||
import sys
|
||||
|
||||
from .model import Integration, Config
|
||||
from . import dependencies, manifest, codeowners, services, config_flow
|
||||
from . import (
|
||||
dependencies, manifest, codeowners, services, config_flow, zeroconf)
|
||||
|
||||
PLUGINS = [
|
||||
manifest,
|
||||
@@ -11,6 +12,7 @@ PLUGINS = [
|
||||
codeowners,
|
||||
services,
|
||||
config_flow,
|
||||
zeroconf
|
||||
]
|
||||
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ MANIFEST_SCHEMA = vol.Schema({
|
||||
vol.Required('domain'): str,
|
||||
vol.Required('name'): str,
|
||||
vol.Optional('config_flow'): bool,
|
||||
vol.Optional('zeroconf'): [str],
|
||||
vol.Required('documentation'): str,
|
||||
vol.Required('requirements'): [str],
|
||||
vol.Required('dependencies'): [str],
|
||||
|
||||
63
script/hassfest/zeroconf.py
Normal file
63
script/hassfest/zeroconf.py
Normal file
@@ -0,0 +1,63 @@
|
||||
"""Generate zeroconf file."""
|
||||
import json
|
||||
from typing import Dict
|
||||
|
||||
from .model import Integration, Config
|
||||
|
||||
BASE = """
|
||||
\"\"\"Automatically generated by hassfest.
|
||||
|
||||
To update, run python3 -m hassfest
|
||||
\"\"\"
|
||||
|
||||
|
||||
SERVICE_TYPES = {}
|
||||
""".strip()
|
||||
|
||||
|
||||
def generate_and_validate(integrations: Dict[str, Integration]):
|
||||
"""Validate and generate zeroconf data."""
|
||||
service_type_dict = {}
|
||||
|
||||
for domain in sorted(integrations):
|
||||
integration = integrations[domain]
|
||||
|
||||
if not integration.manifest:
|
||||
continue
|
||||
|
||||
service_types = integration.manifest.get('zeroconf')
|
||||
|
||||
if not service_types:
|
||||
continue
|
||||
|
||||
for service_type in service_types:
|
||||
|
||||
if service_type not in service_type_dict:
|
||||
service_type_dict[service_type] = []
|
||||
|
||||
service_type_dict[service_type].append(domain)
|
||||
|
||||
return BASE.format(json.dumps(service_type_dict, indent=4))
|
||||
|
||||
|
||||
def validate(integrations: Dict[str, Integration], config: Config):
|
||||
"""Validate zeroconf file."""
|
||||
zeroconf_path = config.root / 'homeassistant/generated/zeroconf.py'
|
||||
config.cache['zeroconf'] = content = generate_and_validate(integrations)
|
||||
|
||||
with open(str(zeroconf_path), 'r') as fp:
|
||||
if fp.read().strip() != 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):
|
||||
"""Generate zeroconf file."""
|
||||
zeroconf_path = config.root / 'homeassistant/generated/zeroconf.py'
|
||||
with open(str(zeroconf_path), 'w') as fp:
|
||||
fp.write(config.cache['zeroconf'] + '\n')
|
||||
Reference in New Issue
Block a user