From a62bd97fa597034c91222dbe4f75f7bae936659c Mon Sep 17 00:00:00 2001 From: jjlawren Date: Thu, 21 Nov 2019 14:39:24 -0600 Subject: [PATCH] Delay Plex websocket connection to avoid race (#28934) --- homeassistant/components/plex/__init__.py | 7 ++++++- homeassistant/components/plex/media_player.py | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/plex/__init__.py b/homeassistant/components/plex/__init__.py index 4a575722826..386be56340a 100644 --- a/homeassistant/components/plex/__init__.py +++ b/homeassistant/components/plex/__init__.py @@ -16,6 +16,7 @@ from homeassistant.const import ( CONF_TOKEN, CONF_URL, CONF_VERIFY_SSL, + EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, ) from homeassistant.helpers import config_validation as cv @@ -164,12 +165,16 @@ async def async_setup_entry(hass, entry): websocket = PlexWebsocket( plex_server.plex_server, update_plex, session=session, verify_ssl=verify_ssl ) - hass.loop.create_task(websocket.listen()) hass.data[PLEX_DOMAIN][WEBSOCKETS][server_id] = websocket + async def async_start_websocket_session(_): + await websocket.listen() + def close_websocket_session(_): websocket.close() + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, async_start_websocket_session) + unsub = hass.bus.async_listen_once( EVENT_HOMEASSISTANT_STOP, close_websocket_session ) diff --git a/homeassistant/components/plex/media_player.py b/homeassistant/components/plex/media_player.py index d6720fd9e95..ad5fb2f73f1 100644 --- a/homeassistant/components/plex/media_player.py +++ b/homeassistant/components/plex/media_player.py @@ -68,6 +68,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): hass, PLEX_NEW_MP_SIGNAL.format(server_id), async_new_media_players ) hass.data[PLEX_DOMAIN][DISPATCHERS][server_id].append(unsub) + _LOGGER.debug("New entity listener created") @callback