Skip to content

Commit

Permalink
Fix timezome history comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
masaccio committed Jun 25, 2023
1 parent 770ecbc commit 84d7c3f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
23 changes: 15 additions & 8 deletions custom_components/kingspan_watchman_sensit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

Expand All @@ -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
6 changes: 2 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
HistoryType,
)

LOCAL_TZINFO = datetime.now(timezone.utc).astimezone().tzinfo

pytest_plugins = "pytest_homeassistant_custom_component"


Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 84d7c3f

Please sign in to comment.