From 8b9b65d3f1e7bca9507b4576e8fcda3659cd78a5 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 4 Oct 2024 11:13:03 +0200 Subject: [PATCH] Use reauth helpers in spotify config flow (#127532) Use async_update_reload_and_abort in spotify --- .../components/spotify/config_flow.py | 28 ++++++++----------- homeassistant/components/spotify/strings.json | 1 + tests/components/spotify/test_config_flow.py | 17 +++-------- 3 files changed, 17 insertions(+), 29 deletions(-) diff --git a/homeassistant/components/spotify/config_flow.py b/homeassistant/components/spotify/config_flow.py index 58c7e612a3565f..510f608746ee77 100644 --- a/homeassistant/components/spotify/config_flow.py +++ b/homeassistant/components/spotify/config_flow.py @@ -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 @@ -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 @@ 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"]} ) diff --git a/homeassistant/components/spotify/strings.json b/homeassistant/components/spotify/strings.json index e58d2098bdea5d..6447e6e6d1b2d5 100644 --- a/homeassistant/components/spotify/strings.json +++ b/homeassistant/components/spotify/strings.json @@ -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%]", diff --git a/tests/components/spotify/test_config_flow.py b/tests/components/spotify/test_config_flow.py index 09feb4a6e83d38..dd662d12681c7a 100644 --- a/tests/components/spotify/test_config_flow.py +++ b/tests/components/spotify/test_config_flow.py @@ -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"