Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use reauth helpers in spotify config flow #127532

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions homeassistant/components/spotify/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from spotipy import Spotify

from homeassistant.config_entries import ConfigEntry, ConfigFlowResult
from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlowResult
from homeassistant.helpers import config_entry_oauth2_flow

from .const import DOMAIN, SPOTIFY_SCOPES
Expand All @@ -22,8 +22,6 @@ class SpotifyFlowHandler(
DOMAIN = DOMAIN
VERSION = 1

reauth_entry: ConfigEntry | None = None

@property
def logger(self) -> logging.Logger:
"""Return logger."""
Expand All @@ -45,41 +43,39 @@ async def async_oauth_create_entry(self, data: dict[str, Any]) -> ConfigFlowResu

name = data["id"] = current_user["id"]

if self.reauth_entry and self.reauth_entry.data["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

await self.async_set_unique_id(current_user["id"])

if self.source == SOURCE_REAUTH:
reauth_entry = self._get_reauth_entry()
if reauth_entry.data["id"] != current_user["id"]:
return self.async_abort(reason="reauth_account_mismatch")
return self.async_update_reload_and_abort(
reauth_entry, title=name, data=data
)
return self.async_create_entry(title=name, data=data)

async def async_step_reauth(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
"""Perform reauth upon migration of old entries."""
self.reauth_entry = self.hass.config_entries.async_get_entry(
self.context["entry_id"]
)

return await self.async_step_reauth_confirm()

async def async_step_reauth_confirm(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Confirm reauth dialog."""
if self.reauth_entry is None:
return self.async_abort(reason="reauth_account_mismatch")

if user_input is None and self.reauth_entry:
reauth_entry = self._get_reauth_entry()
if user_input is None:
return self.async_show_form(
step_id="reauth_confirm",
description_placeholders={"account": self.reauth_entry.data["id"]},
description_placeholders={"account": reauth_entry.data["id"]},
errors={},
)

return await self.async_step_pick_implementation(
user_input={"implementation": self.reauth_entry.data["auth_implementation"]}
user_input={"implementation": reauth_entry.data["auth_implementation"]}
)
1 change: 1 addition & 0 deletions homeassistant/components/spotify/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"missing_configuration": "The Spotify integration is not configured. Please follow the documentation.",
"no_url_available": "[%key:common::config_flow::abort::oauth2_no_url_available%]",
"reauth_account_mismatch": "The Spotify account authenticated with, does not match the account needed re-authentication.",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
"oauth_error": "[%key:common::config_flow::abort::oauth2_error%]",
"oauth_timeout": "[%key:common::config_flow::abort::oauth2_timeout%]",
"oauth_unauthorized": "[%key:common::config_flow::abort::oauth2_unauthorized%]",
Expand Down
17 changes: 4 additions & 13 deletions tests/components/spotify/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,10 @@ async def test_reauthentication(
spotify_mock.return_value.current_user.return_value = {"id": "frenck"}
result = await hass.config_entries.flow.async_configure(result["flow_id"])

assert result["data"]["auth_implementation"] == "cred"
result["data"]["token"].pop("expires_at")
assert result["data"]["token"] == {
updated_data = old_entry.data.copy()
assert updated_data["auth_implementation"] == "cred"
updated_data["token"].pop("expires_at")
assert updated_data["token"] == {
"refresh_token": "mock-refresh-token",
"access_token": "mock-access-token",
"type": "Bearer",
Expand Down Expand Up @@ -292,13 +293,3 @@ async def test_reauth_account_mismatch(

assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reauth_account_mismatch"


async def test_abort_if_no_reauth_entry(hass: HomeAssistant) -> None:
"""Check flow aborts when no entry is known when entring reauth confirmation."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "reauth_confirm"}
)

assert result.get("type") is FlowResultType.ABORT
assert result.get("reason") == "reauth_account_mismatch"