Handle invalidated Plex token (#91438)

This commit is contained in:
jjlawren 2023-04-17 02:38:16 -05:00 committed by GitHub
parent d26160a509
commit c88d4b09c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 0 deletions

View file

@ -35,6 +35,7 @@ from .const import (
CONF_SERVER_IDENTIFIER,
DISPATCHERS,
DOMAIN,
INVALID_TOKEN_MESSAGE,
PLATFORMS,
PLATFORMS_COMPLETED,
PLEX_SERVER_CONFIG,
@ -153,6 +154,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
plexapi.exceptions.BadRequest,
plexapi.exceptions.NotFound,
) as error:
if INVALID_TOKEN_MESSAGE in str(error):
raise ConfigEntryAuthFailed(
"Token not accepted, please reauthenticate Plex server"
f" '{entry.data[CONF_SERVER]}'"
) from error
_LOGGER.error(
"Login to %s failed, verify token and SSL settings: [%s]",
entry.data[CONF_SERVER],

View file

@ -57,3 +57,5 @@ SERVICE_REFRESH_LIBRARY = "refresh_library"
SERVICE_SCAN_CLIENTS = "scan_for_clients"
PLEX_URI_SCHEME = "plex://"
INVALID_TOKEN_MESSAGE = "Invalid token"

View file

@ -360,3 +360,19 @@ async def test_trigger_reauth(
flows = hass.config_entries.flow.async_progress()
assert len(flows) == 1
assert flows[0]["context"]["source"] == SOURCE_REAUTH
async def test_setup_with_deauthorized_token(
hass: HomeAssistant, entry, setup_plex_server
) -> None:
"""Test setup with a deauthorized token."""
with patch(
"plexapi.server.PlexServer",
side_effect=plexapi.exceptions.BadRequest(const.INVALID_TOKEN_MESSAGE),
):
entry.add_to_hass(hass)
assert not await hass.config_entries.async_setup(entry.entry_id)
flows = hass.config_entries.flow.async_progress()
assert len(flows) == 1
assert flows[0]["context"]["source"] == SOURCE_REAUTH