From f6370d052273facede196ff92678c58d653d0a65 Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Mon, 23 May 2022 11:16:21 -0700 Subject: [PATCH] Add Honeywell Lyric application credentials platform and deprecate configuration in yaml (#72335) Add Honeywell Lyric application credentials platform and deprecate config yaml --- homeassistant/components/lyric/__init__.py | 49 ++++++++++++------- homeassistant/components/lyric/api.py | 3 +- .../lyric/application_credentials.py | 26 ++++++++++ homeassistant/components/lyric/manifest.json | 2 +- .../generated/application_credentials.py | 1 + tests/components/lyric/test_config_flow.py | 2 +- 6 files changed, 61 insertions(+), 22 deletions(-) create mode 100644 homeassistant/components/lyric/application_credentials.py diff --git a/homeassistant/components/lyric/__init__.py b/homeassistant/components/lyric/__init__.py index 3709fa5445f..2eaee440ae7 100644 --- a/homeassistant/components/lyric/__init__.py +++ b/homeassistant/components/lyric/__init__.py @@ -13,6 +13,10 @@ from aiolyric.objects.location import LyricLocation import async_timeout 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 @@ -36,18 +40,20 @@ from .api import ( LyricLocalOAuth2Implementation, OAuth2SessionLyric, ) -from .config_flow import OAuth2FlowHandler -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, - } - ) - }, + 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, ) @@ -63,20 +69,23 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: if DOMAIN not in config: return True - hass.data[DOMAIN][CONF_CLIENT_ID] = config[DOMAIN][CONF_CLIENT_ID] - - OAuth2FlowHandler.async_register_implementation( + await async_import_client_credential( hass, - LyricLocalOAuth2Implementation( - hass, - DOMAIN, + DOMAIN, + ClientCredential( config[DOMAIN][CONF_CLIENT_ID], config[DOMAIN][CONF_CLIENT_SECRET], - OAUTH2_AUTHORIZE, - OAUTH2_TOKEN, ), ) + _LOGGER.warning( + "Configuration of Honeywell Lyric 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 @@ -87,13 +96,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass, entry ) ) + if not isinstance(implementation, LyricLocalOAuth2Implementation): + raise ValueError("Unexpected auth implementation; can't find oauth client id") session = aiohttp_client.async_get_clientsession(hass) oauth_session = OAuth2SessionLyric(hass, entry, implementation) client = ConfigEntryLyricClient(session, oauth_session) - client_id = hass.data[DOMAIN][CONF_CLIENT_ID] + client_id = implementation.client_id lyric = Lyric(client, client_id) async def async_update_data(force_refresh_token: bool = False) -> Lyric: diff --git a/homeassistant/components/lyric/api.py b/homeassistant/components/lyric/api.py index 4a8aa44417f..171e010137a 100644 --- a/homeassistant/components/lyric/api.py +++ b/homeassistant/components/lyric/api.py @@ -4,6 +4,7 @@ from typing import cast from aiohttp import BasicAuth, ClientSession from aiolyric.client import LyricClient +from homeassistant.components.application_credentials import AuthImplementation from homeassistant.helpers import config_entry_oauth2_flow from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -41,7 +42,7 @@ class ConfigEntryLyricClient(LyricClient): class LyricLocalOAuth2Implementation( - config_entry_oauth2_flow.LocalOAuth2Implementation + AuthImplementation, ): """Lyric Local OAuth2 implementation.""" diff --git a/homeassistant/components/lyric/application_credentials.py b/homeassistant/components/lyric/application_credentials.py new file mode 100644 index 00000000000..2ccdca72bb6 --- /dev/null +++ b/homeassistant/components/lyric/application_credentials.py @@ -0,0 +1,26 @@ +"""Application credentials platform for the Honeywell Lyric integration.""" + +from homeassistant.components.application_credentials import ( + AuthorizationServer, + ClientCredential, +) +from homeassistant.core import HomeAssistant +from homeassistant.helpers import config_entry_oauth2_flow + +from .api import LyricLocalOAuth2Implementation +from .const import OAUTH2_AUTHORIZE, OAUTH2_TOKEN + + +async def async_get_auth_implementation( + hass: HomeAssistant, auth_domain: str, credential: ClientCredential +) -> config_entry_oauth2_flow.AbstractOAuth2Implementation: + """Return custom auth implementation.""" + return LyricLocalOAuth2Implementation( + hass, + auth_domain, + credential, + AuthorizationServer( + authorize_url=OAUTH2_AUTHORIZE, + token_url=OAUTH2_TOKEN, + ), + ) diff --git a/homeassistant/components/lyric/manifest.json b/homeassistant/components/lyric/manifest.json index da60b046eb7..c0d9168f46f 100644 --- a/homeassistant/components/lyric/manifest.json +++ b/homeassistant/components/lyric/manifest.json @@ -3,7 +3,7 @@ "name": "Honeywell Lyric", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/lyric", - "dependencies": ["auth"], + "dependencies": ["application_credentials"], "requirements": ["aiolyric==1.0.8"], "codeowners": ["@timmo001"], "quality_scale": "silver", diff --git a/homeassistant/generated/application_credentials.py b/homeassistant/generated/application_credentials.py index 63c19fe10b8..6d40b3fdef7 100644 --- a/homeassistant/generated/application_credentials.py +++ b/homeassistant/generated/application_credentials.py @@ -9,6 +9,7 @@ APPLICATION_CREDENTIALS = [ "geocaching", "google", "home_connect", + "lyric", "neato", "netatmo", "senz", diff --git a/tests/components/lyric/test_config_flow.py b/tests/components/lyric/test_config_flow.py index 25cc49c6c09..d9262e215fb 100644 --- a/tests/components/lyric/test_config_flow.py +++ b/tests/components/lyric/test_config_flow.py @@ -41,7 +41,7 @@ async def test_abort_if_no_configuration(hass): DOMAIN, context={"source": config_entries.SOURCE_USER} ) assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT - assert result["reason"] == "missing_configuration" + assert result["reason"] == "missing_credentials" async def test_full_flow(