Skip to content
Snippets Groups Projects
Commit 08af9896 authored by Robert Van Gorkom's avatar Robert Van Gorkom Committed by Paulus Schoutsen
Browse files

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

parent 5a9e5430
No related branches found
No related tags found
No related merge requests found
...@@ -259,8 +259,8 @@ class WithingsDataManager: ...@@ -259,8 +259,8 @@ class WithingsDataManager:
async def update_sleep(self) -> SleepGetResponse: async def update_sleep(self) -> SleepGetResponse:
"""Update the sleep data.""" """Update the sleep data."""
end_date = int(time.time()) end_date = dt.now()
start_date = end_date - (6 * 60 * 60) start_date = end_date - datetime.timedelta(hours=2)
def function(): def function():
return self._api.sleep_get(startdate=start_date, enddate=end_date) return self._api.sleep_get(startdate=start_date, enddate=end_date)
......
"""Tests for the Withings component.""" """Tests for the Withings component."""
from datetime import timedelta
from asynctest import MagicMock from asynctest import MagicMock
import pytest import pytest
from withings_api import WithingsApi from withings_api import WithingsApi
...@@ -8,15 +10,20 @@ from homeassistant.components.withings.common import ( ...@@ -8,15 +10,20 @@ from homeassistant.components.withings.common import (
NotAuthenticatedError, NotAuthenticatedError,
WithingsDataManager, WithingsDataManager,
) )
from homeassistant.config import async_process_ha_core_config
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import PlatformNotReady from homeassistant.exceptions import PlatformNotReady
from homeassistant.util import dt
@pytest.fixture(name="withings_api") @pytest.fixture(name="withings_api")
def withings_api_fixture() -> WithingsApi: def withings_api_fixture() -> WithingsApi:
"""Provide withings api.""" """Provide withings api."""
withings_api = WithingsApi.__new__(WithingsApi) withings_api = WithingsApi.__new__(WithingsApi)
withings_api.get_measures = MagicMock() withings_api.user_get_device = MagicMock()
withings_api.get_sleep = MagicMock() withings_api.measure_get_meas = MagicMock()
withings_api.sleep_get = MagicMock()
withings_api.sleep_get_summary = MagicMock()
return withings_api return withings_api
...@@ -101,3 +108,27 @@ async def test_data_manager_call_throttle_disabled( ...@@ -101,3 +108,27 @@ async def test_data_manager_call_throttle_disabled(
assert result == "HELLO2" assert result == "HELLO2"
assert hello_func.call_count == 2 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment