Refactor Plex device/session updates (#34616)

This commit is contained in:
jjlawren 2020-04-23 20:12:39 -05:00 committed by GitHub
parent f94329dbbd
commit c582911879
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 13 deletions

View File

@ -207,36 +207,37 @@ class PlexServer:
)
return
for device in devices:
def process_device(source, device):
self._known_idle.discard(device.machineIdentifier)
available_clients[device.machineIdentifier] = {"device": device}
available_clients.setdefault(device.machineIdentifier, {"device": device})
if device.machineIdentifier not in self._known_clients:
new_clients.add(device.machineIdentifier)
_LOGGER.debug("New device: %s", device.machineIdentifier)
_LOGGER.debug(
"New %s %s: %s", device.product, source, device.machineIdentifier
)
for device in devices:
process_device("device", device)
for session in sessions:
if session.TYPE == "photo":
_LOGGER.debug("Photo session detected, skipping: %s", session)
continue
session_username = session.usernames[0]
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
"Ignoring %s client owned by '%s'",
player.product,
session_username,
)
continue
self._known_idle.discard(player.machineIdentifier)
available_clients.setdefault(
player.machineIdentifier, {"device": player}
)
process_device("session", player)
available_clients[player.machineIdentifier]["session"] = session
if player.machineIdentifier not in self._known_clients:
new_clients.add(player.machineIdentifier)
_LOGGER.debug("New session: %s", player.machineIdentifier)
new_entity_configs = []
for client_id, client_data in available_clients.items():
if client_id in ignored_clients:

View File

@ -93,7 +93,15 @@ 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
ignored_client = [
x.players[0]
for x in mock_plex_server.sessions()
if x.usernames[0] in ignored_users
][0]
assert (
f"Ignoring {ignored_client.product} 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))