Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 42 additions & 10 deletions homeassistant/components/plex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
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

Expand All @@ -14,7 +21,7 @@
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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Comment thread
jjlawren marked this conversation as resolved.
_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(
Comment thread
jjlawren marked this conversation as resolved.
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))
Comment thread
jjlawren marked this conversation as resolved.

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

Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/plex/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion tests/components/plex/helpers.py
Original file line number Diff line number Diff line change
@@ -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)