Compare commits

...

2 Commits

Author SHA1 Message Date
G Johansson
b50cf12df4 Fixes 2024-12-29 14:30:26 +00:00
Andrea Arcangeli
b3bdf80eb2 open_meteo: correct UTC timezone handling in hourly forecast
The `datetime` variable is timezone-naive. Using
`dt_util.as_utc(datetime)` incorrectly assigns the local timezone
instead of UTC, leading to incorrect conversions. To fix this,
directly assign the UTC timezone to the `datetime` variable before
conversion, ensuring `dt_util.as_utc()` does not overwrite it with the
local timezone.
2024-12-29 14:30:26 +00:00
3 changed files with 1125 additions and 4 deletions

View File

@@ -2,6 +2,8 @@
from __future__ import annotations
from datetime import datetime, time
from open_meteo import Forecast as OpenMeteoForecast
from homeassistant.components.weather import (
@@ -107,8 +109,9 @@ class OpenMeteoWeatherEntity(
daily = self.coordinator.data.daily
for index, date in enumerate(self.coordinator.data.daily.time):
_datetime = datetime.combine(date=date, time=time(0), tzinfo=dt_util.UTC)
forecast = Forecast(
datetime=date.isoformat(),
datetime=_datetime.isoformat(),
)
if daily.weathercode is not None:
@@ -155,12 +158,14 @@ class OpenMeteoWeatherEntity(
today = dt_util.utcnow()
hourly = self.coordinator.data.hourly
for index, datetime in enumerate(self.coordinator.data.hourly.time):
if dt_util.as_utc(datetime) < today:
for index, _datetime in enumerate(self.coordinator.data.hourly.time):
if _datetime.tzinfo is None:
_datetime = _datetime.replace(tzinfo=dt_util.UTC)
if _datetime < today:
continue
forecast = Forecast(
datetime=datetime.isoformat(),
datetime=_datetime.isoformat(),
)
if hourly.weather_code is not None:

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,46 @@
"""Test for the open meteo weather entity."""
from unittest.mock import AsyncMock
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.weather import (
DOMAIN as WEATHER_DOMAIN,
SERVICE_GET_FORECASTS,
)
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
@pytest.mark.freeze_time("2021-11-24T03:00:00+00:00")
async def test_forecast_service(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_open_meteo: AsyncMock,
snapshot: SnapshotAssertion,
) -> None:
"""Test forecast service."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
response = await hass.services.async_call(
WEATHER_DOMAIN,
SERVICE_GET_FORECASTS,
{ATTR_ENTITY_ID: "weather.home", "type": "daily"},
blocking=True,
return_response=True,
)
assert response == snapshot(name="forecast_daily")
response = await hass.services.async_call(
WEATHER_DOMAIN,
SERVICE_GET_FORECASTS,
{ATTR_ENTITY_ID: "weather.home", "type": "hourly"},
blocking=True,
return_response=True,
)
assert response == snapshot(name="forecast_hourly")