mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Standardize Plex server connections (#26444)
* Common connection class * Omit tests for new Plex files * Oops * Add missing properties * Remove redundant log message * Stopgap to avoid duplicate setups * Cleaner check for server setup Co-Authored-By: Martin Hjelmare <marhje52@kth.se> * Cleaner check for server setup Co-Authored-By: Martin Hjelmare <marhje52@kth.se> * Not needed with previous setup check * Remove username/password support * Reduce log level Co-Authored-By: Martin Hjelmare <marhje52@kth.se> * Don't do setup in __init__ * Oops * Committing too fast... * Connect after init * Catch update exceptions like media_player * Pass in validated PlexServer instance * Remove unnecessary check * Counter should be unknown on init * Remove servername config option
This commit is contained in:
committed by
Martin Hjelmare
parent
a000125729
commit
2cd845fb25
59
homeassistant/components/plex/server.py
Normal file
59
homeassistant/components/plex/server.py
Normal file
@@ -0,0 +1,59 @@
|
||||
"""Shared class to maintain Plex server instances."""
|
||||
import logging
|
||||
import plexapi.server
|
||||
from requests import Session
|
||||
|
||||
from homeassistant.const import CONF_TOKEN, CONF_URL, CONF_VERIFY_SSL
|
||||
|
||||
from .const import DEFAULT_VERIFY_SSL
|
||||
|
||||
_LOGGER = logging.getLogger(__package__)
|
||||
|
||||
|
||||
class PlexServer:
|
||||
"""Manages a single Plex server connection."""
|
||||
|
||||
def __init__(self, server_config):
|
||||
"""Initialize a Plex server instance."""
|
||||
self._plex_server = None
|
||||
self._url = server_config.get(CONF_URL)
|
||||
self._token = server_config.get(CONF_TOKEN)
|
||||
self._verify_ssl = server_config.get(CONF_VERIFY_SSL, DEFAULT_VERIFY_SSL)
|
||||
|
||||
def connect(self):
|
||||
"""Connect to a Plex server directly, obtaining direct URL if necessary."""
|
||||
|
||||
def _connect_with_url():
|
||||
session = None
|
||||
if self._url.startswith("https") and not self._verify_ssl:
|
||||
session = Session()
|
||||
session.verify = False
|
||||
self._plex_server = plexapi.server.PlexServer(
|
||||
self._url, self._token, session
|
||||
)
|
||||
_LOGGER.debug("Connected to: %s (%s)", self.friendly_name, self.url_in_use)
|
||||
|
||||
_connect_with_url()
|
||||
|
||||
def clients(self):
|
||||
"""Pass through clients call to plexapi."""
|
||||
return self._plex_server.clients()
|
||||
|
||||
def sessions(self):
|
||||
"""Pass through sessions call to plexapi."""
|
||||
return self._plex_server.sessions()
|
||||
|
||||
@property
|
||||
def friendly_name(self):
|
||||
"""Return name of connected Plex server."""
|
||||
return self._plex_server.friendlyName
|
||||
|
||||
@property
|
||||
def machine_identifier(self):
|
||||
"""Return unique identifier of connected Plex server."""
|
||||
return self._plex_server.machineIdentifier
|
||||
|
||||
@property
|
||||
def url_in_use(self):
|
||||
"""Return URL used for connected Plex server."""
|
||||
return self._plex_server._baseurl # pylint: disable=W0212
|
||||
Reference in New Issue
Block a user