From 229000b8345878e3024c635ff44189ca5c2eab44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Maggioni?= Date: Wed, 12 Jul 2017 08:26:29 +0200 Subject: [PATCH] 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 --- homeassistant/components/media_player/plex.py | 54 ++++++++++++++++--- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/media_player/plex.py b/homeassistant/components/media_player/plex.py index 2bfa97e7e79..a36eb08f851 100644 --- a/homeassistant/components/media_player/plex.py +++ b/homeassistant/components/media_player/plex.py @@ -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': '' }])