diff --git a/homeassistant/components/plex/__init__.py b/homeassistant/components/plex/__init__.py index fff6dea3bb4..95fb6e4f09f 100644 --- a/homeassistant/components/plex/__init__.py +++ b/homeassistant/components/plex/__init__.py @@ -5,7 +5,14 @@ import json import logging import plexapi.exceptions -from plexwebsocket import PlexWebsocket +from plexwebsocket import ( + SIGNAL_CONNECTION_STATE, + SIGNAL_DATA, + STATE_CONNECTED, + STATE_DISCONNECTED, + STATE_STOPPED, + PlexWebsocket, +) import requests.exceptions import voluptuous as vol @@ -14,7 +21,7 @@ from homeassistant.components.media_player.const import ( ATTR_MEDIA_CONTENT_ID, ATTR_MEDIA_CONTENT_TYPE, ) -from homeassistant.config_entries import SOURCE_REAUTH +from homeassistant.config_entries import ENTRY_STATE_SETUP_RETRY, SOURCE_REAUTH from homeassistant.const import ( ATTR_ENTITY_ID, CONF_SOURCE, @@ -95,11 +102,12 @@ async def async_setup_entry(hass, entry): entry, data={**entry.data, PLEX_SERVER_CONFIG: new_server_data} ) except requests.exceptions.ConnectionError as error: - _LOGGER.error( - "Plex server (%s) could not be reached: [%s]", - server_config[CONF_URL], - error, - ) + if entry.state != ENTRY_STATE_SETUP_RETRY: + _LOGGER.error( + "Plex server (%s) could not be reached: [%s]", + server_config[CONF_URL], + error, + ) raise ConfigEntryNotReady from error except plexapi.exceptions.Unauthorized: hass.async_create_task( @@ -142,13 +150,37 @@ async def async_setup_entry(hass, entry): hass.data[PLEX_DOMAIN][DISPATCHERS].setdefault(server_id, []) hass.data[PLEX_DOMAIN][DISPATCHERS][server_id].append(unsub) - def update_plex(): - async_dispatcher_send(hass, PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id)) + def plex_websocket_callback(signal, data, error): + """Handle callbacks from plexwebsocket library.""" + if signal == SIGNAL_CONNECTION_STATE: + + if data == STATE_CONNECTED: + _LOGGER.debug("Websocket to %s successful", entry.data[CONF_SERVER]) + elif data == STATE_DISCONNECTED: + _LOGGER.debug( + "Websocket to %s disconnected, retrying", entry.data[CONF_SERVER] + ) + # Stopped websockets without errors are expected during shutdown and ignored + elif data == STATE_STOPPED and error: + _LOGGER.error( + "Websocket to %s failed, aborting [Error: %s]", + entry.data[CONF_SERVER], + error, + ) + asyncio.run_coroutine_threadsafe( + hass.config_entries.async_reload(entry.entry_id), hass.loop + ) + + elif signal == SIGNAL_DATA: + async_dispatcher_send(hass, PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id)) session = async_get_clientsession(hass) verify_ssl = server_config.get(CONF_VERIFY_SSL) websocket = PlexWebsocket( - plex_server.plex_server, update_plex, session=session, verify_ssl=verify_ssl + plex_server.plex_server, + plex_websocket_callback, + session=session, + verify_ssl=verify_ssl, ) hass.data[PLEX_DOMAIN][WEBSOCKETS][server_id] = websocket diff --git a/homeassistant/components/plex/manifest.json b/homeassistant/components/plex/manifest.json index 29b0fe8038e..f5bbc6ac53c 100644 --- a/homeassistant/components/plex/manifest.json +++ b/homeassistant/components/plex/manifest.json @@ -6,7 +6,7 @@ "requirements": [ "plexapi==4.1.1", "plexauth==0.0.5", - "plexwebsocket==0.0.11" + "plexwebsocket==0.0.12" ], "dependencies": ["http"], "after_dependencies": ["sonos"], diff --git a/requirements_all.txt b/requirements_all.txt index 6b1087502c2..c47f8f0759b 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1116,7 +1116,7 @@ plexapi==4.1.1 plexauth==0.0.5 # homeassistant.components.plex -plexwebsocket==0.0.11 +plexwebsocket==0.0.12 # homeassistant.components.plum_lightpad plumlightpad==0.0.11 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 751fb61f179..49b3b9ea1f7 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -533,7 +533,7 @@ plexapi==4.1.1 plexauth==0.0.5 # homeassistant.components.plex -plexwebsocket==0.0.11 +plexwebsocket==0.0.12 # homeassistant.components.plum_lightpad plumlightpad==0.0.11 diff --git a/tests/components/plex/helpers.py b/tests/components/plex/helpers.py index 8055ab0d5b1..a20d70fbb7e 100644 --- a/tests/components/plex/helpers.py +++ b/tests/components/plex/helpers.py @@ -1,7 +1,8 @@ """Helper methods for Plex tests.""" +from plexwebsocket import SIGNAL_DATA def trigger_plex_update(mock_websocket): """Call the websocket callback method.""" callback = mock_websocket.call_args[0][1] - callback() + callback(SIGNAL_DATA, None, None)