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

@ -212,6 +212,7 @@ homeassistant/components/philips_js/* @elupus
homeassistant/components/pi_hole/* @fabaff homeassistant/components/pi_hole/* @fabaff
homeassistant/components/plaato/* @JohNan homeassistant/components/plaato/* @JohNan
homeassistant/components/plant/* @ChristianKuehnel homeassistant/components/plant/* @ChristianKuehnel
homeassistant/components/plex/* @jjlawren
homeassistant/components/plugwise/* @laetificat @CoMPaTech homeassistant/components/plugwise/* @laetificat @CoMPaTech
homeassistant/components/point/* @fredrike homeassistant/components/point/* @fredrike
homeassistant/components/ps4/* @ktnrg45 homeassistant/components/ps4/* @ktnrg45

View File

@ -20,9 +20,9 @@ from homeassistant.helpers import discovery
from homeassistant.util.json import load_json, save_json from homeassistant.util.json import load_json, save_json
from .const import ( from .const import (
CONF_SERVER,
CONF_USE_EPISODE_ART, CONF_USE_EPISODE_ART,
CONF_SHOW_ALL_CONTROLS, CONF_SHOW_ALL_CONTROLS,
DEFAULT_HOST,
DEFAULT_PORT, DEFAULT_PORT,
DEFAULT_SSL, DEFAULT_SSL,
DEFAULT_VERIFY_SSL, DEFAULT_VERIFY_SSL,
@ -42,14 +42,18 @@ MEDIA_PLAYER_SCHEMA = vol.Schema(
) )
SERVER_CONFIG_SCHEMA = vol.Schema( SERVER_CONFIG_SCHEMA = vol.Schema(
vol.All(
{ {
vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string, vol.Optional(CONF_HOST): cv.string,
vol.Optional(CONF_TOKEN): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port, vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_TOKEN): cv.string,
vol.Optional(CONF_SERVER): cv.string,
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean, vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean, vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
vol.Optional(MP_DOMAIN, default={}): MEDIA_PLAYER_SCHEMA, vol.Optional(MP_DOMAIN, default={}): MEDIA_PLAYER_SCHEMA,
} },
cv.has_at_least_one_key(CONF_HOST, CONF_TOKEN),
)
) )
CONFIG_SCHEMA = vol.Schema({PLEX_DOMAIN: SERVER_CONFIG_SCHEMA}, extra=vol.ALLOW_EXTRA) CONFIG_SCHEMA = vol.Schema({PLEX_DOMAIN: SERVER_CONFIG_SCHEMA}, extra=vol.ALLOW_EXTRA)
@ -73,9 +77,11 @@ def setup(hass, config):
"""Return assembled server_config dict.""" """Return assembled server_config dict."""
json_file = hass.config.path(PLEX_CONFIG_FILE) json_file = hass.config.path(PLEX_CONFIG_FILE)
file_config = load_json(json_file) file_config = load_json(json_file)
host_and_port = None
if config: if config:
server_config = config server_config = config
if CONF_HOST in server_config:
host_and_port = ( host_and_port = (
f"{server_config.pop(CONF_HOST)}:{server_config.pop(CONF_PORT)}" f"{server_config.pop(CONF_HOST)}:{server_config.pop(CONF_PORT)}"
) )
@ -95,6 +101,7 @@ def setup(hass, config):
discovery.listen(hass, SERVICE_PLEX, server_discovered) discovery.listen(hass, SERVICE_PLEX, server_discovered)
return True return True
if host_and_port:
use_ssl = server_config.get(CONF_SSL, DEFAULT_SSL) use_ssl = server_config.get(CONF_SSL, DEFAULT_SSL)
http_prefix = "https" if use_ssl else "http" http_prefix = "https" if use_ssl else "http"
server_config[CONF_URL] = f"{http_prefix}://{host_and_port}" server_config[CONF_URL] = f"{http_prefix}://{host_and_port}"

View File

@ -2,7 +2,6 @@
DOMAIN = "plex" DOMAIN = "plex"
NAME_FORMAT = "Plex {}" NAME_FORMAT = "Plex {}"
DEFAULT_HOST = "localhost"
DEFAULT_PORT = 32400 DEFAULT_PORT = 32400
DEFAULT_SSL = False DEFAULT_SSL = False
DEFAULT_VERIFY_SSL = True DEFAULT_VERIFY_SSL = True
@ -14,7 +13,6 @@ PLEX_CONFIG_FILE = "plex.conf"
PLEX_MEDIA_PLAYER_OPTIONS = "plex_mp_options" PLEX_MEDIA_PLAYER_OPTIONS = "plex_mp_options"
PLEX_SERVER_CONFIG = "server_config" PLEX_SERVER_CONFIG = "server_config"
CONF_SERVER = "server"
CONF_USE_EPISODE_ART = "use_episode_art" CONF_USE_EPISODE_ART = "use_episode_art"
CONF_SHOW_ALL_CONTROLS = "show_all_controls" CONF_SHOW_ALL_CONTROLS = "show_all_controls"
CONF_REMOVE_UNAVAILABLE_CLIENTS = "remove_unavailable_clients"
CONF_CLIENT_REMOVE_INTERVAL = "client_remove_interval"

View File

@ -6,5 +6,7 @@
"plexapi==3.0.6" "plexapi==3.0.6"
], ],
"dependencies": ["configurator"], "dependencies": ["configurator"],
"codeowners": [] "codeowners": [
"@jjlawren"
]
} }

View File

@ -1,12 +1,13 @@
"""Shared class to maintain Plex server instances.""" """Shared class to maintain Plex server instances."""
import logging import logging
import plexapi.myplex
import plexapi.server import plexapi.server
from requests import Session from requests import Session
from homeassistant.const import CONF_TOKEN, CONF_URL, CONF_VERIFY_SSL 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__) _LOGGER = logging.getLogger(__package__)
@ -19,11 +20,25 @@ class PlexServer:
self._plex_server = None self._plex_server = None
self._url = server_config.get(CONF_URL) self._url = server_config.get(CONF_URL)
self._token = server_config.get(CONF_TOKEN) 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) self._verify_ssl = server_config.get(CONF_VERIFY_SSL, DEFAULT_VERIFY_SSL)
def connect(self): def connect(self):
"""Connect to a Plex server directly, obtaining direct URL if necessary.""" """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(): def _connect_with_url():
session = None session = None
if self._url.startswith("https") and not self._verify_ssl: 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) _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() _connect_with_url()
def clients(self): def clients(self):