Fix lacrosse_view fetching of latest data (#85117)

lacrosse_view: fixed fetching of latest data

When using datetime.utcnow(), it only replaces timezone information with
UTC making the actual time offset by the timezone. When you are in UTC-
timezones, it makes no issue as the offset is in the future, but when in
UTC+, the last hour(s) of data are missing.

This commits swtiches to time.time() as UTC timestamp is actually what
the API expects.

It also reduces the window to one hour what noticeably improves the API
performance.
This commit is contained in:
Michal Čihař 2023-01-05 23:45:29 +01:00 committed by Paulus Schoutsen
parent dcd07d3135
commit fe89b663e7

View File

@ -1,7 +1,8 @@
"""DataUpdateCoordinator for LaCrosse View."""
from __future__ import annotations
from datetime import datetime, timedelta
from datetime import timedelta
from time import time
from lacrosse_view import HTTPError, LaCrosse, Location, LoginError, Sensor
@ -30,7 +31,7 @@ class LaCrosseUpdateCoordinator(DataUpdateCoordinator[list[Sensor]]):
) -> None:
"""Initialize DataUpdateCoordinator for LaCrosse View."""
self.api = api
self.last_update = datetime.utcnow()
self.last_update = time()
self.username = entry.data["username"]
self.password = entry.data["password"]
self.hass = hass
@ -45,26 +46,22 @@ class LaCrosseUpdateCoordinator(DataUpdateCoordinator[list[Sensor]]):
async def _async_update_data(self) -> list[Sensor]:
"""Get the data for LaCrosse View."""
now = datetime.utcnow()
now = int(time())
if self.last_update < now - timedelta(minutes=59): # Get new token
if self.last_update < now - 59 * 60: # Get new token once in a hour
self.last_update = now
try:
await self.api.login(self.username, self.password)
except LoginError as error:
raise ConfigEntryAuthFailed from error
# Get the timestamp for yesterday at 6 PM (this is what is used in the app, i noticed it when proxying the request)
yesterday = now - timedelta(days=1)
yesterday = yesterday.replace(hour=18, minute=0, second=0, microsecond=0)
yesterday_timestamp = datetime.timestamp(yesterday)
try:
# Fetch last hour of data
sensors = await self.api.get_sensors(
location=Location(id=self.id, name=self.name),
tz=self.hass.config.time_zone,
start=str(int(yesterday_timestamp)),
end=str(int(datetime.timestamp(now))),
start=str(now - 3600),
end=str(now),
)
except HTTPError as error:
raise ConfigEntryNotReady from error