open_meteo: correct UTC timezone handling in hourly forecast (#129664)

Co-authored-by: G Johansson <goran.johansson@shiftit.se>
This commit is contained in:
Andrea Arcangeli 2025-01-02 18:37:36 +00:00 committed by GitHub
parent 4e74d14beb
commit 25937d7868
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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")