Update Plex via websockets (#28158)
* Save client identifier from auth for future use * Use websocket events to update Plex * Handle websocket disconnections * Use aiohttp, shut down socket cleanly * Bad rebase fix * Don't connect websocket during config_flow validation, fix tests * Move websocket handling to external library * Close websocket session on HA stop * Use external library, revert unnecessary test change * Async & lint fixes * Clean up websocket stopper on entry unload * Setup websocket in component, pass actual needed object to library
This commit is contained in:
1 parent
9153729b21
commit
43c7b57d1e
7 files changed
+52
-13
No files matched your search
@@ -1,9 +1,9 @@
|
||||
"""Support to embed Plex."""
|
||||
import asyncio
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
import plexapi.exceptions
|
||||
from plexwebsocket import PlexWebsocket
|
||||
import requests.exceptions
|
||||
import voluptuous as vol
|
||||
|
||||
@@ -16,9 +16,14 @@ from homeassistant.const import (
|
||||
CONF_TOKEN,
|
||||
CONF_URL,
|
||||
CONF_VERIFY_SSL,
|
||||
EVENT_HOMEASSISTANT_STOP,
|
||||
)
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.event import async_track_time_interval
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.dispatcher import (
|
||||
async_dispatcher_connect,
|
||||
async_dispatcher_send,
|
||||
)
|
||||
|
||||
from .const import (
|
||||
CONF_USE_EPISODE_ART,
|
||||
@@ -33,8 +38,9 @@ from .const import (
|
||||
PLATFORMS,
|
||||
PLEX_MEDIA_PLAYER_OPTIONS,
|
||||
PLEX_SERVER_CONFIG,
|
||||
REFRESH_LISTENERS,
|
||||
PLEX_UPDATE_PLATFORMS_SIGNAL,
|
||||
SERVERS,
|
||||
WEBSOCKETS,
|
||||
)
|
||||
from .server import PlexServer
|
||||
|
||||
@@ -67,9 +73,7 @@ _LOGGER = logging.getLogger(__package__)
|
||||
|
||||
def setup(hass, config):
|
||||
"""Set up the Plex component."""
|
||||
hass.data.setdefault(
|
||||
PLEX_DOMAIN, {SERVERS: {}, REFRESH_LISTENERS: {}, DISPATCHERS: {}}
|
||||
)
|
||||
hass.data.setdefault(PLEX_DOMAIN, {SERVERS: {}, DISPATCHERS: {}, WEBSOCKETS: {}})
|
||||
|
||||
plex_config = config.get(PLEX_DOMAIN, {})
|
||||
if plex_config:
|
||||
@@ -136,7 +140,6 @@ async def async_setup_entry(hass, entry):
|
||||
)
|
||||
server_id = plex_server.machine_identifier
|
||||
hass.data[PLEX_DOMAIN][SERVERS][server_id] = plex_server
|
||||
hass.data[PLEX_DOMAIN][DISPATCHERS][server_id] = []
|
||||
|
||||
for platform in PLATFORMS:
|
||||
hass.async_create_task(
|
||||
@@ -145,9 +148,29 @@ async def async_setup_entry(hass, entry):
|
||||
|
||||
entry.add_update_listener(async_options_updated)
|
||||
|
||||
hass.data[PLEX_DOMAIN][REFRESH_LISTENERS][server_id] = async_track_time_interval(
|
||||
hass, lambda now: plex_server.update_platforms(), timedelta(seconds=10)
|
||||
unsub = async_dispatcher_connect(
|
||||
hass,
|
||||
PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id),
|
||||
plex_server.update_platforms,
|
||||
)
|
||||
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))
|
||||
|
||||
session = async_get_clientsession(hass)
|
||||
websocket = PlexWebsocket(plex_server.plex_server, update_plex, session)
|
||||
hass.loop.create_task(websocket.listen())
|
||||
hass.data[PLEX_DOMAIN][WEBSOCKETS][server_id] = websocket
|
||||
|
||||
def close_websocket_session(_):
|
||||
websocket.close()
|
||||
|
||||
unsub = hass.bus.async_listen_once(
|
||||
EVENT_HOMEASSISTANT_STOP, close_websocket_session
|
||||
)
|
||||
hass.data[PLEX_DOMAIN][DISPATCHERS][server_id].append(unsub)
|
||||
|
||||
return True
|
||||
|
||||
@@ -156,8 +179,8 @@ async def async_unload_entry(hass, entry):
|
||||
"""Unload a config entry."""
|
||||
server_id = entry.data[CONF_SERVER_IDENTIFIER]
|
||||
|
||||
cancel = hass.data[PLEX_DOMAIN][REFRESH_LISTENERS].pop(server_id)
|
||||
cancel()
|
||||
websocket = hass.data[PLEX_DOMAIN][WEBSOCKETS].pop(server_id)
|
||||
websocket.close()
|
||||
|
||||
dispatchers = hass.data[PLEX_DOMAIN][DISPATCHERS].pop(server_id)
|
||||
for unsub in dispatchers:
|
||||
|
||||
Reference in new issue
Block a user