diff --git a/homeassistant/components/withings/common.py b/homeassistant/components/withings/common.py index 4c3772d77e707d37e27b059a2b1a6c613f804755..9cba055bac4cda494dae47333d7212d8b7a84fcf 100644 --- a/homeassistant/components/withings/common.py +++ b/homeassistant/components/withings/common.py @@ -259,8 +259,8 @@ class WithingsDataManager: async def update_sleep(self) -> SleepGetResponse: """Update the sleep data.""" - end_date = int(time.time()) - start_date = end_date - (6 * 60 * 60) + end_date = dt.now() + start_date = end_date - datetime.timedelta(hours=2) def function(): return self._api.sleep_get(startdate=start_date, enddate=end_date) diff --git a/tests/components/withings/test_common.py b/tests/components/withings/test_common.py index 9328526d6ef1ec558db0d1884f2ca94bb84f7c16..37fcb4ce7f5f5d793bba3f0361b209db73c7b4ba 100644 --- a/tests/components/withings/test_common.py +++ b/tests/components/withings/test_common.py @@ -1,4 +1,6 @@ """Tests for the Withings component.""" +from datetime import timedelta + from asynctest import MagicMock import pytest from withings_api import WithingsApi @@ -8,15 +10,20 @@ from homeassistant.components.withings.common import ( NotAuthenticatedError, WithingsDataManager, ) +from homeassistant.config import async_process_ha_core_config +from homeassistant.core import HomeAssistant from homeassistant.exceptions import PlatformNotReady +from homeassistant.util import dt @pytest.fixture(name="withings_api") def withings_api_fixture() -> WithingsApi: """Provide withings api.""" withings_api = WithingsApi.__new__(WithingsApi) - withings_api.get_measures = MagicMock() - withings_api.get_sleep = MagicMock() + withings_api.user_get_device = MagicMock() + withings_api.measure_get_meas = MagicMock() + withings_api.sleep_get = MagicMock() + withings_api.sleep_get_summary = MagicMock() return withings_api @@ -101,3 +108,27 @@ async def test_data_manager_call_throttle_disabled( assert result == "HELLO2" assert hello_func.call_count == 2 + + +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