diff --git a/homeassistant/components/spotify/__init__.py b/homeassistant/components/spotify/__init__.py index c057ea240c0..bf4e9d8deae 100644 --- a/homeassistant/components/spotify/__init__.py +++ b/homeassistant/components/spotify/__init__.py @@ -3,6 +3,7 @@ from __future__ import annotations from dataclasses import dataclass from datetime import timedelta +import logging from typing import Any import aiohttp @@ -10,6 +11,10 @@ import requests from spotipy import Spotify, SpotifyException 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 ( ATTR_CREDENTIALS, @@ -19,7 +24,7 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady -from homeassistant.helpers import config_entry_oauth2_flow, config_validation as cv +from homeassistant.helpers import config_validation as cv from homeassistant.helpers.config_entry_oauth2_flow import ( OAuth2Session, async_get_config_entry_implementation, @@ -27,7 +32,6 @@ from homeassistant.helpers.config_entry_oauth2_flow import ( from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed -from . import config_flow from .browse_media import async_browse_media from .const import DOMAIN, LOGGER, SPOTIFY_SCOPES from .util import ( @@ -36,15 +40,20 @@ from .util import ( spotify_uri_from_media_browser_url, ) +_LOGGER = logging.getLogger(__name__) + CONFIG_SCHEMA = vol.Schema( - { - DOMAIN: vol.Schema( - { - vol.Inclusive(CONF_CLIENT_ID, ATTR_CREDENTIALS): cv.string, - vol.Inclusive(CONF_CLIENT_SECRET, ATTR_CREDENTIALS): cv.string, - } - ) - }, + vol.All( + cv.deprecated(DOMAIN), + { + DOMAIN: vol.Schema( + { + vol.Inclusive(CONF_CLIENT_ID, ATTR_CREDENTIALS): cv.string, + vol.Inclusive(CONF_CLIENT_SECRET, ATTR_CREDENTIALS): cv.string, + } + ) + }, + ), extra=vol.ALLOW_EXTRA, ) @@ -76,17 +85,21 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: return True if CONF_CLIENT_ID in config[DOMAIN]: - config_flow.SpotifyFlowHandler.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], - "https://accounts.spotify.com/authorize", - "https://accounts.spotify.com/api/token", ), ) + _LOGGER.warning( + "Configuration of Spotify 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/spotify/application_credentials.py b/homeassistant/components/spotify/application_credentials.py new file mode 100644 index 00000000000..203028ba7d2 --- /dev/null +++ b/homeassistant/components/spotify/application_credentials.py @@ -0,0 +1,12 @@ +"""Application credentials platform for spotify.""" + +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="https://accounts.spotify.com/authorize", + token_url="https://accounts.spotify.com/api/token", + ) diff --git a/homeassistant/components/spotify/manifest.json b/homeassistant/components/spotify/manifest.json index dd18a05bdc7..979f262e54a 100644 --- a/homeassistant/components/spotify/manifest.json +++ b/homeassistant/components/spotify/manifest.json @@ -4,7 +4,7 @@ "documentation": "https://www.home-assistant.io/integrations/spotify", "requirements": ["spotipy==2.19.0"], "zeroconf": ["_spotify-connect._tcp.local."], - "dependencies": ["auth"], + "dependencies": ["application_credentials"], "codeowners": ["@frenck"], "config_flow": true, "quality_scale": "silver", diff --git a/homeassistant/generated/application_credentials.py b/homeassistant/generated/application_credentials.py index 3bde9e4681f..3e283dcfeee 100644 --- a/homeassistant/generated/application_credentials.py +++ b/homeassistant/generated/application_credentials.py @@ -7,5 +7,6 @@ To update, run python3 -m script.hassfest APPLICATION_CREDENTIALS = [ "google", + "spotify", "xbox" ]