2016-09-13 22:11:50 +00:00
|
|
|
"""The tests for the SleepIQ component."""
|
2021-10-23 18:49:04 +00:00
|
|
|
from http import HTTPStatus
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
|
2017-03-05 09:41:54 +00:00
|
|
|
from homeassistant import setup
|
2016-09-13 22:11:50 +00:00
|
|
|
import homeassistant.components.sleepiq as sleepiq
|
|
|
|
|
2020-10-19 21:31:33 +00:00
|
|
|
from tests.common import load_fixture
|
|
|
|
|
|
|
|
CONFIG = {"sleepiq": {"username": "foo", "password": "bar"}}
|
2016-09-13 22:11:50 +00:00
|
|
|
|
|
|
|
|
2019-07-07 06:40:02 +00:00
|
|
|
def mock_responses(mock, single=False):
|
2016-12-02 05:45:19 +00:00
|
|
|
"""Mock responses for SleepIQ."""
|
2019-07-31 19:25:30 +00:00
|
|
|
base_url = "https://prod-api.sleepiq.sleepnumber.com/rest/"
|
2019-07-07 06:40:02 +00:00
|
|
|
if single:
|
2019-07-31 19:25:30 +00:00
|
|
|
suffix = "-single"
|
2019-07-07 06:40:02 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
suffix = ""
|
|
|
|
mock.put(base_url + "login", text=load_fixture("sleepiq-login.json"))
|
2020-04-04 22:26:08 +00:00
|
|
|
mock.get(base_url + "bed?_k=0987", text=load_fixture(f"sleepiq-bed{suffix}.json"))
|
2019-07-31 19:25:30 +00:00
|
|
|
mock.get(base_url + "sleeper?_k=0987", text=load_fixture("sleepiq-sleeper.json"))
|
2016-09-13 22:11:50 +00:00
|
|
|
mock.get(
|
2019-07-31 19:25:30 +00:00
|
|
|
base_url + "bed/familyStatus?_k=0987",
|
2020-04-04 22:26:08 +00:00
|
|
|
text=load_fixture(f"sleepiq-familystatus{suffix}.json"),
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-09-13 22:11:50 +00:00
|
|
|
|
|
|
|
|
2020-10-19 21:31:33 +00:00
|
|
|
async def test_setup(hass, requests_mock):
|
|
|
|
"""Test the setup."""
|
|
|
|
mock_responses(requests_mock)
|
2016-09-13 22:11:50 +00:00
|
|
|
|
2020-10-19 21:31:33 +00:00
|
|
|
# We're mocking the load_platform discoveries or else the platforms
|
|
|
|
# will be setup during tear down when blocking till done, but the mocks
|
|
|
|
# are no longer active.
|
|
|
|
with patch("homeassistant.helpers.discovery.load_platform", MagicMock()):
|
|
|
|
assert sleepiq.setup(hass, CONFIG)
|
2016-09-13 22:11:50 +00:00
|
|
|
|
|
|
|
|
2020-10-19 21:31:33 +00:00
|
|
|
async def test_setup_login_failed(hass, requests_mock):
|
|
|
|
"""Test the setup if a bad username or password is given."""
|
|
|
|
mock_responses(requests_mock)
|
|
|
|
requests_mock.put(
|
|
|
|
"https://prod-api.sleepiq.sleepnumber.com/rest/login",
|
2021-10-23 18:49:04 +00:00
|
|
|
status_code=HTTPStatus.UNAUTHORIZED,
|
2020-10-19 21:31:33 +00:00
|
|
|
json=load_fixture("sleepiq-login-failed.json"),
|
|
|
|
)
|
2016-09-13 22:11:50 +00:00
|
|
|
|
2020-10-19 21:31:33 +00:00
|
|
|
response = sleepiq.setup(hass, CONFIG)
|
|
|
|
assert not response
|
2016-09-13 22:11:50 +00:00
|
|
|
|
|
|
|
|
2020-10-19 21:31:33 +00:00
|
|
|
async def test_setup_component_no_login(hass):
|
|
|
|
"""Test the setup when no login is configured."""
|
|
|
|
conf = CONFIG.copy()
|
|
|
|
del conf["sleepiq"]["username"]
|
|
|
|
assert not await setup.async_setup_component(hass, sleepiq.DOMAIN, conf)
|
2016-09-13 22:11:50 +00:00
|
|
|
|
|
|
|
|
2020-10-19 21:31:33 +00:00
|
|
|
async def test_setup_component_no_password(hass):
|
|
|
|
"""Test the setup when no password is configured."""
|
|
|
|
conf = CONFIG.copy()
|
|
|
|
del conf["sleepiq"]["password"]
|
2016-09-13 22:11:50 +00:00
|
|
|
|
2020-10-19 21:31:33 +00:00
|
|
|
assert not await setup.async_setup_component(hass, sleepiq.DOMAIN, conf)
|