Handle config with a limited Plex account (#35218)

This commit is contained in:
jjlawren 2020-05-06 20:46:00 -05:00 committed by GitHub
parent 64ce8e970f
commit dfe2a457c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 14 deletions

View file

@ -3,6 +3,7 @@ import logging
import ssl
from urllib.parse import urlparse
from plexapi.exceptions import Unauthorized
import plexapi.myplex
import plexapi.playqueue
import plexapi.server
@ -138,21 +139,24 @@ class PlexServer:
else:
_connect_with_token()
self._accounts = [
account.name
for account in self._plex_server.systemAccounts()
if account.name
]
_LOGGER.debug("Linked accounts: %s", self.accounts)
try:
system_accounts = self._plex_server.systemAccounts()
except Unauthorized:
_LOGGER.warning(
"Plex account has limited permissions, shared account filtering will not be available."
)
else:
self._accounts = [
account.name for account in system_accounts if account.name
]
_LOGGER.debug("Linked accounts: %s", self.accounts)
owner_account = [
account.name
for account in self._plex_server.systemAccounts()
if account.accountID == 1
]
if owner_account:
self._owner_username = owner_account[0]
_LOGGER.debug("Server owner found: '%s'", self._owner_username)
owner_account = [
account.name for account in system_accounts if account.accountID == 1
]
if owner_account:
self._owner_username = owner_account[0]
_LOGGER.debug("Server owner found: '%s'", self._owner_username)
self._version = self._plex_server.version

View file

@ -688,3 +688,36 @@ async def test_manual_config_with_token(hass):
assert result["data"][CONF_SERVER_IDENTIFIER] == mock_plex_server.machineIdentifier
assert result["data"][PLEX_SERVER_CONFIG][CONF_URL] == mock_plex_server._baseurl
assert result["data"][PLEX_SERVER_CONFIG][CONF_TOKEN] == MOCK_TOKEN
async def test_setup_with_limited_credentials(hass):
"""Test setup with a user with limited permissions."""
mock_plex_server = MockPlexServer()
entry = MockConfigEntry(
domain=DOMAIN,
data=DEFAULT_DATA,
options=DEFAULT_OPTIONS,
unique_id=DEFAULT_DATA["server_id"],
)
with patch(
"plexapi.server.PlexServer", return_value=mock_plex_server
), patch.object(
mock_plex_server, "systemAccounts", side_effect=plexapi.exceptions.Unauthorized
) as mock_accounts, patch(
"homeassistant.components.plex.PlexWebsocket.listen"
) as mock_listen:
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert mock_listen.called
assert mock_accounts.called
plex_server = hass.data[DOMAIN][SERVERS][mock_plex_server.machineIdentifier]
assert len(plex_server.accounts) == 0
assert plex_server.owner is None
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert entry.state == ENTRY_STATE_LOADED