Add Spotify application_credentials platform (#71871)

This commit is contained in:
Allen Porter 2022-05-16 00:31:14 -07:00 committed by GitHub
parent a70e2a33dc
commit 2613c865f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 17 deletions

View File

@ -3,6 +3,7 @@ from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from datetime import timedelta from datetime import timedelta
import logging
from typing import Any from typing import Any
import aiohttp import aiohttp
@ -10,6 +11,10 @@ import requests
from spotipy import Spotify, SpotifyException from spotipy import Spotify, SpotifyException
import voluptuous as vol import voluptuous as vol
from homeassistant.components.application_credentials import (
ClientCredential,
async_import_client_credential,
)
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
ATTR_CREDENTIALS, ATTR_CREDENTIALS,
@ -19,7 +24,7 @@ from homeassistant.const import (
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady 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 ( from homeassistant.helpers.config_entry_oauth2_flow import (
OAuth2Session, OAuth2Session,
async_get_config_entry_implementation, 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.typing import ConfigType
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from . import config_flow
from .browse_media import async_browse_media from .browse_media import async_browse_media
from .const import DOMAIN, LOGGER, SPOTIFY_SCOPES from .const import DOMAIN, LOGGER, SPOTIFY_SCOPES
from .util import ( from .util import (
@ -36,15 +40,20 @@ from .util import (
spotify_uri_from_media_browser_url, spotify_uri_from_media_browser_url,
) )
_LOGGER = logging.getLogger(__name__)
CONFIG_SCHEMA = vol.Schema( CONFIG_SCHEMA = vol.Schema(
{ vol.All(
DOMAIN: vol.Schema( cv.deprecated(DOMAIN),
{ {
vol.Inclusive(CONF_CLIENT_ID, ATTR_CREDENTIALS): cv.string, DOMAIN: vol.Schema(
vol.Inclusive(CONF_CLIENT_SECRET, ATTR_CREDENTIALS): cv.string, {
} vol.Inclusive(CONF_CLIENT_ID, ATTR_CREDENTIALS): cv.string,
) vol.Inclusive(CONF_CLIENT_SECRET, ATTR_CREDENTIALS): cv.string,
}, }
)
},
),
extra=vol.ALLOW_EXTRA, extra=vol.ALLOW_EXTRA,
) )
@ -76,17 +85,21 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
return True return True
if CONF_CLIENT_ID in config[DOMAIN]: if CONF_CLIENT_ID in config[DOMAIN]:
config_flow.SpotifyFlowHandler.async_register_implementation( await async_import_client_credential(
hass, hass,
config_entry_oauth2_flow.LocalOAuth2Implementation( DOMAIN,
hass, ClientCredential(
DOMAIN,
config[DOMAIN][CONF_CLIENT_ID], config[DOMAIN][CONF_CLIENT_ID],
config[DOMAIN][CONF_CLIENT_SECRET], 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 return True

View File

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

View File

@ -4,7 +4,7 @@
"documentation": "https://www.home-assistant.io/integrations/spotify", "documentation": "https://www.home-assistant.io/integrations/spotify",
"requirements": ["spotipy==2.19.0"], "requirements": ["spotipy==2.19.0"],
"zeroconf": ["_spotify-connect._tcp.local."], "zeroconf": ["_spotify-connect._tcp.local."],
"dependencies": ["auth"], "dependencies": ["application_credentials"],
"codeowners": ["@frenck"], "codeowners": ["@frenck"],
"config_flow": true, "config_flow": true,
"quality_scale": "silver", "quality_scale": "silver",

View File

@ -7,5 +7,6 @@ To update, run python3 -m script.hassfest
APPLICATION_CREDENTIALS = [ APPLICATION_CREDENTIALS = [
"google", "google",
"spotify",
"xbox" "xbox"
] ]