From 44eaca5985b73a2e2e7af06efb59f34bb0bd7b55 Mon Sep 17 00:00:00 2001
From: Nick Sabinske <quadportnick@users.noreply.github.com>
Date: Sun, 18 Dec 2016 13:05:05 -0500
Subject: [PATCH] Add support for the Sonarr URL Base setting (#4975)

* Add support for the Sonarr URL Base setting

For those of us who have sonarr hidden behind reverse proxy under a path, we need to be able to pass the path

Adds urlbase: XXX to the sonarr yaml... Match it to what you have set in Sonarr (Settings>General>URL Base)

* Fix line lengths

* Fix trailing white space caused by last change

* Removing use of len()
---
 homeassistant/components/sensor/sonarr.py | 25 +++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/homeassistant/components/sensor/sonarr.py b/homeassistant/components/sensor/sonarr.py
index 62f33556c17..d60a26f7092 100644
--- a/homeassistant/components/sensor/sonarr.py
+++ b/homeassistant/components/sensor/sonarr.py
@@ -23,9 +23,11 @@ _LOGGER = logging.getLogger(__name__)
 CONF_DAYS = 'days'
 CONF_INCLUDED = 'include_paths'
 CONF_UNIT = 'unit'
+CONF_URLBASE = 'urlbase'
 
 DEFAULT_HOST = 'localhost'
 DEFAULT_PORT = 8989
+DEFAULT_URLBASE = ''
 DEFAULT_DAYS = '1'
 DEFAULT_UNIT = 'GB'
 
@@ -39,12 +41,13 @@ SENSOR_TYPES = {
 }
 
 ENDPOINTS = {
-    'diskspace': 'http{0}://{1}:{2}/api/diskspace?apikey={3}',
-    'queue': 'http{0}://{1}:{2}/api/queue?apikey={3}',
-    'upcoming': 'http{0}://{1}:{2}/api/calendar?apikey={3}&start={4}&end={5}',
-    'wanted': 'http{0}://{1}:{2}/api/wanted/missing?apikey={3}',
-    'series': 'http{0}://{1}:{2}/api/series?apikey={3}',
-    'commands': 'http{0}://{1}:{2}/api/command?apikey={3}'
+    'diskspace': 'http{0}://{1}:{2}/{3}api/diskspace?apikey={4}',
+    'queue': 'http{0}://{1}:{2}/{3}api/queue?apikey={4}',
+    'upcoming':
+        'http{0}://{1}:{2}/{3}api/calendar?apikey={4}&start={5}&end={6}',
+    'wanted': 'http{0}://{1}:{2}/{3}api/wanted/missing?apikey={4}',
+    'series': 'http{0}://{1}:{2}/{3}api/series?apikey={4}',
+    'commands': 'http{0}://{1}:{2}/{3}api/command?apikey={4}'
 }
 
 # Support to Yottabytes for the future, why not
@@ -57,6 +60,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
     vol.Optional(CONF_SSL, default=False): cv.boolean,
     vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
     vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
+    vol.Optional(CONF_URLBASE, default=DEFAULT_URLBASE): cv.string,
     vol.Optional(CONF_DAYS, default=DEFAULT_DAYS): cv.string,
     vol.Optional(CONF_UNIT, default=DEFAULT_UNIT): vol.In(BYTE_SIZES)
 })
@@ -80,6 +84,9 @@ class SonarrSensor(Entity):
         self.conf = conf
         self.host = conf.get(CONF_HOST)
         self.port = conf.get(CONF_PORT)
+        self.urlbase = conf.get(CONF_URLBASE)
+        if self.urlbase:
+            self.urlbase = "%s/" % self.urlbase.strip('/')
         self.apikey = conf.get(CONF_API_KEY)
         self.included = conf.get(CONF_INCLUDED)
         self.days = int(conf.get(CONF_DAYS))
@@ -107,7 +114,8 @@ class SonarrSensor(Entity):
         try:
             res = requests.get(
                 ENDPOINTS[self.type].format(
-                    self.ssl, self.host, self.port, self.apikey, start, end),
+                    self.ssl, self.host, self.port,
+                    self.urlbase, self.apikey, start, end),
                 timeout=5)
         except OSError:
             _LOGGER.error('Host %s is not available', self.host)
@@ -133,7 +141,8 @@ class SonarrSensor(Entity):
                 data = res.json()
                 res = requests.get('{}&pageSize={}'.format(
                     ENDPOINTS[self.type].format(
-                        self.ssl, self.host, self.port, self.apikey),
+                        self.ssl, self.host, self.port,
+                        self.urlbase, self.apikey),
                     data['totalRecords']), timeout=5)
                 self.data = res.json()['records']
                 self._state = len(self.data)