Fixing timezone issue which caused wrong selection of data to be used. (#30011)

This commit is contained in:
Robert Van Gorkom 2019-12-28 12:25:37 -08:00 committed by Paulus Schoutsen
parent 5a9e543087
commit 08af989658
2 changed files with 35 additions and 4 deletions

View File

@ -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)

View File

@ -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