diff --git a/homeassistant/components/plex/server.py b/homeassistant/components/plex/server.py index e62be0244fe..66f02881b78 100644 --- a/homeassistant/components/plex/server.py +++ b/homeassistant/components/plex/server.py @@ -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 diff --git a/tests/components/plex/test_config_flow.py b/tests/components/plex/test_config_flow.py index 2c0faadcc3b..d901f7e62f0 100644 --- a/tests/components/plex/test_config_flow.py +++ b/tests/components/plex/test_config_flow.py @@ -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