mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 02:07:09 +00:00
Use reauth helpers in spotify config flow (#127532)
Use async_update_reload_and_abort in spotify
This commit is contained in:
parent
e82368ec85
commit
8b9b65d3f1
@ -8,7 +8,7 @@ from typing import Any
|
|||||||
|
|
||||||
from spotipy import Spotify
|
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 homeassistant.helpers import config_entry_oauth2_flow
|
||||||
|
|
||||||
from .const import DOMAIN, SPOTIFY_SCOPES
|
from .const import DOMAIN, SPOTIFY_SCOPES
|
||||||
@ -22,8 +22,6 @@ class SpotifyFlowHandler(
|
|||||||
DOMAIN = DOMAIN
|
DOMAIN = DOMAIN
|
||||||
VERSION = 1
|
VERSION = 1
|
||||||
|
|
||||||
reauth_entry: ConfigEntry | None = None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def logger(self) -> logging.Logger:
|
def logger(self) -> logging.Logger:
|
||||||
"""Return logger."""
|
"""Return logger."""
|
||||||
@ -45,41 +43,39 @@ class SpotifyFlowHandler(
|
|||||||
|
|
||||||
name = data["id"] = current_user["id"]
|
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"):
|
if current_user.get("display_name"):
|
||||||
name = current_user["display_name"]
|
name = current_user["display_name"]
|
||||||
data["name"] = name
|
data["name"] = name
|
||||||
|
|
||||||
await self.async_set_unique_id(current_user["id"])
|
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)
|
return self.async_create_entry(title=name, data=data)
|
||||||
|
|
||||||
async def async_step_reauth(
|
async def async_step_reauth(
|
||||||
self, entry_data: Mapping[str, Any]
|
self, entry_data: Mapping[str, Any]
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Perform reauth upon migration of old entries."""
|
"""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()
|
return await self.async_step_reauth_confirm()
|
||||||
|
|
||||||
async def async_step_reauth_confirm(
|
async def async_step_reauth_confirm(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Confirm reauth dialog."""
|
"""Confirm reauth dialog."""
|
||||||
if self.reauth_entry is None:
|
reauth_entry = self._get_reauth_entry()
|
||||||
return self.async_abort(reason="reauth_account_mismatch")
|
if user_input is None:
|
||||||
|
|
||||||
if user_input is None and self.reauth_entry:
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="reauth_confirm",
|
step_id="reauth_confirm",
|
||||||
description_placeholders={"account": self.reauth_entry.data["id"]},
|
description_placeholders={"account": reauth_entry.data["id"]},
|
||||||
errors={},
|
errors={},
|
||||||
)
|
)
|
||||||
|
|
||||||
return await self.async_step_pick_implementation(
|
return await self.async_step_pick_implementation(
|
||||||
user_input={"implementation": self.reauth_entry.data["auth_implementation"]}
|
user_input={"implementation": reauth_entry.data["auth_implementation"]}
|
||||||
)
|
)
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
"missing_configuration": "The Spotify integration is not configured. Please follow the documentation.",
|
"missing_configuration": "The Spotify integration is not configured. Please follow the documentation.",
|
||||||
"no_url_available": "[%key:common::config_flow::abort::oauth2_no_url_available%]",
|
"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_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_error": "[%key:common::config_flow::abort::oauth2_error%]",
|
||||||
"oauth_timeout": "[%key:common::config_flow::abort::oauth2_timeout%]",
|
"oauth_timeout": "[%key:common::config_flow::abort::oauth2_timeout%]",
|
||||||
"oauth_unauthorized": "[%key:common::config_flow::abort::oauth2_unauthorized%]",
|
"oauth_unauthorized": "[%key:common::config_flow::abort::oauth2_unauthorized%]",
|
||||||
|
@ -235,9 +235,10 @@ async def test_reauthentication(
|
|||||||
spotify_mock.return_value.current_user.return_value = {"id": "frenck"}
|
spotify_mock.return_value.current_user.return_value = {"id": "frenck"}
|
||||||
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
||||||
|
|
||||||
assert result["data"]["auth_implementation"] == "cred"
|
updated_data = old_entry.data.copy()
|
||||||
result["data"]["token"].pop("expires_at")
|
assert updated_data["auth_implementation"] == "cred"
|
||||||
assert result["data"]["token"] == {
|
updated_data["token"].pop("expires_at")
|
||||||
|
assert updated_data["token"] == {
|
||||||
"refresh_token": "mock-refresh-token",
|
"refresh_token": "mock-refresh-token",
|
||||||
"access_token": "mock-access-token",
|
"access_token": "mock-access-token",
|
||||||
"type": "Bearer",
|
"type": "Bearer",
|
||||||
@ -292,13 +293,3 @@ async def test_reauth_account_mismatch(
|
|||||||
|
|
||||||
assert result["type"] is FlowResultType.ABORT
|
assert result["type"] is FlowResultType.ABORT
|
||||||
assert result["reason"] == "reauth_account_mismatch"
|
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"
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user