From 30a391b88b93bfd0eb8ae4c19cfd35525a90e96e Mon Sep 17 00:00:00 2001 From: jjlawren Date: Sun, 5 Apr 2020 01:21:20 -0500 Subject: [PATCH] Plex logging additions & cleanup (#33681) --- homeassistant/components/plex/server.py | 16 ++++++++----- tests/components/plex/test_init.py | 31 +++++++++++-------------- tests/components/plex/test_server.py | 2 +- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/homeassistant/components/plex/server.py b/homeassistant/components/plex/server.py index f2a4908e119..80e7c92640a 100644 --- a/homeassistant/components/plex/server.py +++ b/homeassistant/components/plex/server.py @@ -159,6 +159,7 @@ class PlexServer: for account in self._plex_server.systemAccounts() if account.name ] + _LOGGER.debug("Linked accounts: %s", self.accounts) owner_account = [ account.name @@ -167,6 +168,7 @@ class PlexServer: ] if owner_account: self._owner_username = owner_account[0] + _LOGGER.debug("Server owner found: '%s'", self._owner_username) self._version = self._plex_server.version @@ -209,11 +211,11 @@ class PlexServer: try: devices = self._plex_server.clients() sessions = self._plex_server.sessions() - except plexapi.exceptions.BadRequest: - _LOGGER.exception("Error requesting Plex client data from server") - return - except requests.exceptions.RequestException as ex: - _LOGGER.warning( + except ( + plexapi.exceptions.BadRequest, + requests.exceptions.RequestException, + ) as ex: + _LOGGER.error( "Could not connect to Plex server: %s (%s)", self.friendly_name, ex ) return @@ -234,7 +236,9 @@ class PlexServer: for player in session.players: if session_username and session_username not in monitored_users: ignored_clients.add(player.machineIdentifier) - _LOGGER.debug("Ignoring Plex client owned by %s", session_username) + _LOGGER.debug( + "Ignoring Plex client owned by '%s'", session_username + ) continue self._known_idle.discard(player.machineIdentifier) available_clients.setdefault( diff --git a/tests/components/plex/test_init.py b/tests/components/plex/test_init.py index 1aef7878df5..cd1ea8725bd 100644 --- a/tests/components/plex/test_init.py +++ b/tests/components/plex/test_init.py @@ -116,24 +116,21 @@ async def test_setup_with_config_entry(hass, caplog): await trigger_plex_update(hass, server_id) - with patch.object( - mock_plex_server, "clients", side_effect=plexapi.exceptions.BadRequest - ) as patched_clients_bad_request: - await trigger_plex_update(hass, server_id) + for test_exception in ( + plexapi.exceptions.BadRequest, + requests.exceptions.RequestException, + ): + with patch.object( + mock_plex_server, "clients", side_effect=test_exception + ) as patched_clients_bad_request: + await trigger_plex_update(hass, server_id) - assert patched_clients_bad_request.called - assert "Error requesting Plex client data from server" in caplog.text - - with patch.object( - mock_plex_server, "clients", side_effect=requests.exceptions.RequestException - ) as patched_clients_requests_exception: - await trigger_plex_update(hass, server_id) - - assert patched_clients_requests_exception.called - assert ( - f"Could not connect to Plex server: {mock_plex_server.friendlyName}" - in caplog.text - ) + assert patched_clients_bad_request.called + assert ( + f"Could not connect to Plex server: {mock_plex_server.friendlyName}" + in caplog.text + ) + caplog.clear() async def test_set_config_entry_unique_id(hass): diff --git a/tests/components/plex/test_server.py b/tests/components/plex/test_server.py index 242c0fe5504..3b70f30189a 100644 --- a/tests/components/plex/test_server.py +++ b/tests/components/plex/test_server.py @@ -94,7 +94,7 @@ async def test_new_ignored_users_available(hass, caplog): assert len(monitored_users) == 1 assert len(ignored_users) == 2 for ignored_user in ignored_users: - assert f"Ignoring Plex client owned by {ignored_user}" in caplog.text + assert f"Ignoring Plex client owned by '{ignored_user}'" in caplog.text sensor = hass.states.get("sensor.plex_plex_server_1") assert sensor.state == str(len(mock_plex_server.accounts))