Sleepiq single sleeper crash (#24941)

* Update sleepyq to 0.7

Fixes crash when working with a single sleeper.

* sleepiq: Handle null side definitions

These happen if no sleeper is defined for a side of the bed. Don't
create sensors for null sides; they'll crash every time we try to use
them.

* sleepiq: Fix urls mocked to match sleepyq 0.7

* sleepi: Fix test_sensor.TestSleepIQSensorSetup

Sleepyq 0.7 throws on empty strings, so we have to specify them.

* sleepiq: Test for ValueError thrown by sleepyq 0.7

* sleepiq: Drop no longer used HTTPError import

* sleepiq: Add tests for single sleeper case

* sleepiq: Shorten comments to not overflow line length

* sleepiq: Use formatted string literals for adding suffixes to test files

* sleepiq: Use str.format() for test suffixing
This commit is contained in:
David Winn authored and Pascal Vizeli committed 2019-07-07 08:40:02 +02:00
1 parent adbec5bffc
commit 628e12c944
11 files changed
+103 -18

No files matched your search

@@ -30,6 +30,7 @@ class TestSleepIQBinarySensorSetup(unittest.TestCase):
'username': self.username,
'password': self.password,
}
self.DEVICES = []
def tearDown(self): # pylint: disable=invalid-name
"""Stop everything that was started."""
@@ -56,3 +57,21 @@ class TestSleepIQBinarySensorSetup(unittest.TestCase):
right_side = self.DEVICES[0]
assert 'SleepNumber ILE Test2 Is In Bed' == right_side.name
assert 'off' == right_side.state
@requests_mock.Mocker()
def test_setup_single(self, mock):
"""Test for successfully setting up the SleepIQ platform."""
mock_responses(mock, single=True)
setup_component(self.hass, 'sleepiq', {
'sleepiq': self.config})
sleepiq.setup_platform(self.hass,
self.config,
self.add_entities,
MagicMock())
assert 1 == len(self.DEVICES)
right_side = self.DEVICES[0]
assert 'SleepNumber ILE Test1 Is In Bed' == right_side.name
assert 'on' == right_side.state
+9 -5
View File
@@ -10,21 +10,25 @@ import homeassistant.components.sleepiq as sleepiq
from tests.common import load_fixture, get_test_home_assistant
def mock_responses(mock):
def mock_responses(mock, single=False):
"""Mock responses for SleepIQ."""
base_url = 'https://api.sleepiq.sleepnumber.com/rest/'
base_url = 'https://prod-api.sleepiq.sleepnumber.com/rest/'
if single:
suffix = '-single'
else:
suffix = ''
mock.put(
base_url + 'login',
text=load_fixture('sleepiq-login.json'))
mock.get(
base_url + 'bed?_k=0987',
text=load_fixture('sleepiq-bed.json'))
text=load_fixture('sleepiq-bed{}.json'.format(suffix)))
mock.get(
base_url + 'sleeper?_k=0987',
text=load_fixture('sleepiq-sleeper.json'))
mock.get(
base_url + 'bed/familyStatus?_k=0987',
text=load_fixture('sleepiq-familystatus.json'))
text=load_fixture('sleepiq-familystatus{}.json'.format(suffix)))
class TestSleepIQ(unittest.TestCase):
@@ -61,7 +65,7 @@ class TestSleepIQ(unittest.TestCase):
@requests_mock.Mocker()
def test_setup_login_failed(self, mock):
"""Test the setup if a bad username or password is given."""
mock.put('https://api.sleepiq.sleepnumber.com/rest/login',
mock.put('https://prod-api.sleepiq.sleepnumber.com/rest/login',
status_code=401,
json=load_fixture('sleepiq-login-failed.json'))
+21 -4
View File
@@ -30,6 +30,7 @@ class TestSleepIQSensorSetup(unittest.TestCase):
'username': self.username,
'password': self.password,
}
self.DEVICES = []
def tearDown(self): # pylint: disable=invalid-name
"""Stop everything that was started."""
@@ -41,10 +42,7 @@ class TestSleepIQSensorSetup(unittest.TestCase):
mock_responses(mock)
assert setup_component(self.hass, 'sleepiq', {
'sleepiq': {
'username': '',
'password': '',
}
'sleepiq': self.config
})
sleepiq.setup_platform(self.hass,
@@ -60,3 +58,22 @@ class TestSleepIQSensorSetup(unittest.TestCase):
right_side = self.DEVICES[0]
assert 'SleepNumber ILE Test2 SleepNumber' == right_side.name
assert 80 == right_side.state
@requests_mock.Mocker()
def test_setup_sigle(self, mock):
"""Test for successfully setting up the SleepIQ platform."""
mock_responses(mock, single=True)
assert setup_component(self.hass, 'sleepiq', {
'sleepiq': self.config
})
sleepiq.setup_platform(self.hass,
self.config,
self.add_entities,
MagicMock())
assert 1 == len(self.DEVICES)
right_side = self.DEVICES[0]
assert 'SleepNumber ILE Test1 SleepNumber' == right_side.name
assert 40 == right_side.state