Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ homeassistant/components/philips_js/* @elupus
homeassistant/components/pi_hole/* @fabaff
homeassistant/components/plaato/* @JohNan
homeassistant/components/plant/* @ChristianKuehnel
homeassistant/components/plex/* @jjlawren
homeassistant/components/plugwise/* @laetificat @CoMPaTech
homeassistant/components/point/* @fredrike
homeassistant/components/ps4/* @ktnrg45
Expand Down
37 changes: 22 additions & 15 deletions homeassistant/components/plex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
from homeassistant.util.json import load_json, save_json

from .const import (
CONF_SERVER,
CONF_USE_EPISODE_ART,
CONF_SHOW_ALL_CONTROLS,
DEFAULT_HOST,
DEFAULT_PORT,
DEFAULT_SSL,
DEFAULT_VERIFY_SSL,
Expand All @@ -42,14 +42,18 @@
)

SERVER_CONFIG_SCHEMA = vol.Schema(
{
vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
vol.Optional(CONF_TOKEN): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
vol.Optional(MP_DOMAIN, default={}): MEDIA_PLAYER_SCHEMA,
}
vol.All(
{
vol.Optional(CONF_HOST): cv.string,
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_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
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)
Expand All @@ -73,12 +77,14 @@ def setup_plex(config=None, discovery_info=None, configurator_info=None):
"""Return assembled server_config dict."""
json_file = hass.config.path(PLEX_CONFIG_FILE)
file_config = load_json(json_file)
host_and_port = None

if config:
server_config = config
host_and_port = (
f"{server_config.pop(CONF_HOST)}:{server_config.pop(CONF_PORT)}"
)
if CONF_HOST in server_config:
host_and_port = (
f"{server_config.pop(CONF_HOST)}:{server_config.pop(CONF_PORT)}"
)
if MP_DOMAIN in server_config:
hass.data[PLEX_MEDIA_PLAYER_OPTIONS] = server_config.pop(MP_DOMAIN)
elif file_config:
Expand All @@ -95,9 +101,10 @@ def setup_plex(config=None, discovery_info=None, configurator_info=None):
discovery.listen(hass, SERVICE_PLEX, server_discovered)
return True

use_ssl = server_config.get(CONF_SSL, DEFAULT_SSL)
http_prefix = "https" if use_ssl else "http"
server_config[CONF_URL] = f"{http_prefix}://{host_and_port}"
if host_and_port:
use_ssl = server_config.get(CONF_SSL, DEFAULT_SSL)
http_prefix = "https" if use_ssl else "http"
server_config[CONF_URL] = f"{http_prefix}://{host_and_port}"

plex_server = PlexServer(server_config)
try:
Expand Down
4 changes: 1 addition & 3 deletions homeassistant/components/plex/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
DOMAIN = "plex"
NAME_FORMAT = "Plex {}"

DEFAULT_HOST = "localhost"
DEFAULT_PORT = 32400
DEFAULT_SSL = False
DEFAULT_VERIFY_SSL = True
Expand All @@ -14,7 +13,6 @@
PLEX_MEDIA_PLAYER_OPTIONS = "plex_mp_options"
PLEX_SERVER_CONFIG = "server_config"

CONF_SERVER = "server"
CONF_USE_EPISODE_ART = "use_episode_art"
CONF_SHOW_ALL_CONTROLS = "show_all_controls"
CONF_REMOVE_UNAVAILABLE_CLIENTS = "remove_unavailable_clients"
CONF_CLIENT_REMOVE_INTERVAL = "client_remove_interval"
4 changes: 3 additions & 1 deletion homeassistant/components/plex/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
"plexapi==3.0.6"
],
"dependencies": ["configurator"],
"codeowners": []
"codeowners": [
"@jjlawren"
]
}
20 changes: 19 additions & 1 deletion homeassistant/components/plex/server.py
Original file line number Diff line number Diff line change
@@ -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__)

Expand All @@ -19,11 +20,25 @@ def __init__(self, server_config):
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:
Expand All @@ -34,6 +49,9 @@ def _connect_with_url():
)
_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):
Expand Down