Update scaffold script to use application_credentials platform (#71881)

This commit is contained in:
Allen Porter 2022-05-16 08:11:09 -07:00 committed by GitHub
parent 514e7708b3
commit 5d32659d17
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 56 deletions

View file

@ -173,7 +173,7 @@ def _custom_tasks(template, info: Info) -> None:
) )
elif template == "config_flow_oauth2": 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( info.update_strings(
config={ config={
"step": { "step": {

View file

@ -1,60 +1,19 @@
"""The NEW_NAME integration.""" """The NEW_NAME integration."""
from __future__ import annotations from __future__ import annotations
import voluptuous as vol
from homeassistant.config_entries import ConfigEntry 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.core import HomeAssistant
from homeassistant.helpers import ( from homeassistant.helpers import aiohttp_client, config_entry_oauth2_flow
aiohttp_client,
config_entry_oauth2_flow,
config_validation as cv,
)
from homeassistant.helpers.typing import ConfigType
from . import api, config_flow from . import api
from .const import DOMAIN, OAUTH2_AUTHORIZE, OAUTH2_TOKEN from .const import DOMAIN
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,
)
# TODO List the platforms that you want to support. # TODO List the platforms that you want to support.
# For your initial PR, limit it to 1 platform. # For your initial PR, limit it to 1 platform.
PLATFORMS: list[Platform] = [Platform.LIGHT] 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: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up NEW_NAME from a config entry.""" """Set up NEW_NAME from a config entry."""
implementation = ( implementation = (

View file

@ -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,
)

View file

@ -1,35 +1,46 @@
"""Test the NEW_NAME config flow.""" """Test the NEW_NAME config flow."""
from unittest.mock import patch 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 ( from homeassistant.components.NEW_DOMAIN.const import (
DOMAIN, DOMAIN,
OAUTH2_AUTHORIZE, OAUTH2_AUTHORIZE,
OAUTH2_TOKEN, OAUTH2_TOKEN,
) )
from homeassistant.components.application_credentials import (
ClientCredential,
async_import_client_credential,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_entry_oauth2_flow from homeassistant.helpers import config_entry_oauth2_flow
from homeassistant.setup import async_setup_component
CLIENT_ID = "1234" CLIENT_ID = "1234"
CLIENT_SECRET = "5678" 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( async def test_full_flow(
hass: HomeAssistant, hass: HomeAssistant,
hass_client_no_auth, hass_client_no_auth,
aioclient_mock, aioclient_mock,
current_request_with_host, current_request_with_host,
setup_credentials,
) -> None: ) -> None:
"""Check full flow.""" """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( result = await hass.config_entries.flow.async_init(
"NEW_DOMAIN", context={"source": config_entries.SOURCE_USER} "NEW_DOMAIN", context={"source": config_entries.SOURCE_USER}
) )