Add "status" to Sonarr sensor ()

* Use X-Api-Key header

* Increase timeout

* Add "status" to Sonarr sensor

* Update test_sonarr.py

* Update test_sonarr.py

* Update test_sonarr.py

* Update sonarr.py

* Update sonarr.py
pull/9215/head
Trevor 2017-08-29 08:33:27 -05:00 committed by Pascal Vizeli
parent 0de6a37822
commit 75559cb81f
3 changed files with 72 additions and 15 deletions
homeassistant/components/sensor
tests/components/sensor

View File

@ -162,7 +162,7 @@ class RadarrSensor(Entity):
res = requests.get(
ENDPOINTS[self.type].format(
self.ssl, self.host, self.port, self.urlbase, start, end),
headers={'X-Api-Key': self.apikey}, timeout=5)
headers={'X-Api-Key': self.apikey}, timeout=10)
except OSError:
_LOGGER.error("Host %s is not available", self.host)
self._available = False

View File

@ -36,17 +36,19 @@ SENSOR_TYPES = {
'upcoming': ['Upcoming', 'Episodes', 'mdi:television'],
'wanted': ['Wanted', 'Episodes', 'mdi:television'],
'series': ['Series', 'Shows', 'mdi:television'],
'commands': ['Commands', 'Commands', 'mdi:code-braces']
'commands': ['Commands', 'Commands', 'mdi:code-braces'],
'status': ['Status', 'Status', 'mdi:information']
}
ENDPOINTS = {
'diskspace': 'http{0}://{1}:{2}/{3}api/diskspace?apikey={4}',
'queue': 'http{0}://{1}:{2}/{3}api/queue?apikey={4}',
'diskspace': 'http{0}://{1}:{2}/{3}api/diskspace',
'queue': 'http{0}://{1}:{2}/{3}api/queue',
'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}'
'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'
}
# Support to Yottabytes for the future, why not
@ -156,6 +158,8 @@ class SonarrSensor(Entity):
for show in self.data:
attributes[show['title']] = '{}/{} Episodes'.format(
show['episodeFileCount'], show['episodeCount'])
elif self.type == 'status':
attributes = self.data
return attributes
@property
@ -168,9 +172,12 @@ class SonarrSensor(Entity):
start = get_date(self._tz)
end = get_date(self._tz, self.days)
try:
res = requests.get(ENDPOINTS[self.type].format(
self.ssl, self.host, self.port, self.urlbase, self.apikey,
start, end), timeout=5)
res = requests.get(
ENDPOINTS[self.type].format(
self.ssl, self.host, self.port,
self.urlbase, start, end),
headers={'X-Api-Key': self.apikey},
timeout=10)
except OSError:
_LOGGER.error("Host %s is not available", self.host)
self._available = False
@ -193,10 +200,13 @@ class SonarrSensor(Entity):
self._state = len(self.data)
elif self.type == 'wanted':
data = res.json()
res = requests.get('{}&pageSize={}'.format(
ENDPOINTS[self.type].format(
self.ssl, self.host, self.port, self.urlbase,
self.apikey), data['totalRecords']), timeout=5)
res = requests.get(
'{}?pageSize={}'.format(
ENDPOINTS[self.type].format(
self.ssl, self.host, self.port, self.urlbase),
data['totalRecords']),
headers={'X-Api-Key': self.apikey},
timeout=10)
self.data = res.json()['records']
self._state = len(self.data)
elif self.type == 'diskspace':
@ -217,6 +227,9 @@ class SonarrSensor(Entity):
self._unit
)
)
elif self.type == 'status':
self.data = res.json()
self._state = self.data['version']
self._available = True

View File

@ -549,6 +549,25 @@ def mocked_requests_get(*args, **kwargs):
"totalSpace": 499738734592
}
], 200)
elif 'api/system/status' in url:
return MockResponse({
"version": "2.0.0.1121",
"buildTime": "2014-02-08T20:49:36.5560392Z",
"isDebug": "false",
"isProduction": "true",
"isAdmin": "true",
"isUserInteractive": "false",
"startupPath": "C:\\ProgramData\\NzbDrone\\bin",
"appData": "C:\\ProgramData\\NzbDrone",
"osVersion": "6.2.9200.0",
"isMono": "false",
"isLinux": "false",
"isWindows": "true",
"branch": "develop",
"authentication": "false",
"startOfWeek": 0,
"urlBase": ""
}, 200)
else:
return MockResponse({
"error": "Unauthorized"
@ -794,6 +813,31 @@ class TestSonarrSetup(unittest.TestCase):
device.device_state_attributes["Bob's Burgers"]
)
@unittest.mock.patch('requests.get', side_effect=mocked_requests_get)
def test_system_status(self, req_mock):
"""Test getting system status"""
config = {
'platform': 'sonarr',
'api_key': 'foo',
'days': '2',
'unit': 'GB',
"include_paths": [
'/data'
],
'monitored_conditions': [
'status'
]
}
sonarr.setup_platform(self.hass, config, self.add_devices, None)
for device in self.DEVICES:
device.update()
self.assertEqual('2.0.0.1121', device.state)
self.assertEqual('mdi:information', device.icon)
self.assertEqual('Sonarr Status', device.name)
self.assertEqual(
'6.2.9200.0',
device.device_state_attributes['osVersion'])
@pytest.mark.skip
@unittest.mock.patch('requests.get', side_effect=mocked_requests_get)
def test_ssl(self, req_mock):