Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(weather::forecasts): add timezone to forecasts datetime #113

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions custom_components/yandex_weather/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.util.dt import get_time_zone

from .const import (
ATTR_API_CONDITION,
Expand Down Expand Up @@ -229,7 +230,7 @@ def get_timezone(nows: str, nowi: int) -> timezone:
microsecond=0
)
server_unix_time = datetime.fromtimestamp(nowi)
return timezone(server_utc_time - server_unix_time)
return timezone(server_unix_time - server_utc_time)

async def update(self):
"""Update weather information.
Expand All @@ -238,23 +239,23 @@ async def update(self):
"""
result = {}
timeout = aiohttp.ClientTimeout(total=20)
local_tz = get_time_zone(self.hass.config.time_zone)
async with aiohttp.ClientSession(timeout=timeout) as session:
response = await self.request(
session, self.__api_key, self._lat, self._lon, "en_US"
)
r = json.loads(response)

result = {
ATTR_API_WEATHER_TIME: datetime.fromtimestamp(
r["fact"][ATTR_API_WEATHER_TIME],
tz=self.get_timezone(r["now_dt"], r["now"]),
),
).astimezone(tz=local_tz),
ATTR_API_FORECAST_ICONS: [],
ATTR_FORECAST_DATA: [],
}
self.process_data(result, r["fact"], CURRENT_WEATHER_ATTRIBUTE_TRANSLATION)

f_datetime = datetime.utcnow()
f_datetime = datetime.now(tz=local_tz)
for f in r["forecast"]["parts"]:
f_datetime += timedelta(hours=24 / 4)
forecast = Forecast(datetime=f_datetime.isoformat())
Expand Down
Loading