From 8d22479d24d6f63f7f5a415e8b2fcda72bec1b2b Mon Sep 17 00:00:00 2001 From: jjlawren Date: Thu, 23 May 2019 07:00:41 -0500 Subject: [PATCH] Always update all Plex client types (#24038) --- homeassistant/components/plex/media_player.py | 79 ++++++++----------- 1 file changed, 33 insertions(+), 46 deletions(-) diff --git a/homeassistant/components/plex/media_player.py b/homeassistant/components/plex/media_player.py index 4a65808e049..4d5f70cf36e 100644 --- a/homeassistant/components/plex/media_player.py +++ b/homeassistant/components/plex/media_player.py @@ -29,7 +29,6 @@ MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(seconds=1) PLEX_CONFIG_FILE = 'plex.conf' PLEX_DATA = 'plex' -CONF_INCLUDE_NON_CLIENTS = 'include_non_clients' CONF_USE_EPISODE_ART = 'use_episode_art' CONF_USE_CUSTOM_ENTITY_IDS = 'use_custom_entity_ids' CONF_SHOW_ALL_CONTROLS = 'show_all_controls' @@ -37,7 +36,6 @@ CONF_REMOVE_UNAVAILABLE_CLIENTS = 'remove_unavailable_clients' CONF_CLIENT_REMOVE_INTERVAL = 'client_remove_interval' PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ - vol.Optional(CONF_INCLUDE_NON_CLIENTS, default=False): cv.boolean, vol.Optional(CONF_USE_EPISODE_ART, default=False): cv.boolean, vol.Optional(CONF_USE_CUSTOM_ENTITY_IDS, default=False): cv.boolean, vol.Optional(CONF_SHOW_ALL_CONTROLS, default=False): cv.boolean, @@ -160,8 +158,7 @@ def setup_plexserver( if device.machineIdentifier not in plex_clients: new_client = PlexClient( - config, device, None, plex_sessions, update_devices, - update_sessions) + config, device, None, plex_sessions, update_devices) plex_clients[device.machineIdentifier] = new_client _LOGGER.debug("New device: %s", device.machineIdentifier) new_plex_clients.append(new_client) @@ -171,27 +168,38 @@ def setup_plexserver( plex_clients[device.machineIdentifier].refresh(device, None) # add devices with a session and no client (ex. PlexConnect Apple TV's) - if config.get(CONF_INCLUDE_NON_CLIENTS): - # To avoid errors when plex sessions created during iteration - sessions = list(plex_sessions.items()) - for machine_identifier, (session, player) in sessions: - if machine_identifier in available_client_ids: - # Avoid using session if already added as a device. - _LOGGER.debug("Skipping session, device exists: %s", - machine_identifier) - continue + try: + sessions = plexserver.sessions() + except plexapi.exceptions.BadRequest: + _LOGGER.exception("Error listing plex sessions") + return + except requests.exceptions.RequestException as ex: + _LOGGER.warning( + "Could not connect to plex server at http://%s (%s)", host, ex) + return - if (machine_identifier not in plex_clients - and machine_identifier is not None): - new_client = PlexClient( - config, player, session, plex_sessions, update_devices, - update_sessions) - plex_clients[machine_identifier] = new_client - _LOGGER.debug("New session: %s", machine_identifier) - new_plex_clients.append(new_client) - else: - _LOGGER.debug("Refreshing session: %s", machine_identifier) - plex_clients[machine_identifier].refresh(None, session) + plex_sessions.clear() + for session in sessions: + for player in session.players: + plex_sessions[player.machineIdentifier] = session, player + + for machine_identifier, (session, player) in plex_sessions.items(): + if machine_identifier in available_client_ids: + # Avoid using session if already added as a device. + _LOGGER.debug("Skipping session, device exists: %s", + machine_identifier) + continue + + if (machine_identifier not in plex_clients + and machine_identifier is not None): + new_client = PlexClient( + config, player, session, plex_sessions, update_devices) + plex_clients[machine_identifier] = new_client + _LOGGER.debug("New session: %s", machine_identifier) + new_plex_clients.append(new_client) + else: + _LOGGER.debug("Refreshing session: %s", machine_identifier) + plex_clients[machine_identifier].refresh(None, session) clients_to_remove = [] for client in plex_clients.values(): @@ -219,25 +227,6 @@ def setup_plexserver( if new_plex_clients: add_entities_callback(new_plex_clients) - @util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS) - def update_sessions(): - """Update the sessions objects.""" - try: - sessions = plexserver.sessions() - except plexapi.exceptions.BadRequest: - _LOGGER.exception("Error listing plex sessions") - return - except requests.exceptions.RequestException as ex: - _LOGGER.warning( - "Could not connect to plex server at http://%s (%s)", host, ex) - return - - plex_sessions.clear() - for session in sessions: - for player in session.players: - plex_sessions[player.machineIdentifier] = session, player - - update_sessions() update_devices() @@ -285,7 +274,7 @@ class PlexClient(MediaPlayerDevice): """Representation of a Plex device.""" def __init__(self, config, device, session, plex_sessions, - update_devices, update_sessions): + update_devices): """Initialize the Plex device.""" self._app_name = '' self._device = None @@ -309,7 +298,6 @@ class PlexClient(MediaPlayerDevice): self.config = config self.plex_sessions = plex_sessions self.update_devices = update_devices - self.update_sessions = update_sessions # General self._media_content_id = None self._media_content_rating = None @@ -575,7 +563,6 @@ class PlexClient(MediaPlayerDevice): def update(self): """Get the latest details.""" self.update_devices(no_throttle=True) - self.update_sessions(no_throttle=True) @property def _active_media_plexapi_type(self):