Add Spotify media browser capability (#39240)
Co-authored-by: Tobias Sauerwein <cgtobi@gmail.com> Co-authored-by: Bram Kragten <mail@bramkragten.nl> Co-authored-by: Franck Nijhof <frenck@frenck.nl> Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
7 files changed
+390
-21
No files matched your search
@@ -1,12 +1,15 @@
|
||||
"""Config flow for Spotify."""
|
||||
import logging
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from spotipy import Spotify
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import persistent_notification
|
||||
from homeassistant.helpers import config_entry_oauth2_flow
|
||||
|
||||
from .const import DOMAIN
|
||||
from .const import DOMAIN, SPOTIFY_SCOPES
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -17,27 +20,25 @@ class SpotifyFlowHandler(
|
||||
"""Config flow to handle Spotify OAuth2 authentication."""
|
||||
|
||||
DOMAIN = DOMAIN
|
||||
VERSION = 1
|
||||
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""Instantiate config flow."""
|
||||
super().__init__()
|
||||
self.entry: Optional[Dict[str, Any]] = None
|
||||
|
||||
@property
|
||||
def logger(self) -> logging.Logger:
|
||||
"""Return logger."""
|
||||
return logging.getLogger(__name__)
|
||||
|
||||
@property
|
||||
def extra_authorize_data(self) -> dict:
|
||||
def extra_authorize_data(self) -> Dict[str, Any]:
|
||||
"""Extra data that needs to be appended to the authorize url."""
|
||||
scopes = [
|
||||
# Needed to be able to control playback
|
||||
"user-modify-playback-state",
|
||||
# Needed in order to read available devices
|
||||
"user-read-playback-state",
|
||||
# Needed to determine if the user has Spotify Premium
|
||||
"user-read-private",
|
||||
]
|
||||
return {"scope": ",".join(scopes)}
|
||||
return {"scope": ",".join(SPOTIFY_SCOPES)}
|
||||
|
||||
async def async_oauth_create_entry(self, data: dict) -> dict:
|
||||
async def async_oauth_create_entry(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Create an entry for Spotify."""
|
||||
spotify = Spotify(auth=data["token"]["access_token"])
|
||||
|
||||
@@ -48,6 +49,9 @@ class SpotifyFlowHandler(
|
||||
|
||||
name = data["id"] = current_user["id"]
|
||||
|
||||
if self.entry and self.entry["id"] != current_user["id"]:
|
||||
return self.async_abort(reason="reauth_account_mismatch")
|
||||
|
||||
if current_user.get("display_name"):
|
||||
name = current_user["display_name"]
|
||||
data["name"] = name
|
||||
@@ -55,3 +59,37 @@ class SpotifyFlowHandler(
|
||||
await self.async_set_unique_id(current_user["id"])
|
||||
|
||||
return self.async_create_entry(title=name, data=data)
|
||||
|
||||
async def async_step_reauth(self, entry: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Perform reauth upon migration of old entries."""
|
||||
if entry:
|
||||
self.entry = entry
|
||||
|
||||
assert self.hass
|
||||
persistent_notification.async_create(
|
||||
self.hass,
|
||||
f"Spotify integration for account {entry['id']} needs to be re-authenticated. Please go to the integrations page to re-configure it.",
|
||||
"Spotify re-authentication",
|
||||
"spotify_reauth",
|
||||
)
|
||||
|
||||
return await self.async_step_reauth_confirm()
|
||||
|
||||
async def async_step_reauth_confirm(
|
||||
self, user_input: Optional[Dict[str, Any]] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""Confirm reauth dialog."""
|
||||
if user_input is None:
|
||||
return self.async_show_form(
|
||||
step_id="reauth_confirm",
|
||||
description_placeholders={"account": self.entry["id"]},
|
||||
data_schema=vol.Schema({}),
|
||||
errors={},
|
||||
)
|
||||
|
||||
assert self.hass
|
||||
persistent_notification.async_dismiss(self.hass, "spotify_reauth")
|
||||
|
||||
return await self.async_step_pick_implementation(
|
||||
user_input={"implementation": self.entry["auth_implementation"]}
|
||||
)
|
||||
Reference in new issue
Block a user