Use reauth helpers in spotify config flow (#127532)

Use async_update_reload_and_abort in spotify
This commit is contained in:
epenet 2024-10-04 11:13:03 +02:00 committed by GitHub
parent e82368ec85
commit 8b9b65d3f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 17 additions and 29 deletions

View file

@ -8,7 +8,7 @@ from typing import Any
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
@ -22,8 +22,6 @@ class SpotifyFlowHandler(
DOMAIN = DOMAIN
VERSION = 1
reauth_entry: ConfigEntry | None = None
@property
def logger(self) -> logging.Logger:
"""Return logger."""
@ -45,41 +43,39 @@ class SpotifyFlowHandler(
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"]}
)

View file

@ -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%]",

View file

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