From 8198970af99ce0377e7e74c74a288c771e0458d6 Mon Sep 17 00:00:00 2001 From: springstan <46536646+springstan@users.noreply.github.com> Date: Fri, 10 Apr 2020 22:01:57 +0200 Subject: [PATCH] Clean up ssl usage (#33960) * Clean up ssl usage * Revert config[CONF_SSL] from sonarr, radarr and mfi --- .../cisco_mobility_express/device_tracker.py | 2 +- homeassistant/components/emby/media_player.py | 2 +- homeassistant/components/enigma2/media_player.py | 2 +- homeassistant/components/epson/media_player.py | 2 +- .../components/hikvision/binary_sensor.py | 5 +---- homeassistant/components/homematic/__init__.py | 2 +- homeassistant/components/influxdb/sensor.py | 4 ++-- homeassistant/components/itunes/media_player.py | 2 +- homeassistant/components/mfi/switch.py | 2 +- homeassistant/components/octoprint/__init__.py | 4 ++-- homeassistant/components/opengarage/cover.py | 4 ++-- homeassistant/components/plex/__init__.py | 4 ++-- homeassistant/components/pyload/sensor.py | 4 ++-- homeassistant/components/qnap/sensor.py | 2 +- homeassistant/components/radarr/sensor.py | 12 ++++++------ homeassistant/components/sabnzbd/__init__.py | 2 +- homeassistant/components/sma/sensor.py | 3 ++- homeassistant/components/sonarr/sensor.py | 16 ++++++++-------- homeassistant/components/splunk/__init__.py | 2 +- homeassistant/components/tautulli/sensor.py | 2 +- homeassistant/components/venstar/climate.py | 7 ++----- homeassistant/components/zabbix/__init__.py | 7 ++----- homeassistant/components/zoneminder/__init__.py | 7 ++----- 23 files changed, 44 insertions(+), 55 deletions(-) diff --git a/homeassistant/components/cisco_mobility_express/device_tracker.py b/homeassistant/components/cisco_mobility_express/device_tracker.py index db504e3d19b..220228b6570 100644 --- a/homeassistant/components/cisco_mobility_express/device_tracker.py +++ b/homeassistant/components/cisco_mobility_express/device_tracker.py @@ -43,7 +43,7 @@ def get_scanner(hass, config): config[CONF_HOST], config[CONF_USERNAME], config[CONF_PASSWORD], - config.get(CONF_SSL), + config[CONF_SSL], config.get(CONF_VERIFY_SSL), ) if not controller.is_logged_in(): diff --git a/homeassistant/components/emby/media_player.py b/homeassistant/components/emby/media_player.py index 19fad984b27..0a14799ce24 100644 --- a/homeassistant/components/emby/media_player.py +++ b/homeassistant/components/emby/media_player.py @@ -71,7 +71,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= host = config.get(CONF_HOST) key = config.get(CONF_API_KEY) port = config.get(CONF_PORT) - ssl = config.get(CONF_SSL) + ssl = config[CONF_SSL] if port is None: port = DEFAULT_SSL_PORT if ssl else DEFAULT_PORT diff --git a/homeassistant/components/enigma2/media_player.py b/homeassistant/components/enigma2/media_player.py index 85dec4abd94..f8a341481a8 100644 --- a/homeassistant/components/enigma2/media_player.py +++ b/homeassistant/components/enigma2/media_player.py @@ -107,7 +107,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): port=config.get(CONF_PORT), username=config.get(CONF_USERNAME), password=config.get(CONF_PASSWORD), - is_https=config.get(CONF_SSL), + is_https=config[CONF_SSL], prefer_picon=config.get(CONF_USE_CHANNEL_ICON), mac_address=config.get(CONF_MAC_ADDRESS), turn_off_to_deep=config.get(CONF_DEEP_STANDBY), diff --git a/homeassistant/components/epson/media_player.py b/homeassistant/components/epson/media_player.py index b39722c39f3..6a04988bebb 100644 --- a/homeassistant/components/epson/media_player.py +++ b/homeassistant/components/epson/media_player.py @@ -90,7 +90,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= name = config.get(CONF_NAME) host = config.get(CONF_HOST) port = config.get(CONF_PORT) - ssl = config.get(CONF_SSL) + ssl = config[CONF_SSL] epson_proj = EpsonProjector( async_get_clientsession(hass, verify_ssl=False), name, host, port, ssl diff --git a/homeassistant/components/hikvision/binary_sensor.py b/homeassistant/components/hikvision/binary_sensor.py index 140f6908dce..4d2a879bc73 100644 --- a/homeassistant/components/hikvision/binary_sensor.py +++ b/homeassistant/components/hikvision/binary_sensor.py @@ -90,10 +90,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): customize = config.get(CONF_CUSTOMIZE) - if config.get(CONF_SSL): - protocol = "https" - else: - protocol = "http" + protocol = "https" if config[CONF_SSL] else "http" url = f"{protocol}://{host}" diff --git a/homeassistant/components/homematic/__init__.py b/homeassistant/components/homematic/__init__.py index 24c9e37a3be..4dfc27650c8 100644 --- a/homeassistant/components/homematic/__init__.py +++ b/homeassistant/components/homematic/__init__.py @@ -222,7 +222,7 @@ def setup(hass, config): "password": rconfig.get(CONF_PASSWORD), "callbackip": rconfig.get(CONF_CALLBACK_IP), "callbackport": rconfig.get(CONF_CALLBACK_PORT), - "ssl": rconfig.get(CONF_SSL), + "ssl": rconfig[CONF_SSL], "verify_ssl": rconfig.get(CONF_VERIFY_SSL), "connect": True, } diff --git a/homeassistant/components/influxdb/sensor.py b/homeassistant/components/influxdb/sensor.py index 9d0eaa84340..64ab1174b8b 100644 --- a/homeassistant/components/influxdb/sensor.py +++ b/homeassistant/components/influxdb/sensor.py @@ -75,7 +75,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): "host": config[CONF_HOST], "password": config.get(CONF_PASSWORD), "port": config.get(CONF_PORT), - "ssl": config.get(CONF_SSL), + "ssl": config[CONF_SSL], "username": config.get(CONF_USERNAME), "verify_ssl": config.get(CONF_VERIFY_SSL), } @@ -203,7 +203,7 @@ class InfluxSensorData: points = list(self.influx.query(self.query).get_points()) if not points: _LOGGER.warning( - "Query returned no points, sensor state set to UNKNOWN: %s", self.query, + "Query returned no points, sensor state set to UNKNOWN: %s", self.query ) self.value = None else: diff --git a/homeassistant/components/itunes/media_player.py b/homeassistant/components/itunes/media_player.py index ccefb681d6e..e96c40b13b6 100644 --- a/homeassistant/components/itunes/media_player.py +++ b/homeassistant/components/itunes/media_player.py @@ -198,7 +198,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): config.get(CONF_NAME), config.get(CONF_HOST), config.get(CONF_PORT), - config.get(CONF_SSL), + config[CONF_SSL], add_entities, ) ] diff --git a/homeassistant/components/mfi/switch.py b/homeassistant/components/mfi/switch.py index b3d3e0ea285..08ed841e7e1 100644 --- a/homeassistant/components/mfi/switch.py +++ b/homeassistant/components/mfi/switch.py @@ -40,7 +40,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): host = config.get(CONF_HOST) username = config.get(CONF_USERNAME) password = config.get(CONF_PASSWORD) - use_tls = config.get(CONF_SSL) + use_tls = config[CONF_SSL] verify_tls = config.get(CONF_VERIFY_SSL) default_port = 6443 if use_tls else 6080 port = int(config.get(CONF_PORT, default_port)) diff --git a/homeassistant/components/octoprint/__init__.py b/homeassistant/components/octoprint/__init__.py index 9e8f1dff6c6..c345dd6cce7 100644 --- a/homeassistant/components/octoprint/__init__.py +++ b/homeassistant/components/octoprint/__init__.py @@ -144,9 +144,9 @@ def setup(hass, config): for printer in config[DOMAIN]: name = printer[CONF_NAME] - ssl = "s" if printer[CONF_SSL] else "" + protocol = "https" if printer[CONF_SSL] else "http" base_url = ( - f"http{ssl}://{printer[CONF_HOST]}:{printer[CONF_PORT]}" + f"{protocol}://{printer[CONF_HOST]}:{printer[CONF_PORT]}" f"{printer[CONF_PATH]}api/" ) api_key = printer[CONF_API_KEY] diff --git a/homeassistant/components/opengarage/cover.py b/homeassistant/components/opengarage/cover.py index 0bb9d7a7c5b..e34f98c87d2 100644 --- a/homeassistant/components/opengarage/cover.py +++ b/homeassistant/components/opengarage/cover.py @@ -64,7 +64,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): CONF_NAME: device_config.get(CONF_NAME), CONF_HOST: device_config.get(CONF_HOST), CONF_PORT: device_config.get(CONF_PORT), - CONF_SSL: device_config.get(CONF_SSL), + CONF_SSL: device_config[CONF_SSL], CONF_VERIFY_SSL: device_config.get(CONF_VERIFY_SSL), CONF_DEVICE_KEY: device_config.get(CONF_DEVICE_KEY), } @@ -80,7 +80,7 @@ class OpenGarageCover(CoverDevice): def __init__(self, args): """Initialize the cover.""" self.opengarage_url = ( - f"http{'s' if args[CONF_SSL] else ''}://" + f"{'https' if args[CONF_SSL] else 'http'}://" f"{args[CONF_HOST]}:{args[CONF_PORT]}" ) self._name = args[CONF_NAME] diff --git a/homeassistant/components/plex/__init__.py b/homeassistant/components/plex/__init__.py index ff36f4f5c32..6dabc5ad594 100644 --- a/homeassistant/components/plex/__init__.py +++ b/homeassistant/components/plex/__init__.py @@ -100,10 +100,10 @@ def _async_setup_plex(hass, config): if MP_DOMAIN in server_config: hass.data.setdefault(PLEX_MEDIA_PLAYER_OPTIONS, server_config.pop(MP_DOMAIN)) if CONF_HOST in server_config: - prefix = "https" if server_config.pop(CONF_SSL) else "http" + protocol = "https" if server_config.pop(CONF_SSL) else "http" server_config[ CONF_URL - ] = f"{prefix}://{server_config.pop(CONF_HOST)}:{server_config.pop(CONF_PORT)}" + ] = f"{protocol}://{server_config.pop(CONF_HOST)}:{server_config.pop(CONF_PORT)}" hass.async_create_task( hass.config_entries.flow.async_init( PLEX_DOMAIN, diff --git a/homeassistant/components/pyload/sensor.py b/homeassistant/components/pyload/sensor.py index 579919821a3..249fb85bacc 100644 --- a/homeassistant/components/pyload/sensor.py +++ b/homeassistant/components/pyload/sensor.py @@ -51,12 +51,12 @@ def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the pyLoad sensors.""" host = config.get(CONF_HOST) port = config.get(CONF_PORT) - ssl = "s" if config.get(CONF_SSL) else "" + protocol = "https" if config[CONF_SSL] else "http" name = config.get(CONF_NAME) username = config.get(CONF_USERNAME) password = config.get(CONF_PASSWORD) monitored_types = config.get(CONF_MONITORED_VARIABLES) - url = f"http{ssl}://{host}:{port}/api/" + url = f"{protocol}://{host}:{port}/api/" try: pyloadapi = PyLoadAPI(api_url=url, username=username, password=password) diff --git a/homeassistant/components/qnap/sensor.py b/homeassistant/components/qnap/sensor.py index 475e02aba86..dc148d4f516 100644 --- a/homeassistant/components/qnap/sensor.py +++ b/homeassistant/components/qnap/sensor.py @@ -175,7 +175,7 @@ class QNAPStatsAPI: def __init__(self, config): """Initialize the API wrapper.""" - protocol = "https" if config.get(CONF_SSL) else "http" + protocol = "https" if config[CONF_SSL] else "http" self._api = QNAPStats( "{}://{}".format(protocol, config.get(CONF_HOST)), config.get(CONF_PORT), diff --git a/homeassistant/components/radarr/sensor.py b/homeassistant/components/radarr/sensor.py index 30051d929db..a15f8b4fe4e 100644 --- a/homeassistant/components/radarr/sensor.py +++ b/homeassistant/components/radarr/sensor.py @@ -53,11 +53,11 @@ SENSOR_TYPES = { } ENDPOINTS = { - "diskspace": "http{0}://{1}:{2}/{3}api/diskspace", - "upcoming": "http{0}://{1}:{2}/{3}api/calendar?start={4}&end={5}", - "movies": "http{0}://{1}:{2}/{3}api/movie", - "commands": "http{0}://{1}:{2}/{3}api/command", - "status": "http{0}://{1}:{2}/{3}api/system/status", + "diskspace": "{0}://{1}:{2}/{3}api/diskspace", + "upcoming": "{0}://{1}:{2}/{3}api/calendar?start={4}&end={5}", + "movies": "{0}://{1}:{2}/{3}api/movie", + "commands": "{0}://{1}:{2}/{3}api/command", + "status": "{0}://{1}:{2}/{3}api/system/status", } # Support to Yottabytes for the future, why not @@ -110,7 +110,7 @@ class RadarrSensor(Entity): self.apikey = conf.get(CONF_API_KEY) self.included = conf.get(CONF_INCLUDED) self.days = int(conf.get(CONF_DAYS)) - self.ssl = "s" if conf.get(CONF_SSL) else "" + self.ssl = "https" if conf.get(CONF_SSL) else "http" self._state = None self.data = [] self._tz = timezone(str(hass.config.time_zone)) diff --git a/homeassistant/components/sabnzbd/__init__.py b/homeassistant/components/sabnzbd/__init__.py index b36abbedb48..64265f71903 100644 --- a/homeassistant/components/sabnzbd/__init__.py +++ b/homeassistant/components/sabnzbd/__init__.py @@ -134,7 +134,7 @@ async def async_setup(hass, config): conf = config.get(DOMAIN) if conf is not None: - use_ssl = conf.get(CONF_SSL) + use_ssl = conf[CONF_SSL] name = conf.get(CONF_NAME) api_key = conf.get(CONF_API_KEY) await async_configure_sabnzbd(hass, conf, use_ssl, name, api_key) diff --git a/homeassistant/components/sma/sensor.py b/homeassistant/components/sma/sensor.py index e5afa272c40..ce61d4ff17b 100644 --- a/homeassistant/components/sma/sensor.py +++ b/homeassistant/components/sma/sensor.py @@ -131,7 +131,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= session = async_get_clientsession(hass, verify_ssl=config[CONF_VERIFY_SSL]) grp = config[CONF_GROUP] - url = "http{}://{}".format("s" if config[CONF_SSL] else "", config[CONF_HOST]) + protocol = "https" if config[CONF_SSL] else "http" + url = f"{protocol}://{config[CONF_HOST]}" sma = pysma.SMA(session, url, config[CONF_PASSWORD], group=grp) diff --git a/homeassistant/components/sonarr/sensor.py b/homeassistant/components/sonarr/sensor.py index ac9b81be7a4..d7298b2abeb 100644 --- a/homeassistant/components/sonarr/sensor.py +++ b/homeassistant/components/sonarr/sensor.py @@ -52,13 +52,13 @@ SENSOR_TYPES = { } ENDPOINTS = { - "diskspace": "http{0}://{1}:{2}/{3}api/diskspace", - "queue": "http{0}://{1}:{2}/{3}api/queue", - "upcoming": "http{0}://{1}:{2}/{3}api/calendar?start={4}&end={5}", - "wanted": "http{0}://{1}:{2}/{3}api/wanted/missing", - "series": "http{0}://{1}:{2}/{3}api/series", - "commands": "http{0}://{1}:{2}/{3}api/command", - "status": "http{0}://{1}:{2}/{3}api/system/status", + "diskspace": "{0}://{1}:{2}/{3}api/diskspace", + "queue": "{0}://{1}:{2}/{3}api/queue", + "upcoming": "{0}://{1}:{2}/{3}api/calendar?start={4}&end={5}", + "wanted": "{0}://{1}:{2}/{3}api/wanted/missing", + "series": "{0}://{1}:{2}/{3}api/series", + "commands": "{0}://{1}:{2}/{3}api/command", + "status": "{0}://{1}:{2}/{3}api/system/status", } # Support to Yottabytes for the future, why not @@ -111,7 +111,7 @@ class SonarrSensor(Entity): self.apikey = conf.get(CONF_API_KEY) self.included = conf.get(CONF_INCLUDED) self.days = int(conf.get(CONF_DAYS)) - self.ssl = "s" if conf.get(CONF_SSL) else "" + self.ssl = "https" if conf.get(CONF_SSL) else "http" self._state = None self.data = [] self._tz = timezone(str(hass.config.time_zone)) diff --git a/homeassistant/components/splunk/__init__.py b/homeassistant/components/splunk/__init__.py index 5b26d9b1c6f..bbff510db14 100644 --- a/homeassistant/components/splunk/__init__.py +++ b/homeassistant/components/splunk/__init__.py @@ -70,7 +70,7 @@ def setup(hass, config): host = conf.get(CONF_HOST) port = conf.get(CONF_PORT) token = conf.get(CONF_TOKEN) - use_ssl = conf.get(CONF_SSL) + use_ssl = conf[CONF_SSL] verify_ssl = conf.get(CONF_VERIFY_SSL) name = conf.get(CONF_NAME) entity_filter = conf[CONF_FILTER] diff --git a/homeassistant/components/tautulli/sensor.py b/homeassistant/components/tautulli/sensor.py index b800bf6af1e..3c61559bcfa 100644 --- a/homeassistant/components/tautulli/sensor.py +++ b/homeassistant/components/tautulli/sensor.py @@ -59,7 +59,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= api_key = config[CONF_API_KEY] monitored_conditions = config.get(CONF_MONITORED_CONDITIONS) user = config.get(CONF_MONITORED_USERS) - use_ssl = config.get(CONF_SSL) + use_ssl = config[CONF_SSL] verify_ssl = config.get(CONF_VERIFY_SSL) session = async_get_clientsession(hass, verify_ssl) diff --git a/homeassistant/components/venstar/climate.py b/homeassistant/components/venstar/climate.py index effecd7244c..7de6427b5d8 100644 --- a/homeassistant/components/venstar/climate.py +++ b/homeassistant/components/venstar/climate.py @@ -82,10 +82,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): timeout = config.get(CONF_TIMEOUT) humidifier = config.get(CONF_HUMIDIFIER) - if config.get(CONF_SSL): - proto = "https" - else: - proto = "http" + protocol = "https" if config[CONF_SSL] else "http" client = VenstarColorTouch( addr=host, @@ -93,7 +90,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): user=username, password=password, pin=pin, - proto=proto, + proto=protocol, ) add_entities([VenstarThermostat(client, humidifier)], True) diff --git a/homeassistant/components/zabbix/__init__.py b/homeassistant/components/zabbix/__init__.py index b6576fc6893..644d35da728 100644 --- a/homeassistant/components/zabbix/__init__.py +++ b/homeassistant/components/zabbix/__init__.py @@ -40,12 +40,9 @@ def setup(hass, config): """Set up the Zabbix component.""" conf = config[DOMAIN] - if conf[CONF_SSL]: - schema = "https" - else: - schema = "http" + protocol = "https" if config[CONF_SSL] else "http" - url = urljoin("{}://{}".format(schema, conf[CONF_HOST]), conf[CONF_PATH]) + url = urljoin(f"{protocol}://{conf[CONF_HOST]}", conf[CONF_PATH]) username = conf.get(CONF_USERNAME) password = conf.get(CONF_PASSWORD) diff --git a/homeassistant/components/zoneminder/__init__.py b/homeassistant/components/zoneminder/__init__.py index 3007c981480..cd62cef74eb 100644 --- a/homeassistant/components/zoneminder/__init__.py +++ b/homeassistant/components/zoneminder/__init__.py @@ -58,13 +58,10 @@ def setup(hass, config): success = True for conf in config[DOMAIN]: - if conf[CONF_SSL]: - schema = "https" - else: - schema = "http" + protocol = "https" if config[CONF_SSL] else "http" host_name = conf[CONF_HOST] - server_origin = f"{schema}://{host_name}" + server_origin = f"{protocol}://{host_name}" zm_client = ZoneMinder( server_origin, conf.get(CONF_USERNAME),