Accept new Plex websocket callback payloads (#40773)

This commit is contained in:
jjlawren 2020-10-01 02:57:45 -05:00 committed by Paulus Schoutsen
parent 0902caa7e4
commit c0d4e1eaf4
5 changed files with 47 additions and 14 deletions

View File

@ -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

View File

@ -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"],

View File

@ -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

View File

@ -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

View File

@ -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)