mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Accept new Plex websocket callback payloads (#40773)
This commit is contained in:
parent
6dc25ccc7b
commit
be9ff3bd66
@ -5,7 +5,14 @@ import json
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
import plexapi.exceptions
|
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 requests.exceptions
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -14,7 +21,7 @@ from homeassistant.components.media_player.const import (
|
|||||||
ATTR_MEDIA_CONTENT_ID,
|
ATTR_MEDIA_CONTENT_ID,
|
||||||
ATTR_MEDIA_CONTENT_TYPE,
|
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 (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
CONF_SOURCE,
|
CONF_SOURCE,
|
||||||
@ -95,11 +102,12 @@ async def async_setup_entry(hass, entry):
|
|||||||
entry, data={**entry.data, PLEX_SERVER_CONFIG: new_server_data}
|
entry, data={**entry.data, PLEX_SERVER_CONFIG: new_server_data}
|
||||||
)
|
)
|
||||||
except requests.exceptions.ConnectionError as error:
|
except requests.exceptions.ConnectionError as error:
|
||||||
_LOGGER.error(
|
if entry.state != ENTRY_STATE_SETUP_RETRY:
|
||||||
"Plex server (%s) could not be reached: [%s]",
|
_LOGGER.error(
|
||||||
server_config[CONF_URL],
|
"Plex server (%s) could not be reached: [%s]",
|
||||||
error,
|
server_config[CONF_URL],
|
||||||
)
|
error,
|
||||||
|
)
|
||||||
raise ConfigEntryNotReady from error
|
raise ConfigEntryNotReady from error
|
||||||
except plexapi.exceptions.Unauthorized:
|
except plexapi.exceptions.Unauthorized:
|
||||||
hass.async_create_task(
|
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].setdefault(server_id, [])
|
||||||
hass.data[PLEX_DOMAIN][DISPATCHERS][server_id].append(unsub)
|
hass.data[PLEX_DOMAIN][DISPATCHERS][server_id].append(unsub)
|
||||||
|
|
||||||
def update_plex():
|
def plex_websocket_callback(signal, data, error):
|
||||||
async_dispatcher_send(hass, PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id))
|
"""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)
|
session = async_get_clientsession(hass)
|
||||||
verify_ssl = server_config.get(CONF_VERIFY_SSL)
|
verify_ssl = server_config.get(CONF_VERIFY_SSL)
|
||||||
websocket = PlexWebsocket(
|
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
|
hass.data[PLEX_DOMAIN][WEBSOCKETS][server_id] = websocket
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
"requirements": [
|
"requirements": [
|
||||||
"plexapi==4.1.1",
|
"plexapi==4.1.1",
|
||||||
"plexauth==0.0.5",
|
"plexauth==0.0.5",
|
||||||
"plexwebsocket==0.0.11"
|
"plexwebsocket==0.0.12"
|
||||||
],
|
],
|
||||||
"dependencies": ["http"],
|
"dependencies": ["http"],
|
||||||
"after_dependencies": ["sonos"],
|
"after_dependencies": ["sonos"],
|
||||||
|
@ -1116,7 +1116,7 @@ plexapi==4.1.1
|
|||||||
plexauth==0.0.5
|
plexauth==0.0.5
|
||||||
|
|
||||||
# homeassistant.components.plex
|
# homeassistant.components.plex
|
||||||
plexwebsocket==0.0.11
|
plexwebsocket==0.0.12
|
||||||
|
|
||||||
# homeassistant.components.plum_lightpad
|
# homeassistant.components.plum_lightpad
|
||||||
plumlightpad==0.0.11
|
plumlightpad==0.0.11
|
||||||
|
@ -533,7 +533,7 @@ plexapi==4.1.1
|
|||||||
plexauth==0.0.5
|
plexauth==0.0.5
|
||||||
|
|
||||||
# homeassistant.components.plex
|
# homeassistant.components.plex
|
||||||
plexwebsocket==0.0.11
|
plexwebsocket==0.0.12
|
||||||
|
|
||||||
# homeassistant.components.plum_lightpad
|
# homeassistant.components.plum_lightpad
|
||||||
plumlightpad==0.0.11
|
plumlightpad==0.0.11
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
"""Helper methods for Plex tests."""
|
"""Helper methods for Plex tests."""
|
||||||
|
from plexwebsocket import SIGNAL_DATA
|
||||||
|
|
||||||
|
|
||||||
def trigger_plex_update(mock_websocket):
|
def trigger_plex_update(mock_websocket):
|
||||||
"""Call the websocket callback method."""
|
"""Call the websocket callback method."""
|
||||||
callback = mock_websocket.call_args[0][1]
|
callback = mock_websocket.call_args[0][1]
|
||||||
callback()
|
callback(SIGNAL_DATA, None, None)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user