Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plex logging additions & cleanup #33681

Merged
merged 1 commit into from Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions homeassistant/components/plex/server.py
Expand Up @@ -159,6 +159,7 @@ def _update_plexdirect_hostname():
for account in self._plex_server.systemAccounts()
if account.name
]
_LOGGER.debug("Linked accounts: %s", self.accounts)

owner_account = [
account.name
Expand All @@ -167,6 +168,7 @@ def _update_plexdirect_hostname():
]
if owner_account:
self._owner_username = owner_account[0]
_LOGGER.debug("Server owner found: '%s'", self._owner_username)

self._version = self._plex_server.version

Expand Down Expand Up @@ -209,11 +211,11 @@ def update_platforms(self):
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
Expand All @@ -234,7 +236,9 @@ def update_platforms(self):
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(
Expand Down
33 changes: 15 additions & 18 deletions tests/components/plex/test_init.py
Expand Up @@ -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)

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
)
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 (
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):
Expand Down
2 changes: 1 addition & 1 deletion tests/components/plex/test_server.py
Expand Up @@ -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))
Expand Down