From 425d56d024d82dc593703803520184307558f224 Mon Sep 17 00:00:00 2001 From: jjlawren Date: Tue, 23 Feb 2021 16:36:46 -0600 Subject: [PATCH] Fix Plex showing removed shared users (#46971) --- homeassistant/components/plex/server.py | 20 +++++++++++++------- tests/components/plex/conftest.py | 10 ++++++++++ tests/components/plex/test_init.py | 4 ++++ tests/fixtures/plex/plextv_shared_users.xml | 9 +++++++++ 4 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 tests/fixtures/plex/plextv_shared_users.xml diff --git a/homeassistant/components/plex/server.py b/homeassistant/components/plex/server.py index 1baceb78ff1..8f9d4d1cc51 100644 --- a/homeassistant/components/plex/server.py +++ b/homeassistant/components/plex/server.py @@ -213,21 +213,27 @@ class PlexServer: try: system_accounts = self._plex_server.systemAccounts() + shared_users = self.account.users() if self.account else [] 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 - ] + self._accounts = [] + for user in shared_users: + for shared_server in user.servers: + if shared_server.machineIdentifier == self.machine_identifier: + self._accounts.append(user.title) + _LOGGER.debug("Linked accounts: %s", self.accounts) - owner_account = [ - account.name for account in system_accounts if account.accountID == 1 - ] + owner_account = next( + (account.name for account in system_accounts if account.accountID == 1), + None, + ) if owner_account: - self._owner_username = owner_account[0] + self._owner_username = owner_account + self._accounts.append(owner_account) _LOGGER.debug("Server owner found: '%s'", self._owner_username) self._version = self._plex_server.version diff --git a/tests/components/plex/conftest.py b/tests/components/plex/conftest.py index d3e66cc4989..372a06f15b6 100644 --- a/tests/components/plex/conftest.py +++ b/tests/components/plex/conftest.py @@ -218,6 +218,12 @@ def plextv_resources_fixture(plextv_resources_base): return plextv_resources_base.format(second_server_enabled=0) +@pytest.fixture(name="plextv_shared_users", scope="session") +def plextv_shared_users_fixture(plextv_resources_base): + """Load payload for plex.tv shared users and return it.""" + return load_fixture("plex/plextv_shared_users.xml") + + @pytest.fixture(name="session_base", scope="session") def session_base_fixture(): """Load the base session payload and return it.""" @@ -293,6 +299,7 @@ def mock_plex_calls( children_200, children_300, empty_library, + empty_payload, grandchildren_300, library, library_sections, @@ -310,12 +317,15 @@ def mock_plex_calls( playlist_500, plextv_account, plextv_resources, + plextv_shared_users, plex_server_accounts, plex_server_clients, plex_server_default, security_token, ): """Mock Plex API calls.""" + requests_mock.get("https://plex.tv/api/users/", text=plextv_shared_users) + requests_mock.get("https://plex.tv/api/invites/requested", text=empty_payload) requests_mock.get("https://plex.tv/users/account", text=plextv_account) requests_mock.get("https://plex.tv/api/resources", text=plextv_resources) diff --git a/tests/components/plex/test_init.py b/tests/components/plex/test_init.py index 95d2ef9bddb..2e5a30ce11a 100644 --- a/tests/components/plex/test_init.py +++ b/tests/components/plex/test_init.py @@ -116,6 +116,7 @@ async def test_setup_when_certificate_changed( plex_server_default, plextv_account, plextv_resources, + plextv_shared_users, ): """Test setup component when the Plex certificate has changed.""" await async_setup_component(hass, "persistent_notification", {}) @@ -141,6 +142,9 @@ async def test_setup_when_certificate_changed( unique_id=DEFAULT_DATA["server_id"], ) + requests_mock.get("https://plex.tv/api/users/", text=plextv_shared_users) + requests_mock.get("https://plex.tv/api/invites/requested", text=empty_payload) + requests_mock.get("https://plex.tv/users/account", text=plextv_account) requests_mock.get("https://plex.tv/api/resources", text=plextv_resources) requests_mock.get(old_url, exc=WrongCertHostnameException) diff --git a/tests/fixtures/plex/plextv_shared_users.xml b/tests/fixtures/plex/plextv_shared_users.xml new file mode 100644 index 00000000000..9421bdfa17a --- /dev/null +++ b/tests/fixtures/plex/plextv_shared_users.xml @@ -0,0 +1,9 @@ + + + + + + + + +