Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion homeassistant/components/sensor/radarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def update(self):
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
Expand Down
41 changes: 27 additions & 14 deletions homeassistant/components/sensor/sonarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,19 @@
'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
Expand Down Expand Up @@ -156,6 +158,8 @@ def device_state_attributes(self):
for show in self.data:
attributes[show['title']] = '{}/{} Episodes'.format(
show['episodeFileCount'], show['episodeCount'])
elif self.type == 'status':
attributes = self.data
return attributes

@property
Expand All @@ -168,9 +172,12 @@ def update(self):
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
Expand All @@ -193,10 +200,13 @@ def update(self):
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']),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line unaligned for hanging indent

headers={'X-Api-Key': self.apikey},

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line unaligned for hanging indent

timeout=10)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line unaligned for hanging indent

self.data = res.json()['records']
self._state = len(self.data)
elif self.type == 'diskspace':
Expand All @@ -217,6 +227,9 @@ def update(self):
self._unit
)
)
elif self.type == 'status':
self.data = res.json()
self._state = self.data['version']
self._available = True


Expand Down
44 changes: 44 additions & 0 deletions tests/components/sensor/test_sonarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,25 @@ def json(self):
"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"
Expand Down Expand Up @@ -794,6 +813,31 @@ def test_upcoming_today(self, req_mock):
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):
Expand Down