Add reauth flow to PlayStation Network integration (#147397)

* Add reauth flow to psn integration

* changes

* catch auth error in coordinator
This commit is contained in:
Manu
2025-06-24 12:20:08 +02:00
committed by GitHub
parent 02e33c3551
commit 38c7eaf70a
6 changed files with 243 additions and 9 deletions

View File

@@ -1,5 +1,6 @@
"""Config flow for the PlayStation Network integration."""
from collections.abc import Mapping
import logging
from typing import Any
@@ -14,8 +15,9 @@ from psnawp_api.utils.misc import parse_npsso_token
import voluptuous as vol
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_NAME
from .const import CONF_NPSSO, DOMAIN
from .const import CONF_NPSSO, DOMAIN, NPSSO_LINK, PSN_LINK
from .helpers import PlaystationNetwork
_LOGGER = logging.getLogger(__name__)
@@ -63,7 +65,61 @@ class PlaystationNetworkConfigFlow(ConfigFlow, domain=DOMAIN):
data_schema=STEP_USER_DATA_SCHEMA,
errors=errors,
description_placeholders={
"npsso_link": "https://ca.account.sony.com/api/v1/ssocookie",
"psn_link": "https://playstation.com",
"npsso_link": NPSSO_LINK,
"psn_link": PSN_LINK,
},
)
async def async_step_reauth(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
"""Perform reauth upon an API authentication error."""
return await self.async_step_reauth_confirm()
async def async_step_reauth_confirm(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Confirm reauthentication dialog."""
errors: dict[str, str] = {}
entry = self._get_reauth_entry()
if user_input is not None:
try:
npsso = parse_npsso_token(user_input[CONF_NPSSO])
psn = PlaystationNetwork(self.hass, npsso)
user: User = await psn.get_user()
except PSNAWPAuthenticationError:
errors["base"] = "invalid_auth"
except (PSNAWPNotFoundError, PSNAWPInvalidTokenError):
errors["base"] = "invalid_account"
except PSNAWPError:
errors["base"] = "cannot_connect"
except Exception:
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
else:
await self.async_set_unique_id(user.account_id)
self._abort_if_unique_id_mismatch(
description_placeholders={
"wrong_account": user.online_id,
CONF_NAME: entry.title,
}
)
return self.async_update_reload_and_abort(
entry,
data_updates={CONF_NPSSO: npsso},
)
return self.async_show_form(
step_id="reauth_confirm",
data_schema=self.add_suggested_values_to_schema(
data_schema=STEP_USER_DATA_SCHEMA, suggested_values=user_input
),
errors=errors,
description_placeholders={
"npsso_link": NPSSO_LINK,
"psn_link": PSN_LINK,
},
)

View File

@@ -13,3 +13,6 @@ SUPPORTED_PLATFORMS = {
PlatformType.PS3,
PlatformType.PSPC,
}
NPSSO_LINK: Final = "https://ca.account.sony.com/api/v1/ssocookie"
PSN_LINK: Final = "https://playstation.com"

View File

@@ -13,7 +13,7 @@ from psnawp_api.models.user import User
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import DOMAIN
@@ -53,7 +53,7 @@ class PlaystationNetworkCoordinator(DataUpdateCoordinator[PlaystationNetworkData
try:
self.user = await self.psn.get_user()
except PSNAWPAuthenticationError as error:
raise ConfigEntryNotReady(
raise ConfigEntryAuthFailed(
translation_domain=DOMAIN,
translation_key="not_ready",
) from error
@@ -62,7 +62,12 @@ class PlaystationNetworkCoordinator(DataUpdateCoordinator[PlaystationNetworkData
"""Get the latest data from the PSN."""
try:
return await self.psn.get_data()
except (PSNAWPAuthenticationError, PSNAWPServerError) as error:
except PSNAWPAuthenticationError as error:
raise ConfigEntryAuthFailed(
translation_domain=DOMAIN,
translation_key="not_ready",
) from error
except PSNAWPServerError as error:
raise UpdateFailed(
translation_domain=DOMAIN,
translation_key="update_failed",

View File

@@ -39,7 +39,7 @@ rules:
integration-owner: done
log-when-unavailable: done
parallel-updates: done
reauthentication-flow: todo
reauthentication-flow: done
test-coverage: todo
# Gold

View File

@@ -9,6 +9,16 @@
"npsso": "The NPSSO token is generated upon successful login of your PlayStation Network account and is used to authenticate your requests within Home Assistant."
},
"description": "To obtain your NPSSO token, log in to your [PlayStation account]({psn_link}) first. Then [click here]({npsso_link}) to retrieve the token."
},
"reauth_confirm": {
"title": "Re-authenticate {name} with PlayStation Network",
"description": "The NPSSO token for **{name}** has expired. To obtain a new one, log in to your [PlayStation account]({psn_link}) first. Then [click here]({npsso_link}) to retrieve the token.",
"data": {
"npsso": "[%key:component::playstation_network::config::step::user::data::npsso%]"
},
"data_description": {
"npsso": "[%key:component::playstation_network::config::step::user::data_description::npsso%]"
}
}
},
"error": {
@@ -18,7 +28,9 @@
"unknown": "[%key:common::config_flow::error::unknown%]"
},
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]"
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
"unique_id_mismatch": "The provided NPSSO token corresponds to the account {wrong_account}. Please re-authenticate with the account **{name}**"
}
},
"exceptions": {