mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Automatically generate config flow list (#23802)
* Add config flow to manifest.json * Still load config flows via config flow platform * Fix typo * Lint * Update config_flows.py" * Catch import error when setting up entry * Lint * Fix tests * Fix imports * Lint * Fix Unifi tests * Fix translation test * Add homekit_controller config flow
This commit is contained in:
@@ -3,13 +3,14 @@ import pathlib
|
||||
import sys
|
||||
|
||||
from .model import Integration, Config
|
||||
from . import dependencies, manifest, codeowners, services
|
||||
from . import dependencies, manifest, codeowners, services, config_flow
|
||||
|
||||
PLUGINS = [
|
||||
manifest,
|
||||
dependencies,
|
||||
codeowners,
|
||||
services,
|
||||
config_flow,
|
||||
]
|
||||
|
||||
|
||||
|
||||
85
script/hassfest/config_flow.py
Normal file
85
script/hassfest/config_flow.py
Normal file
@@ -0,0 +1,85 @@
|
||||
"""Generate config flow file."""
|
||||
import json
|
||||
from typing import Dict
|
||||
|
||||
from .model import Integration, Config
|
||||
|
||||
BASE = """
|
||||
\"\"\"Automatically generated by hassfest.
|
||||
|
||||
To update, run python3 -m hassfest
|
||||
\"\"\"
|
||||
|
||||
|
||||
FLOWS = {}
|
||||
""".strip()
|
||||
|
||||
|
||||
def validate_integration(integration: Integration):
|
||||
"""Validate we can load config flow without installing requirements."""
|
||||
if not (integration.path / "config_flow.py").is_file():
|
||||
integration.add_error(
|
||||
'config_flow',
|
||||
"Config flows need to be defined in the file config_flow.py")
|
||||
|
||||
# Currently not require being able to load config flow without
|
||||
# installing requirements.
|
||||
# try:
|
||||
# integration.import_pkg('config_flow')
|
||||
# except ImportError as err:
|
||||
# integration.add_error(
|
||||
# 'config_flow',
|
||||
# "Unable to import config flow: {}. Config flows should be able "
|
||||
# "to be imported without installing requirements.".format(err))
|
||||
# return
|
||||
|
||||
# if integration.domain not in config_entries.HANDLERS:
|
||||
# integration.add_error(
|
||||
# 'config_flow',
|
||||
# "Importing the config flow platform did not register a config "
|
||||
# "flow handler.")
|
||||
|
||||
|
||||
def generate_and_validate(integrations: Dict[str, Integration]):
|
||||
"""Validate and generate config flow data."""
|
||||
domains = []
|
||||
|
||||
for domain in sorted(integrations):
|
||||
integration = integrations[domain]
|
||||
|
||||
if not integration.manifest:
|
||||
continue
|
||||
|
||||
config_flow = integration.manifest.get('config_flow')
|
||||
|
||||
if not config_flow:
|
||||
continue
|
||||
|
||||
validate_integration(integration)
|
||||
|
||||
domains.append(domain)
|
||||
|
||||
return BASE.format(json.dumps(domains, indent=4))
|
||||
|
||||
|
||||
def validate(integrations: Dict[str, Integration], config: Config):
|
||||
"""Validate config flow file."""
|
||||
config_flow_path = config.root / 'homeassistant/generated/config_flows.py'
|
||||
config.cache['config_flow'] = content = generate_and_validate(integrations)
|
||||
|
||||
with open(str(config_flow_path), 'r') as fp:
|
||||
if fp.read().strip() != content:
|
||||
config.add_error(
|
||||
"config_flow",
|
||||
"File config_flows.py is not up to date. "
|
||||
"Run python3 -m script.hassfest",
|
||||
fixable=True
|
||||
)
|
||||
return
|
||||
|
||||
|
||||
def generate(integrations: Dict[str, Integration], config: Config):
|
||||
"""Generate config flow file."""
|
||||
config_flow_path = config.root / 'homeassistant/generated/config_flows.py'
|
||||
with open(str(config_flow_path), 'w') as fp:
|
||||
fp.write(config.cache['config_flow'] + '\n')
|
||||
@@ -10,6 +10,7 @@ from .model import Integration
|
||||
MANIFEST_SCHEMA = vol.Schema({
|
||||
vol.Required('domain'): str,
|
||||
vol.Required('name'): str,
|
||||
vol.Optional('config_flow'): bool,
|
||||
vol.Required('documentation'): str,
|
||||
vol.Required('requirements'): [str],
|
||||
vol.Required('dependencies'): [str],
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import json
|
||||
from typing import List, Dict, Any
|
||||
import pathlib
|
||||
import importlib
|
||||
|
||||
import attr
|
||||
|
||||
@@ -92,3 +93,10 @@ class Integration:
|
||||
return
|
||||
|
||||
self.manifest = manifest
|
||||
|
||||
def import_pkg(self, platform=None):
|
||||
"""Import the Python file."""
|
||||
pkg = "homeassistant.components.{}".format(self.domain)
|
||||
if platform is not None:
|
||||
pkg += ".{}".format(platform)
|
||||
return importlib.import_module(pkg)
|
||||
|
||||
Reference in New Issue
Block a user