From ecaf0189cca74e15a12f1b5ffe496f76d3a814d2 Mon Sep 17 00:00:00 2001
From: Ryan McLean <ryanm101@users.noreply.github.com>
Date: Fri, 9 Mar 2018 16:50:21 +0000
Subject: [PATCH] Plex mark devices unavailable if they 'vanish' and clear
 media (#12811)

* Marks Devices unavailable if they 'vanish' and clears media

* Fixed PEP8 complaint

* Fixed Linting

* Lint Fix

* Fix redine of id

* More lint fixes

* Removed redundant loop for setting availability of client
Renamed '_is_device_available' to '_available'
Renamed 'available_ids' to 'available_client_ids'

* removed whitespace per houndCI
---
 homeassistant/components/media_player/plex.py | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/homeassistant/components/media_player/plex.py b/homeassistant/components/media_player/plex.py
index a63bf8525ed..caa81424377 100644
--- a/homeassistant/components/media_player/plex.py
+++ b/homeassistant/components/media_player/plex.py
@@ -154,11 +154,14 @@ def setup_plexserver(
             return
 
         new_plex_clients = []
+        available_client_ids = []
         for device in devices:
             # For now, let's allow all deviceClass types
             if device.deviceClass in ['badClient']:
                 continue
 
+            available_client_ids.append(device.machineIdentifier)
+
             if device.machineIdentifier not in plex_clients:
                 new_client = PlexClient(config, device, None,
                                         plex_sessions, update_devices,
@@ -186,6 +189,9 @@ def setup_plexserver(
             if client.session is None:
                 client.force_idle()
 
+            client.set_availability(client.machine_identifier
+                                    in available_client_ids)
+
         if new_plex_clients:
             add_devices_callback(new_plex_clients)
 
@@ -259,6 +265,7 @@ class PlexClient(MediaPlayerDevice):
         """Initialize the Plex device."""
         self._app_name = ''
         self._device = None
+        self._available = False
         self._device_protocol_capabilities = None
         self._is_player_active = False
         self._is_player_available = False
@@ -407,6 +414,12 @@ class PlexClient(MediaPlayerDevice):
 
         self._media_image_url = thumb_url
 
+    def set_availability(self, available):
+        """Set the device as available/unavailable noting time."""
+        if not available:
+            self._clear_media_details()
+        self._available = available
+
     def _set_player_state(self):
         if self._player_state == 'playing':
             self._is_player_active = True
@@ -468,6 +481,11 @@ class PlexClient(MediaPlayerDevice):
         """Return the id of this plex client."""
         return self.machine_identifier
 
+    @property
+    def available(self):
+        """Return the availability of the client."""
+        return self._available
+
     @property
     def name(self):
         """Return the name of the device."""
-- 
GitLab