Discover controllable Plex clients using plex.tv (#36857)
This commit is contained in:
1 parent
94c8d74a66
commit
d5cc3208af
7 files changed
+193
-34
No files matched your search
@@ -1,6 +1,7 @@
|
||||
"""Shared class to maintain Plex server instances."""
|
||||
import logging
|
||||
import ssl
|
||||
import time
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from plexapi.exceptions import NotFound, Unauthorized
|
||||
@@ -34,6 +35,7 @@ from .const import (
|
||||
PLEX_NEW_MP_SIGNAL,
|
||||
PLEX_UPDATE_MEDIA_PLAYER_SIGNAL,
|
||||
PLEX_UPDATE_SENSOR_SIGNAL,
|
||||
PLEXTV_THROTTLE,
|
||||
X_PLEX_DEVICE_NAME,
|
||||
X_PLEX_PLATFORM,
|
||||
X_PLEX_PRODUCT,
|
||||
@@ -70,6 +72,9 @@ class PlexServer:
|
||||
self.server_choice = None
|
||||
self._accounts = []
|
||||
self._owner_username = None
|
||||
self._plextv_clients = None
|
||||
self._plextv_client_timestamp = 0
|
||||
self._plextv_device_cache = {}
|
||||
self._version = None
|
||||
self.async_update_platforms = Debouncer(
|
||||
hass,
|
||||
@@ -92,15 +97,28 @@ class PlexServer:
|
||||
self._plex_account = plexapi.myplex.MyPlexAccount(token=self._token)
|
||||
return self._plex_account
|
||||
|
||||
def plextv_clients(self):
|
||||
"""Return available clients linked to Plex account."""
|
||||
now = time.time()
|
||||
if now - self._plextv_client_timestamp > PLEXTV_THROTTLE:
|
||||
self._plextv_client_timestamp = now
|
||||
resources = self.account.resources()
|
||||
self._plextv_clients = [
|
||||
x for x in resources if "player" in x.provides and x.presence
|
||||
]
|
||||
_LOGGER.debug(
|
||||
"Current available clients from plex.tv: %s", self._plextv_clients
|
||||
)
|
||||
return self._plextv_clients
|
||||
|
||||
def connect(self):
|
||||
"""Connect to a Plex server directly, obtaining direct URL if necessary."""
|
||||
config_entry_update_needed = False
|
||||
|
||||
def _connect_with_token():
|
||||
account = plexapi.myplex.MyPlexAccount(token=self._token)
|
||||
available_servers = [
|
||||
(x.name, x.clientIdentifier)
|
||||
for x in account.resources()
|
||||
for x in self.account.resources()
|
||||
if "server" in x.provides
|
||||
]
|
||||
|
||||
@@ -112,7 +130,9 @@ class PlexServer:
|
||||
self.server_choice = (
|
||||
self._server_name if self._server_name else available_servers[0][0]
|
||||
)
|
||||
self._plex_server = account.resource(self.server_choice).connect(timeout=10)
|
||||
self._plex_server = self.account.resource(self.server_choice).connect(
|
||||
timeout=10
|
||||
)
|
||||
|
||||
def _connect_with_url():
|
||||
session = None
|
||||
@@ -124,13 +144,14 @@ class PlexServer:
|
||||
)
|
||||
|
||||
def _update_plexdirect_hostname():
|
||||
account = plexapi.myplex.MyPlexAccount(token=self._token)
|
||||
matching_server = [
|
||||
x.name
|
||||
for x in account.resources()
|
||||
for x in self.account.resources()
|
||||
if x.clientIdentifier == self._server_id
|
||||
][0]
|
||||
self._plex_server = account.resource(matching_server).connect(timeout=10)
|
||||
self._plex_server = self.account.resource(matching_server).connect(
|
||||
timeout=10
|
||||
)
|
||||
|
||||
if self._url:
|
||||
try:
|
||||
@@ -193,7 +214,11 @@ class PlexServer:
|
||||
|
||||
def _fetch_platform_data(self):
|
||||
"""Fetch all data from the Plex server in a single method."""
|
||||
return (self._plex_server.clients(), self._plex_server.sessions())
|
||||
return (
|
||||
self._plex_server.clients(),
|
||||
self._plex_server.sessions(),
|
||||
self.plextv_clients(),
|
||||
)
|
||||
|
||||
async def _async_update_platforms(self):
|
||||
"""Update the platform entities."""
|
||||
@@ -217,7 +242,7 @@ class PlexServer:
|
||||
monitored_users.add(new_user)
|
||||
|
||||
try:
|
||||
devices, sessions = await self.hass.async_add_executor_job(
|
||||
devices, sessions, plextv_clients = await self.hass.async_add_executor_job(
|
||||
self._fetch_platform_data
|
||||
)
|
||||
except (
|
||||
@@ -245,10 +270,8 @@ class PlexServer:
|
||||
)
|
||||
return
|
||||
|
||||
if (
|
||||
device.machineIdentifier not in self._created_clients
|
||||
and device.machineIdentifier not in ignored_clients
|
||||
and device.machineIdentifier not in new_clients
|
||||
if device.machineIdentifier not in (
|
||||
self._created_clients | ignored_clients | new_clients
|
||||
):
|
||||
new_clients.add(device.machineIdentifier)
|
||||
_LOGGER.debug(
|
||||
@@ -258,6 +281,30 @@ class PlexServer:
|
||||
for device in devices:
|
||||
process_device("device", device)
|
||||
|
||||
def connect_to_resource(resource):
|
||||
"""Connect to a plex.tv resource and return a Plex client."""
|
||||
client_id = resource.clientIdentifier
|
||||
if client_id in self._plextv_device_cache:
|
||||
return self._plextv_device_cache[client_id]
|
||||
|
||||
client = None
|
||||
try:
|
||||
client = resource.connect(timeout=3)
|
||||
_LOGGER.debug("plex.tv resource connection successful: %s", client)
|
||||
except NotFound:
|
||||
_LOGGER.error("plex.tv resource connection failed: %s", resource.name)
|
||||
|
||||
self._plextv_device_cache[client_id] = client
|
||||
return client
|
||||
|
||||
for plextv_client in plextv_clients:
|
||||
if plextv_client.clientIdentifier not in available_clients:
|
||||
device = await self.hass.async_add_executor_job(
|
||||
connect_to_resource, plextv_client
|
||||
)
|
||||
if device:
|
||||
process_device("resource", device)
|
||||
|
||||
for session in sessions:
|
||||
if session.TYPE == "photo":
|
||||
_LOGGER.debug("Photo session detected, skipping: %s", session)
|
||||
@@ -296,6 +343,7 @@ class PlexServer:
|
||||
for client_id in idle_clients:
|
||||
self.async_refresh_entity(client_id, None, None)
|
||||
self._known_idle.add(client_id)
|
||||
self._plextv_device_cache.pop(client_id, None)
|
||||
|
||||
if new_entity_configs:
|
||||
async_dispatcher_send(
|
||||
@@ -390,7 +438,7 @@ class PlexServer:
|
||||
key = kwargs["plex_key"]
|
||||
try:
|
||||
return self.fetch_item(key)
|
||||
except plexapi.exceptions.NotFound:
|
||||
except NotFound:
|
||||
_LOGGER.error("Media for key %s not found", key)
|
||||
return None
|
||||
|
||||
|
||||
Reference in new issue
Block a user