Add OAuth2 config flow scaffold (#28220)
* Add OAuth2 scaffold * Generate integration if non-existing domain specified * Update URL
This commit is contained in:
parent
e700384cce
commit
24c29f9227
13 changed files with 567 additions and 209 deletions
|
@ -1,7 +1,6 @@
|
|||
"""Generate an integration."""
|
||||
from pathlib import Path
|
||||
|
||||
from .error import ExitApp
|
||||
from .model import Info
|
||||
|
||||
TEMPLATE_DIR = Path(__file__).parent / "templates"
|
||||
|
@ -11,8 +10,6 @@ TEMPLATE_TESTS = TEMPLATE_DIR / "tests"
|
|||
|
||||
def generate(template: str, info: Info) -> None:
|
||||
"""Generate a template."""
|
||||
_validate(template, info)
|
||||
|
||||
print(f"Scaffolding {template} for the {info.domain} integration...")
|
||||
_ensure_tests_dir_exists(info)
|
||||
_generate(TEMPLATE_DIR / template / "integration", info.integration_dir, info)
|
||||
|
@ -21,13 +18,6 @@ def generate(template: str, info: Info) -> None:
|
|||
print()
|
||||
|
||||
|
||||
def _validate(template, info):
|
||||
"""Validate we can run this task."""
|
||||
if template == "config_flow":
|
||||
if (info.integration_dir / "config_flow.py").exists():
|
||||
raise ExitApp(f"Integration {info.domain} already has a config flow.")
|
||||
|
||||
|
||||
def _generate(src_dir, target_dir, info: Info) -> None:
|
||||
"""Generate an integration."""
|
||||
replaces = {"NEW_DOMAIN": info.domain, "NEW_NAME": info.name}
|
||||
|
@ -42,6 +32,20 @@ def _generate(src_dir, target_dir, info: Info) -> None:
|
|||
content = content.replace(to_search, to_replace)
|
||||
|
||||
target_file = target_dir / source_file.relative_to(src_dir)
|
||||
|
||||
# If the target file exists, create our template as EXAMPLE_<filename>.
|
||||
# Exception: If we are creating a new integration, we can end up running integration base
|
||||
# and a config flows on top of one another. In that case, we want to override the files.
|
||||
if not info.is_new and target_file.exists():
|
||||
new_name = f"EXAMPLE_{target_file.name}"
|
||||
print(f"File {target_file} already exists, creating {new_name} instead.")
|
||||
target_file = target_file.parent / new_name
|
||||
info.examples_added.add(target_file)
|
||||
elif src_dir.name == "integration":
|
||||
info.files_added.add(target_file)
|
||||
else:
|
||||
info.tests_added.add(target_file)
|
||||
|
||||
print(f"Writing {target_file}")
|
||||
target_file.write_text(content)
|
||||
|
||||
|
@ -58,6 +62,11 @@ def _ensure_tests_dir_exists(info: Info) -> None:
|
|||
)
|
||||
|
||||
|
||||
def _append(path: Path, text):
|
||||
"""Append some text to a path."""
|
||||
path.write_text(path.read_text() + text)
|
||||
|
||||
|
||||
def _custom_tasks(template, info) -> None:
|
||||
"""Handle custom tasks for templates."""
|
||||
if template == "integration":
|
||||
|
@ -68,7 +77,7 @@ def _custom_tasks(template, info) -> None:
|
|||
|
||||
info.update_manifest(**changes)
|
||||
|
||||
if template == "device_trigger":
|
||||
elif template == "device_trigger":
|
||||
info.update_strings(
|
||||
device_automation={
|
||||
**info.strings().get("device_automation", {}),
|
||||
|
@ -79,7 +88,7 @@ def _custom_tasks(template, info) -> None:
|
|||
}
|
||||
)
|
||||
|
||||
if template == "device_condition":
|
||||
elif template == "device_condition":
|
||||
info.update_strings(
|
||||
device_automation={
|
||||
**info.strings().get("device_automation", {}),
|
||||
|
@ -90,7 +99,7 @@ def _custom_tasks(template, info) -> None:
|
|||
}
|
||||
)
|
||||
|
||||
if template == "device_action":
|
||||
elif template == "device_action":
|
||||
info.update_strings(
|
||||
device_automation={
|
||||
**info.strings().get("device_automation", {}),
|
||||
|
@ -101,7 +110,7 @@ def _custom_tasks(template, info) -> None:
|
|||
}
|
||||
)
|
||||
|
||||
if template == "config_flow":
|
||||
elif template == "config_flow":
|
||||
info.update_manifest(config_flow=True)
|
||||
info.update_strings(
|
||||
config={
|
||||
|
@ -118,7 +127,7 @@ def _custom_tasks(template, info) -> None:
|
|||
}
|
||||
)
|
||||
|
||||
if template == "config_flow_discovery":
|
||||
elif template == "config_flow_discovery":
|
||||
info.update_manifest(config_flow=True)
|
||||
info.update_strings(
|
||||
config={
|
||||
|
@ -136,19 +145,28 @@ def _custom_tasks(template, info) -> None:
|
|||
}
|
||||
)
|
||||
|
||||
if template in ("config_flow", "config_flow_discovery"):
|
||||
init_file = info.integration_dir / "__init__.py"
|
||||
init_file.write_text(
|
||||
init_file.read_text()
|
||||
+ """
|
||||
|
||||
async def async_setup_entry(hass, entry):
|
||||
\"\"\"Set up a config entry for NEW_NAME.\"\"\"
|
||||
# TODO forward the entry for each platform that you want to set up.
|
||||
# hass.async_create_task(
|
||||
# hass.config_entries.async_forward_entry_setup(entry, "media_player")
|
||||
# )
|
||||
|
||||
return True
|
||||
"""
|
||||
elif template == "config_flow_oauth2":
|
||||
info.update_manifest(config_flow=True)
|
||||
info.update_strings(
|
||||
config={
|
||||
"title": info.name,
|
||||
"step": {
|
||||
"pick_implementation": {"title": "Pick Authentication Method"}
|
||||
},
|
||||
"abort": {
|
||||
"missing_configuration": "The Somfy component is not configured. Please follow the documentation."
|
||||
},
|
||||
"create_entry": {
|
||||
"default": f"Successfully authenticated with {info.name}."
|
||||
},
|
||||
}
|
||||
)
|
||||
_append(
|
||||
info.integration_dir / "const.py",
|
||||
"""
|
||||
|
||||
# TODO Update with your own urls
|
||||
OAUTH2_AUTHORIZE = "https://www.example.com/auth/authorize"
|
||||
OAUTH2_TOKEN = "https://www.example.com/auth/token"
|
||||
""",
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue