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()
This commit is contained in:
Nick Sabinske 2016-12-18 13:05:05 -05:00 committed by Paulus Schoutsen
parent 18cf6f6f99
commit 44eaca5985

View file

@ -23,9 +23,11 @@ _LOGGER = logging.getLogger(__name__)
CONF_DAYS = 'days' CONF_DAYS = 'days'
CONF_INCLUDED = 'include_paths' CONF_INCLUDED = 'include_paths'
CONF_UNIT = 'unit' CONF_UNIT = 'unit'
CONF_URLBASE = 'urlbase'
DEFAULT_HOST = 'localhost' DEFAULT_HOST = 'localhost'
DEFAULT_PORT = 8989 DEFAULT_PORT = 8989
DEFAULT_URLBASE = ''
DEFAULT_DAYS = '1' DEFAULT_DAYS = '1'
DEFAULT_UNIT = 'GB' DEFAULT_UNIT = 'GB'
@ -39,12 +41,13 @@ SENSOR_TYPES = {
} }
ENDPOINTS = { ENDPOINTS = {
'diskspace': 'http{0}://{1}:{2}/api/diskspace?apikey={3}', 'diskspace': 'http{0}://{1}:{2}/{3}api/diskspace?apikey={4}',
'queue': 'http{0}://{1}:{2}/api/queue?apikey={3}', 'queue': 'http{0}://{1}:{2}/{3}api/queue?apikey={4}',
'upcoming': 'http{0}://{1}:{2}/api/calendar?apikey={3}&start={4}&end={5}', 'upcoming':
'wanted': 'http{0}://{1}:{2}/api/wanted/missing?apikey={3}', 'http{0}://{1}:{2}/{3}api/calendar?apikey={4}&start={5}&end={6}',
'series': 'http{0}://{1}:{2}/api/series?apikey={3}', 'wanted': 'http{0}://{1}:{2}/{3}api/wanted/missing?apikey={4}',
'commands': 'http{0}://{1}:{2}/api/command?apikey={3}' '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 # 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_SSL, default=False): cv.boolean,
vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string, vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port, 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_DAYS, default=DEFAULT_DAYS): cv.string,
vol.Optional(CONF_UNIT, default=DEFAULT_UNIT): vol.In(BYTE_SIZES) vol.Optional(CONF_UNIT, default=DEFAULT_UNIT): vol.In(BYTE_SIZES)
}) })
@ -80,6 +84,9 @@ class SonarrSensor(Entity):
self.conf = conf self.conf = conf
self.host = conf.get(CONF_HOST) self.host = conf.get(CONF_HOST)
self.port = conf.get(CONF_PORT) 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.apikey = conf.get(CONF_API_KEY)
self.included = conf.get(CONF_INCLUDED) self.included = conf.get(CONF_INCLUDED)
self.days = int(conf.get(CONF_DAYS)) self.days = int(conf.get(CONF_DAYS))
@ -107,7 +114,8 @@ class SonarrSensor(Entity):
try: try:
res = requests.get( res = requests.get(
ENDPOINTS[self.type].format( 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) timeout=5)
except OSError: except OSError:
_LOGGER.error('Host %s is not available', self.host) _LOGGER.error('Host %s is not available', self.host)
@ -133,7 +141,8 @@ class SonarrSensor(Entity):
data = res.json() data = res.json()
res = requests.get('{}&pageSize={}'.format( res = requests.get('{}&pageSize={}'.format(
ENDPOINTS[self.type].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) data['totalRecords']), timeout=5)
self.data = res.json()['records'] self.data = res.json()['records']
self._state = len(self.data) self._state = len(self.data)