From dc76cce96b8af4e8e02b3c9169e23d7da7bb4821 Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Mon, 23 May 2022 05:44:45 -0700 Subject: [PATCH] Add SENZ application credentials platform (#72338) --- homeassistant/components/senz/__init__.py | 42 ++++++++++++------- .../senz/application_credentials.py | 14 +++++++ homeassistant/components/senz/manifest.json | 2 +- .../generated/application_credentials.py | 1 + tests/components/senz/test_config_flow.py | 1 - 5 files changed, 42 insertions(+), 18 deletions(-) create mode 100644 homeassistant/components/senz/application_credentials.py diff --git a/homeassistant/components/senz/__init__.py b/homeassistant/components/senz/__init__.py index b6fc24228884..08aa26fa3c57 100644 --- a/homeassistant/components/senz/__init__.py +++ b/homeassistant/components/senz/__init__.py @@ -4,10 +4,14 @@ from __future__ import annotations from datetime import timedelta import logging -from aiosenz import AUTHORIZATION_ENDPOINT, SENZAPI, TOKEN_ENDPOINT, Thermostat +from aiosenz import SENZAPI, Thermostat from httpx import RequestError import voluptuous as vol +from homeassistant.components.application_credentials import ( + ClientCredential, + async_import_client_credential, +) from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_CLIENT_ID, CONF_CLIENT_SECRET, Platform from homeassistant.core import HomeAssistant @@ -20,7 +24,6 @@ from homeassistant.helpers import ( from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed -from . import config_flow from .api import SENZConfigEntryAuth from .const import DOMAIN @@ -29,14 +32,17 @@ UPDATE_INTERVAL = timedelta(seconds=30) _LOGGER = logging.getLogger(__name__) CONFIG_SCHEMA = vol.Schema( - { - DOMAIN: vol.Schema( - { - vol.Required(CONF_CLIENT_ID): cv.string, - vol.Required(CONF_CLIENT_SECRET): cv.string, - } - ) - }, + vol.All( + cv.deprecated(DOMAIN), + { + DOMAIN: vol.Schema( + { + vol.Required(CONF_CLIENT_ID): cv.string, + vol.Required(CONF_CLIENT_SECRET): cv.string, + } + ) + }, + ), extra=vol.ALLOW_EXTRA, ) @@ -52,17 +58,21 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: if DOMAIN not in config: return True - config_flow.OAuth2FlowHandler.async_register_implementation( + await async_import_client_credential( hass, - config_entry_oauth2_flow.LocalOAuth2Implementation( - hass, - DOMAIN, + DOMAIN, + ClientCredential( config[DOMAIN][CONF_CLIENT_ID], config[DOMAIN][CONF_CLIENT_SECRET], - AUTHORIZATION_ENDPOINT, - TOKEN_ENDPOINT, ), ) + _LOGGER.warning( + "Configuration of SENZ integration in YAML is deprecated " + "and will be removed in a future release; Your existing OAuth " + "Application Credentials have been imported into the UI " + "automatically and can be safely removed from your " + "configuration.yaml file" + ) return True diff --git a/homeassistant/components/senz/application_credentials.py b/homeassistant/components/senz/application_credentials.py new file mode 100644 index 000000000000..205f00ff33f5 --- /dev/null +++ b/homeassistant/components/senz/application_credentials.py @@ -0,0 +1,14 @@ +"""Application credentials platform for senz.""" + +from aiosenz import AUTHORIZATION_ENDPOINT, TOKEN_ENDPOINT + +from homeassistant.components.application_credentials import AuthorizationServer +from homeassistant.core import HomeAssistant + + +async def async_get_authorization_server(hass: HomeAssistant) -> AuthorizationServer: + """Return authorization server.""" + return AuthorizationServer( + authorize_url=AUTHORIZATION_ENDPOINT, + token_url=TOKEN_ENDPOINT, + ) diff --git a/homeassistant/components/senz/manifest.json b/homeassistant/components/senz/manifest.json index e9b6165cb26d..937a20d8482c 100644 --- a/homeassistant/components/senz/manifest.json +++ b/homeassistant/components/senz/manifest.json @@ -4,7 +4,7 @@ "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/senz", "requirements": ["aiosenz==1.0.0"], - "dependencies": ["auth"], + "dependencies": ["application_credentials"], "codeowners": ["@milanmeu"], "iot_class": "cloud_polling" } diff --git a/homeassistant/generated/application_credentials.py b/homeassistant/generated/application_credentials.py index d7b3259a158c..63c19fe10b8e 100644 --- a/homeassistant/generated/application_credentials.py +++ b/homeassistant/generated/application_credentials.py @@ -11,6 +11,7 @@ APPLICATION_CREDENTIALS = [ "home_connect", "neato", "netatmo", + "senz", "spotify", "withings", "xbox", diff --git a/tests/components/senz/test_config_flow.py b/tests/components/senz/test_config_flow.py index 5143acea60a3..3906f2c03205 100644 --- a/tests/components/senz/test_config_flow.py +++ b/tests/components/senz/test_config_flow.py @@ -24,7 +24,6 @@ async def test_full_flow( "senz", { "senz": {"client_id": CLIENT_ID, "client_secret": CLIENT_SECRET}, - "http": {"base_url": "https://example.com"}, }, )