Add support for discovery via DHCP (#45087)

* Add support for discovery via DHCP

* additional tesla ouis

* merge tests

* dhcp test

* merge requirements test

* dhcp test

* dhcp discovery

* dhcp discovery

* pylint

* pylint

* pylint

* fix

* Add matching tests

* 100% cover

* cleanup

* fix codespell

* Update exception handling

* remove unneeded comment

* fix options handling exception

* fix options handling exception
This commit is contained in:
J. Nick Koston 2021-01-13 22:09:08 -10:00 committed by GitHub
parent 402a0ea7da
commit da677f7d5a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
38 changed files with 843 additions and 17 deletions

63
script/hassfest/dhcp.py Normal file
View file

@ -0,0 +1,63 @@
"""Generate dhcp file."""
import json
from typing import Dict, List
from .model import Config, Integration
BASE = """
\"\"\"Automatically generated by hassfest.
To update, run python3 -m script.hassfest
\"\"\"
# fmt: off
DHCP = {}
""".strip()
def generate_and_validate(integrations: List[Dict[str, str]]):
"""Validate and generate dhcp data."""
match_list = []
for domain in sorted(integrations):
integration = integrations[domain]
if not integration.manifest:
continue
match_types = integration.manifest.get("dhcp", [])
if not match_types:
continue
for entry in match_types:
match_list.append({"domain": domain, **entry})
return BASE.format(json.dumps(match_list, indent=4))
def validate(integrations: Dict[str, Integration], config: Config):
"""Validate dhcp file."""
dhcp_path = config.root / "homeassistant/generated/dhcp.py"
config.cache["dhcp"] = content = generate_and_validate(integrations)
if config.specific_integrations:
return
with open(str(dhcp_path)) as fp:
current = fp.read().strip()
if current != content:
config.add_error(
"dhcp",
"File dhcp.py is not up to date. Run python3 -m script.hassfest",
fixable=True,
)
return
def generate(integrations: Dict[str, Integration], config: Config):
"""Generate dhcp file."""
dhcp_path = config.root / "homeassistant/generated/dhcp.py"
with open(str(dhcp_path), "w") as fp:
fp.write(f"{config.cache['dhcp']}\n")