2019-08-31 12:30:59 +00:00
|
|
|
"""Tests for the Withings component."""
|
2019-12-28 20:25:37 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
|
2019-08-31 12:30:59 +00:00
|
|
|
from asynctest import MagicMock
|
|
|
|
import pytest
|
2019-10-24 16:41:04 +00:00
|
|
|
from withings_api import WithingsApi
|
2019-12-10 20:54:50 +00:00
|
|
|
from withings_api.common import TimeoutException, UnauthorizedException
|
2019-08-31 12:30:59 +00:00
|
|
|
|
|
|
|
from homeassistant.components.withings.common import (
|
|
|
|
NotAuthenticatedError,
|
|
|
|
WithingsDataManager,
|
|
|
|
)
|
2019-12-28 20:25:37 +00:00
|
|
|
from homeassistant.config import async_process_ha_core_config
|
|
|
|
from homeassistant.core import HomeAssistant
|
2019-12-10 20:54:50 +00:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
2019-12-28 20:25:37 +00:00
|
|
|
from homeassistant.util import dt
|
2019-08-31 12:30:59 +00:00
|
|
|
|
2019-12-31 13:30:09 +00:00
|
|
|
DEFAULT_TIME_ZONE = dt.DEFAULT_TIME_ZONE
|
|
|
|
|
|
|
|
|
|
|
|
def teardown():
|
|
|
|
"""Ensure the time zone is reverted after tests finish."""
|
|
|
|
dt.set_default_time_zone(DEFAULT_TIME_ZONE)
|
|
|
|
|
2019-08-31 12:30:59 +00:00
|
|
|
|
2019-10-10 18:22:36 +00:00
|
|
|
@pytest.fixture(name="withings_api")
|
2019-10-24 16:41:04 +00:00
|
|
|
def withings_api_fixture() -> WithingsApi:
|
2019-10-10 18:22:36 +00:00
|
|
|
"""Provide withings api."""
|
2019-10-24 16:41:04 +00:00
|
|
|
withings_api = WithingsApi.__new__(WithingsApi)
|
2019-12-28 20:25:37 +00:00
|
|
|
withings_api.user_get_device = MagicMock()
|
|
|
|
withings_api.measure_get_meas = MagicMock()
|
|
|
|
withings_api.sleep_get = MagicMock()
|
|
|
|
withings_api.sleep_get_summary = MagicMock()
|
2019-10-10 18:22:36 +00:00
|
|
|
return withings_api
|
2019-08-31 12:30:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="data_manager")
|
2019-10-24 16:41:04 +00:00
|
|
|
def data_manager_fixture(hass, withings_api: WithingsApi) -> WithingsDataManager:
|
2019-08-31 12:30:59 +00:00
|
|
|
"""Provide data manager."""
|
2019-10-10 18:22:36 +00:00
|
|
|
return WithingsDataManager(hass, "My Profile", withings_api)
|
2019-08-31 12:30:59 +00:00
|
|
|
|
|
|
|
|
2019-10-24 16:41:04 +00:00
|
|
|
def test_print_service() -> None:
|
2019-08-31 12:30:59 +00:00
|
|
|
"""Test method."""
|
|
|
|
# Go from None to True
|
|
|
|
WithingsDataManager.service_available = None
|
|
|
|
assert WithingsDataManager.print_service_available()
|
|
|
|
assert WithingsDataManager.service_available is True
|
|
|
|
assert not WithingsDataManager.print_service_available()
|
|
|
|
assert not WithingsDataManager.print_service_available()
|
|
|
|
|
|
|
|
# Go from True to False
|
|
|
|
assert WithingsDataManager.print_service_unavailable()
|
|
|
|
assert WithingsDataManager.service_available is False
|
|
|
|
assert not WithingsDataManager.print_service_unavailable()
|
|
|
|
assert not WithingsDataManager.print_service_unavailable()
|
|
|
|
|
|
|
|
# Go from False to True
|
|
|
|
assert WithingsDataManager.print_service_available()
|
|
|
|
assert WithingsDataManager.service_available is True
|
|
|
|
assert not WithingsDataManager.print_service_available()
|
|
|
|
assert not WithingsDataManager.print_service_available()
|
|
|
|
|
|
|
|
# Go from Non to False
|
|
|
|
WithingsDataManager.service_available = None
|
|
|
|
assert WithingsDataManager.print_service_unavailable()
|
|
|
|
assert WithingsDataManager.service_available is False
|
|
|
|
assert not WithingsDataManager.print_service_unavailable()
|
|
|
|
assert not WithingsDataManager.print_service_unavailable()
|
|
|
|
|
|
|
|
|
2019-10-24 16:41:04 +00:00
|
|
|
async def test_data_manager_call(data_manager: WithingsDataManager) -> None:
|
2019-08-31 12:30:59 +00:00
|
|
|
"""Test method."""
|
|
|
|
# Not authenticated 1.
|
2019-10-24 16:41:04 +00:00
|
|
|
test_function = MagicMock(side_effect=UnauthorizedException(401))
|
|
|
|
with pytest.raises(NotAuthenticatedError):
|
|
|
|
await data_manager.call(test_function)
|
2019-08-31 12:30:59 +00:00
|
|
|
|
|
|
|
# Not authenticated 2.
|
2019-10-24 16:41:04 +00:00
|
|
|
test_function = MagicMock(side_effect=TimeoutException(522))
|
|
|
|
with pytest.raises(PlatformNotReady):
|
|
|
|
await data_manager.call(test_function)
|
2019-08-31 12:30:59 +00:00
|
|
|
|
|
|
|
# Service error.
|
|
|
|
test_function = MagicMock(side_effect=PlatformNotReady())
|
2019-10-24 16:41:04 +00:00
|
|
|
with pytest.raises(PlatformNotReady):
|
|
|
|
await data_manager.call(test_function)
|
2019-08-31 12:30:59 +00:00
|
|
|
|
|
|
|
|
2019-10-24 16:41:04 +00:00
|
|
|
async def test_data_manager_call_throttle_enabled(
|
2019-10-29 06:32:34 +00:00
|
|
|
data_manager: WithingsDataManager,
|
2019-10-24 16:41:04 +00:00
|
|
|
) -> None:
|
2019-08-31 12:30:59 +00:00
|
|
|
"""Test method."""
|
|
|
|
hello_func = MagicMock(return_value="HELLO2")
|
|
|
|
|
|
|
|
result = await data_manager.call(hello_func, throttle_domain="test")
|
|
|
|
assert result == "HELLO2"
|
|
|
|
|
|
|
|
result = await data_manager.call(hello_func, throttle_domain="test")
|
|
|
|
assert result == "HELLO2"
|
|
|
|
|
|
|
|
assert hello_func.call_count == 1
|
|
|
|
|
|
|
|
|
2019-10-24 16:41:04 +00:00
|
|
|
async def test_data_manager_call_throttle_disabled(
|
2019-10-29 06:32:34 +00:00
|
|
|
data_manager: WithingsDataManager,
|
2019-10-24 16:41:04 +00:00
|
|
|
) -> None:
|
2019-08-31 12:30:59 +00:00
|
|
|
"""Test method."""
|
|
|
|
hello_func = MagicMock(return_value="HELLO2")
|
|
|
|
|
|
|
|
result = await data_manager.call(hello_func)
|
|
|
|
assert result == "HELLO2"
|
|
|
|
|
|
|
|
result = await data_manager.call(hello_func)
|
|
|
|
assert result == "HELLO2"
|
|
|
|
|
|
|
|
assert hello_func.call_count == 2
|
2019-12-28 20:25:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_data_manager_update_sleep_date_range(
|
|
|
|
hass: HomeAssistant, data_manager: WithingsDataManager,
|
|
|
|
) -> None:
|
|
|
|
"""Test method."""
|
|
|
|
await async_process_ha_core_config(
|
|
|
|
hass=hass, config={"time_zone": "America/Los_Angeles"}
|
|
|
|
)
|
|
|
|
|
|
|
|
update_start_time = dt.now()
|
|
|
|
await data_manager.update_sleep()
|
|
|
|
|
|
|
|
call_args = data_manager.api.sleep_get.call_args_list[0][1]
|
|
|
|
startdate = call_args.get("startdate")
|
|
|
|
enddate = call_args.get("enddate")
|
|
|
|
|
|
|
|
assert startdate.tzname() == "PST"
|
|
|
|
|
|
|
|
assert enddate.tzname() == "PST"
|
|
|
|
assert startdate.tzname() == "PST"
|
|
|
|
assert update_start_time < enddate
|
|
|
|
assert enddate < update_start_time + timedelta(seconds=1)
|
|
|
|
assert enddate > startdate
|