Add "status" to Sonarr sensor (#9204)
* 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
This commit is contained in:
parent
0de6a37822
commit
75559cb81f
3 changed files with 72 additions and 15 deletions
|
@ -162,7 +162,7 @@ class RadarrSensor(Entity):
|
||||||
res = requests.get(
|
res = requests.get(
|
||||||
ENDPOINTS[self.type].format(
|
ENDPOINTS[self.type].format(
|
||||||
self.ssl, self.host, self.port, self.urlbase, start, end),
|
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:
|
except OSError:
|
||||||
_LOGGER.error("Host %s is not available", self.host)
|
_LOGGER.error("Host %s is not available", self.host)
|
||||||
self._available = False
|
self._available = False
|
||||||
|
|
|
@ -36,17 +36,19 @@ SENSOR_TYPES = {
|
||||||
'upcoming': ['Upcoming', 'Episodes', 'mdi:television'],
|
'upcoming': ['Upcoming', 'Episodes', 'mdi:television'],
|
||||||
'wanted': ['Wanted', 'Episodes', 'mdi:television'],
|
'wanted': ['Wanted', 'Episodes', 'mdi:television'],
|
||||||
'series': ['Series', 'Shows', '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 = {
|
ENDPOINTS = {
|
||||||
'diskspace': 'http{0}://{1}:{2}/{3}api/diskspace?apikey={4}',
|
'diskspace': 'http{0}://{1}:{2}/{3}api/diskspace',
|
||||||
'queue': 'http{0}://{1}:{2}/{3}api/queue?apikey={4}',
|
'queue': 'http{0}://{1}:{2}/{3}api/queue',
|
||||||
'upcoming':
|
'upcoming':
|
||||||
'http{0}://{1}:{2}/{3}api/calendar?apikey={4}&start={5}&end={6}',
|
'http{0}://{1}:{2}/{3}api/calendar?start={4}&end={5}',
|
||||||
'wanted': 'http{0}://{1}:{2}/{3}api/wanted/missing?apikey={4}',
|
'wanted': 'http{0}://{1}:{2}/{3}api/wanted/missing',
|
||||||
'series': 'http{0}://{1}:{2}/{3}api/series?apikey={4}',
|
'series': 'http{0}://{1}:{2}/{3}api/series',
|
||||||
'commands': 'http{0}://{1}:{2}/{3}api/command?apikey={4}'
|
'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
|
# Support to Yottabytes for the future, why not
|
||||||
|
@ -156,6 +158,8 @@ class SonarrSensor(Entity):
|
||||||
for show in self.data:
|
for show in self.data:
|
||||||
attributes[show['title']] = '{}/{} Episodes'.format(
|
attributes[show['title']] = '{}/{} Episodes'.format(
|
||||||
show['episodeFileCount'], show['episodeCount'])
|
show['episodeFileCount'], show['episodeCount'])
|
||||||
|
elif self.type == 'status':
|
||||||
|
attributes = self.data
|
||||||
return attributes
|
return attributes
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -168,9 +172,12 @@ class SonarrSensor(Entity):
|
||||||
start = get_date(self._tz)
|
start = get_date(self._tz)
|
||||||
end = get_date(self._tz, self.days)
|
end = get_date(self._tz, self.days)
|
||||||
try:
|
try:
|
||||||
res = requests.get(ENDPOINTS[self.type].format(
|
res = requests.get(
|
||||||
self.ssl, self.host, self.port, self.urlbase, self.apikey,
|
ENDPOINTS[self.type].format(
|
||||||
start, end), timeout=5)
|
self.ssl, self.host, self.port,
|
||||||
|
self.urlbase, start, end),
|
||||||
|
headers={'X-Api-Key': self.apikey},
|
||||||
|
timeout=10)
|
||||||
except OSError:
|
except OSError:
|
||||||
_LOGGER.error("Host %s is not available", self.host)
|
_LOGGER.error("Host %s is not available", self.host)
|
||||||
self._available = False
|
self._available = False
|
||||||
|
@ -193,10 +200,13 @@ class SonarrSensor(Entity):
|
||||||
self._state = len(self.data)
|
self._state = len(self.data)
|
||||||
elif self.type == 'wanted':
|
elif self.type == 'wanted':
|
||||||
data = res.json()
|
data = res.json()
|
||||||
res = requests.get('{}&pageSize={}'.format(
|
res = requests.get(
|
||||||
ENDPOINTS[self.type].format(
|
'{}?pageSize={}'.format(
|
||||||
self.ssl, self.host, self.port, self.urlbase,
|
ENDPOINTS[self.type].format(
|
||||||
self.apikey), data['totalRecords']), timeout=5)
|
self.ssl, self.host, self.port, self.urlbase),
|
||||||
|
data['totalRecords']),
|
||||||
|
headers={'X-Api-Key': self.apikey},
|
||||||
|
timeout=10)
|
||||||
self.data = res.json()['records']
|
self.data = res.json()['records']
|
||||||
self._state = len(self.data)
|
self._state = len(self.data)
|
||||||
elif self.type == 'diskspace':
|
elif self.type == 'diskspace':
|
||||||
|
@ -217,6 +227,9 @@ class SonarrSensor(Entity):
|
||||||
self._unit
|
self._unit
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
elif self.type == 'status':
|
||||||
|
self.data = res.json()
|
||||||
|
self._state = self.data['version']
|
||||||
self._available = True
|
self._available = True
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -549,6 +549,25 @@ def mocked_requests_get(*args, **kwargs):
|
||||||
"totalSpace": 499738734592
|
"totalSpace": 499738734592
|
||||||
}
|
}
|
||||||
], 200)
|
], 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:
|
else:
|
||||||
return MockResponse({
|
return MockResponse({
|
||||||
"error": "Unauthorized"
|
"error": "Unauthorized"
|
||||||
|
@ -794,6 +813,31 @@ class TestSonarrSetup(unittest.TestCase):
|
||||||
device.device_state_attributes["Bob's Burgers"]
|
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
|
@pytest.mark.skip
|
||||||
@unittest.mock.patch('requests.get', side_effect=mocked_requests_get)
|
@unittest.mock.patch('requests.get', side_effect=mocked_requests_get)
|
||||||
def test_ssl(self, req_mock):
|
def test_ssl(self, req_mock):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue