From 5d32659d17f4a42f995a67ab647dfe1838f3ddca Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Mon, 16 May 2022 08:11:09 -0700 Subject: [PATCH] Update scaffold script to use application_credentials platform (#71881) --- script/scaffold/generate.py | 2 +- .../integration/__init__.py | 49 ++----------------- .../integration/application_credentials.py | 16 ++++++ .../tests/test_config_flow.py | 31 ++++++++---- 4 files changed, 42 insertions(+), 56 deletions(-) create mode 100644 script/scaffold/templates/config_flow_oauth2/integration/application_credentials.py diff --git a/script/scaffold/generate.py b/script/scaffold/generate.py index a86c4e3a015..7f418868463 100644 --- a/script/scaffold/generate.py +++ b/script/scaffold/generate.py @@ -173,7 +173,7 @@ def _custom_tasks(template, info: Info) -> None: ) elif template == "config_flow_oauth2": - info.update_manifest(config_flow=True, dependencies=["auth"]) + info.update_manifest(config_flow=True, dependencies=["application_credentials"]) info.update_strings( config={ "step": { diff --git a/script/scaffold/templates/config_flow_oauth2/integration/__init__.py b/script/scaffold/templates/config_flow_oauth2/integration/__init__.py index b580e609bba..24dcee48ccd 100644 --- a/script/scaffold/templates/config_flow_oauth2/integration/__init__.py +++ b/script/scaffold/templates/config_flow_oauth2/integration/__init__.py @@ -1,60 +1,19 @@ """The NEW_NAME integration.""" from __future__ import annotations -import voluptuous as vol - from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CONF_CLIENT_ID, CONF_CLIENT_SECRET, Platform +from homeassistant.const import Platform from homeassistant.core import HomeAssistant -from homeassistant.helpers import ( - aiohttp_client, - config_entry_oauth2_flow, - config_validation as cv, -) -from homeassistant.helpers.typing import ConfigType +from homeassistant.helpers import aiohttp_client, config_entry_oauth2_flow -from . import api, config_flow -from .const import DOMAIN, OAUTH2_AUTHORIZE, OAUTH2_TOKEN - -CONFIG_SCHEMA = vol.Schema( - { - DOMAIN: vol.Schema( - { - vol.Required(CONF_CLIENT_ID): cv.string, - vol.Required(CONF_CLIENT_SECRET): cv.string, - } - ) - }, - extra=vol.ALLOW_EXTRA, -) +from . import api +from .const import DOMAIN # TODO List the platforms that you want to support. # For your initial PR, limit it to 1 platform. PLATFORMS: list[Platform] = [Platform.LIGHT] -async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: - """Set up the NEW_NAME component.""" - hass.data[DOMAIN] = {} - - if DOMAIN not in config: - return True - - config_flow.OAuth2FlowHandler.async_register_implementation( - hass, - config_entry_oauth2_flow.LocalOAuth2Implementation( - hass, - DOMAIN, - config[DOMAIN][CONF_CLIENT_ID], - config[DOMAIN][CONF_CLIENT_SECRET], - OAUTH2_AUTHORIZE, - OAUTH2_TOKEN, - ), - ) - - return True - - async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up NEW_NAME from a config entry.""" implementation = ( diff --git a/script/scaffold/templates/config_flow_oauth2/integration/application_credentials.py b/script/scaffold/templates/config_flow_oauth2/integration/application_credentials.py new file mode 100644 index 00000000000..51ef70b1885 --- /dev/null +++ b/script/scaffold/templates/config_flow_oauth2/integration/application_credentials.py @@ -0,0 +1,16 @@ +"""application_credentials platform the NEW_NAME integration.""" + +from homeassistant.components.application_credentials import AuthorizationServer +from homeassistant.core import HomeAssistant + +# TODO Update with your own urls +OAUTH2_AUTHORIZE = "https://www.example.com/auth/authorize" +OAUTH2_TOKEN = "https://www.example.com/auth/token" + + +async def async_get_authorization_server(hass: HomeAssistant) -> AuthorizationServer: + """Return authorization server.""" + return AuthorizationServer( + authorize_url=OAUTH2_AUTHORIZE, + token_url=OAUTH2_TOKEN, + ) diff --git a/script/scaffold/templates/config_flow_oauth2/tests/test_config_flow.py b/script/scaffold/templates/config_flow_oauth2/tests/test_config_flow.py index 3ed8d9d293f..bc087119c6e 100644 --- a/script/scaffold/templates/config_flow_oauth2/tests/test_config_flow.py +++ b/script/scaffold/templates/config_flow_oauth2/tests/test_config_flow.py @@ -1,35 +1,46 @@ """Test the NEW_NAME config flow.""" + from unittest.mock import patch -from homeassistant import config_entries, setup +import pytest + +from homeassistant import config_entries from homeassistant.components.NEW_DOMAIN.const import ( DOMAIN, OAUTH2_AUTHORIZE, OAUTH2_TOKEN, ) +from homeassistant.components.application_credentials import ( + ClientCredential, + async_import_client_credential, +) from homeassistant.core import HomeAssistant from homeassistant.helpers import config_entry_oauth2_flow +from homeassistant.setup import async_setup_component CLIENT_ID = "1234" CLIENT_SECRET = "5678" +@pytest.fixture +async def setup_credentials(hass: HomeAssistant) -> None: + """Fixture to setup credentials.""" + assert await async_setup_component(hass, "application_credentials", {}) + await async_import_client_credential( + hass, + DOMAIN, + ClientCredential(CLIENT_ID, CLIENT_SECRET), + ) + + async def test_full_flow( hass: HomeAssistant, hass_client_no_auth, aioclient_mock, current_request_with_host, + setup_credentials, ) -> None: """Check full flow.""" - assert await setup.async_setup_component( - hass, - "NEW_DOMAIN", - { - "NEW_DOMAIN": {"client_id": CLIENT_ID, "client_secret": CLIENT_SECRET}, - "http": {"base_url": "https://example.com"}, - }, - ) - result = await hass.config_entries.flow.async_init( "NEW_DOMAIN", context={"source": config_entries.SOURCE_USER} )