Support for Plex servers with enforced SSL (#8341)

* Support for Plex servers with enforced SSL

* Fixed HoundCI warnings

* Fixed HoundCI warnings (2nd)

* Configurator data validation

* Travis linting
This commit is contained in:
Niccolò Maggioni 2017-07-12 08:26:29 +02:00 committed by Paulus Schoutsen
parent 9704057959
commit 229000b834

View File

@ -82,8 +82,17 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
if file_config:
# Setup a configured PlexServer
host, token = file_config.popitem()
token = token['token']
host, host_config = file_config.popitem()
token = host_config['token']
try:
has_ssl = host_config['ssl']
except KeyError:
has_ssl = False
try:
verify_ssl = host_config['verify']
except KeyError:
verify_ssl = True
# Via discovery
elif discovery_info is not None:
# Parse discovery data
@ -95,19 +104,34 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
if host in _CONFIGURING:
return
token = None
has_ssl = False
verify_ssl = True
else:
return
setup_plexserver(host, token, hass, config, add_devices_callback)
setup_plexserver(
host, token, has_ssl, verify_ssl,
hass, config, add_devices_callback
)
def setup_plexserver(host, token, hass, config, add_devices_callback):
def setup_plexserver(
host, token, has_ssl, verify_ssl, hass, config, add_devices_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
try:
plexserver = plexapi.server.PlexServer('http://%s' % host, token)
plexserver = plexapi.server.PlexServer(
'%s://%s' % (http_prefix, host),
token, cert_session
)
_LOGGER.info("Discovery configuration done (no token needed)")
except (plexapi.exceptions.BadRequest, plexapi.exceptions.Unauthorized,
plexapi.exceptions.NotFound) as error:
@ -126,11 +150,13 @@ def setup_plexserver(host, token, hass, config, add_devices_callback):
# Save config
if not config_from_file(
hass.config.path(PLEX_CONFIG_FILE), {host: {
'token': token
'token': token,
'ssl': has_ssl,
'verify': verify_ssl,
}}):
_LOGGER.error("Failed to save configuration file")
_LOGGER.info('Connected to: http://%s', host)
_LOGGER.info('Connected to: %s://%s', http_prefix, host)
plex_clients = {}
plex_sessions = {}
@ -217,7 +243,11 @@ def request_configuration(host, hass, config, add_devices_callback):
def plex_configuration_callback(data):
"""Handle configuration changes."""
setup_plexserver(
host, data.get('token'), hass, config, add_devices_callback)
host, data.get('token'),
cv.boolean(data.get('has_ssl')),
cv.boolean(data.get('do_not_verify')),
hass, config, add_devices_callback
)
_CONFIGURING[host] = configurator.request_config(
hass,
@ -230,6 +260,14 @@ def request_configuration(host, hass, config, add_devices_callback):
'id': 'token',
'name': 'X-Plex-Token',
'type': ''
}, {
'id': 'has_ssl',
'name': 'Use SSL',
'type': ''
}, {
'id': 'do_not_verify_ssl',
'name': 'Do not verify SSL',
'type': ''
}])