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:
jjlawrenandMartin Hjelmare authored and Martin Hjelmare committed 2019-09-05 19:50:26 +02:00
1 parent a000125729
commit 2cd845fb25
5 files changed
+136 -88

No files matched your search

+33 -26
View File
@@ -2,8 +2,7 @@
from datetime import timedelta
import json
import logging
import requests
import requests.exceptions
import voluptuous as vol
from homeassistant.components.media_player import MediaPlayerDevice, PLATFORM_SCHEMA
@@ -21,6 +20,9 @@ from homeassistant.components.media_player.const import (
SUPPORT_VOLUME_SET,
)
from homeassistant.const import (
CONF_URL,
CONF_TOKEN,
CONF_VERIFY_SSL,
DEVICE_DEFAULT_NAME,
STATE_IDLE,
STATE_OFF,
@@ -32,18 +34,22 @@ from homeassistant.helpers.event import track_time_interval
from homeassistant.util import dt as dt_util
from homeassistant.util.json import load_json, save_json
from .const import (
CONF_USE_EPISODE_ART,
CONF_SHOW_ALL_CONTROLS,
CONF_REMOVE_UNAVAILABLE_CLIENTS,
CONF_CLIENT_REMOVE_INTERVAL,
DOMAIN as PLEX_DOMAIN,
NAME_FORMAT,
PLEX_CONFIG_FILE,
)
from .server import PlexServer
SERVER_SETUP = "server_setup"
_CONFIGURING = {}
_LOGGER = logging.getLogger(__name__)
NAME_FORMAT = "Plex {}"
PLEX_CONFIG_FILE = "plex.conf"
PLEX_DATA = "plex"
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"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_USE_EPISODE_ART, default=False): cv.boolean,
@@ -58,8 +64,10 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
def setup_platform(hass, config, add_entities_callback, discovery_info=None):
"""Set up the Plex platform."""
if PLEX_DATA not in hass.data:
hass.data[PLEX_DATA] = {}
plex_data = hass.data.setdefault(PLEX_DOMAIN, {})
server_setup = plex_data.setdefault(SERVER_SETUP, False)
if server_setup:
return
# get config from plex.conf
file_config = load_json(hass.config.path(PLEX_CONFIG_FILE))
@@ -102,20 +110,19 @@ def setup_plexserver(
host, token, has_ssl, verify_ssl, hass, config, add_entities_callback
):
"""Set up a plexserver based on host parameter."""
import plexapi.server
import plexapi.exceptions
cert_session = None
http_prefix = "https" if has_ssl else "http"
if has_ssl and (verify_ssl is False):
_LOGGER.info("Ignoring SSL verification")
cert_session = requests.Session()
cert_session.verify = False
server_config = {
CONF_URL: f"{http_prefix}://{host}",
CONF_TOKEN: token,
CONF_VERIFY_SSL: verify_ssl,
}
try:
plexserver = plexapi.server.PlexServer(
f"{http_prefix}://{host}", token, cert_session
)
_LOGGER.info("Discovery configuration done (no token needed)")
plexserver = PlexServer(server_config)
plexserver.connect()
except (
plexapi.exceptions.BadRequest,
plexapi.exceptions.Unauthorized,
@@ -125,6 +132,8 @@ def setup_plexserver(
# No token or wrong token
request_configuration(host, hass, config, add_entities_callback)
return
else:
hass.data[PLEX_DOMAIN][SERVER_SETUP] = True
# If we came here and configuring this host, mark as done
if host in _CONFIGURING:
@@ -139,9 +148,7 @@ def setup_plexserver(
{host: {"token": token, "ssl": has_ssl, "verify": verify_ssl}},
)
_LOGGER.info("Connected to: %s://%s", http_prefix, host)
plex_clients = hass.data[PLEX_DATA]
plex_clients = {}
plex_sessions = {}
track_time_interval(hass, lambda now: update_devices(), timedelta(seconds=10))