1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Remove default host for Plex config (#26583)

* Remove default host, allow config with token(+server)

* Require one of host or token

* Oops

* Adjust schema

* Fix schema

* Add self as codeowner

* Update CODEOWNERS
This commit is contained in:
jjlawren
2019-09-11 13:21:08 -05:00
committed by Martin Hjelmare
parent 2b30f47f4b
commit 6eeb01edc4
5 changed files with 46 additions and 20 deletions

View File

@@ -1,12 +1,13 @@
"""Shared class to maintain Plex server instances."""
import logging
import plexapi.myplex
import plexapi.server
from requests import Session
from homeassistant.const import CONF_TOKEN, CONF_URL, CONF_VERIFY_SSL
from .const import DEFAULT_VERIFY_SSL
from .const import CONF_SERVER, DEFAULT_VERIFY_SSL
_LOGGER = logging.getLogger(__package__)
@@ -19,11 +20,25 @@ class PlexServer:
self._plex_server = None
self._url = server_config.get(CONF_URL)
self._token = server_config.get(CONF_TOKEN)
self._server_name = server_config.get(CONF_SERVER)
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 _set_missing_url():
account = plexapi.myplex.MyPlexAccount(token=self._token)
available_servers = [
x.name for x in account.resources() if "server" in x.provides
]
server_choice = (
self._server_name if self._server_name else available_servers[0]
)
connections = account.resource(server_choice).connections
local_url = [x.httpuri for x in connections if x.local]
remote_url = [x.uri for x in connections if not x.local]
self._url = local_url[0] if local_url else remote_url[0]
def _connect_with_url():
session = None
if self._url.startswith("https") and not self._verify_ssl:
@@ -34,6 +49,9 @@ class PlexServer:
)
_LOGGER.debug("Connected to: %s (%s)", self.friendly_name, self.url_in_use)
if self._token and not self._url:
_set_missing_url()
_connect_with_url()
def clients(self):