From 84d7c3f8bd44263c5b2d2ac3375f867824def831 Mon Sep 17 00:00:00 2001 From: Jon Connell Date: Sun, 25 Jun 2023 10:51:50 +0100 Subject: [PATCH] Fix timezome history comparison --- .../kingspan_watchman_sensit/api.py | 23 ++++++++++++------- tests/conftest.py | 6 ++--- tests/test_sensor.py | 2 +- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/custom_components/kingspan_watchman_sensit/api.py b/custom_components/kingspan_watchman_sensit/api.py index 42d0d31..753f9ce 100644 --- a/custom_components/kingspan_watchman_sensit/api.py +++ b/custom_components/kingspan_watchman_sensit/api.py @@ -75,10 +75,7 @@ async def _get_tank_data(self): return self.data def usage_rate(self, tank_data: TankData): - time_delta = datetime.today() - timedelta(days=USAGE_WINDOW) - time_delta = time_delta.replace(tzinfo=LOCAL_TZINFO) - history = tank_data.history - history = [x for x in history if x["reading_date"] >= time_delta] + history = filter_history(tank_data.history) if len(history) == 0: return 0 @@ -100,10 +97,7 @@ def usage_rate(self, tank_data: TankData): return 0 def forecast_empty(self, tank_data: TankData): - time_delta = datetime.today() - timedelta(days=USAGE_WINDOW) - time_delta = time_delta.replace(tzinfo=LOCAL_TZINFO) - history = tank_data.history - history = [x for x in history if x["reading_date"] >= time_delta] + history = filter_history(tank_data.history) if len(history) == 0: return 0 @@ -114,3 +108,16 @@ def forecast_empty(self, tank_data: TankData): else: current_level = int(history[-1]["level_litres"]) return int(current_level / abs(rate)) + + +def filter_history(history: list[dict]) -> list[dict]: + """Filter tank history to a smaller recent window of days""" + time_delta = datetime.today() - timedelta(days=USAGE_WINDOW) + time_delta = time_delta.replace(tzinfo=LOCAL_TZINFO) + # API returns naive datetime rather than with timezones + history = [ + dict(x, reading_date=x["reading_date"].replace(tzinfo=LOCAL_TZINFO)) + for x in history + ] + history = [x for x in history if x["reading_date"] >= time_delta] + return history diff --git a/tests/conftest.py b/tests/conftest.py index 6878ef6..1712f5b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -16,8 +16,6 @@ HistoryType, ) -LOCAL_TZINFO = datetime.now(timezone.utc).astimezone().tzinfo - pytest_plugins = "pytest_homeassistant_custom_component" @@ -80,7 +78,7 @@ def error_sensor_client_fixture(): def decreasing_history(start_date: datetime) -> list: history = [] start_date = start_date.replace( - hour=0, minute=30, second=0, microsecond=0, tzinfo=LOCAL_TZINFO + hour=0, minute=30, second=0, microsecond=0 ) - timedelta(days=30) for day in range(1, 20): @@ -156,7 +154,7 @@ async def last_read(self) -> str: if len(history) > 0: return history[-1]["reading_date"] else: - return datetime.now().replace(tzinfo=LOCAL_TZINFO) + return datetime.now() @async_property async def history(self) -> str: diff --git a/tests/test_sensor.py b/tests/test_sensor.py index beba301..734a4e0 100644 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -38,7 +38,7 @@ async def test_sensor(hass, mock_sensor_client): state = hass.states.get("sensor.last_reading_date") assert state test_date = datetime.now().replace(hour=0, minute=30, second=0, microsecond=0) - # Timestamps from HA are in UTC + # Timestamps from HA are in UTC`` test_date = test_date.astimezone(timezone.utc) assert state.state == test_date.isoformat() assert state.attributes.get(ATTR_ICON) == "mdi:clock-outline"