Skip to content
Merged
Show file tree
Hide file tree
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
22 changes: 21 additions & 1 deletion homeassistant/components/open_meteo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
"""Support for Open-Meteo."""
from __future__ import annotations

from open_meteo import Forecast, OpenMeteo, OpenMeteoError
from open_meteo import (
DailyParameters,
Forecast,
OpenMeteo,
OpenMeteoError,
PrecipitationUnit,
TemperatureUnit,
WindSpeedUnit,
)

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE, CONF_ZONE, Platform
Expand Down Expand Up @@ -29,6 +37,18 @@ async def async_update_forecast() -> Forecast:
latitude=zone.attributes[ATTR_LATITUDE],
longitude=zone.attributes[ATTR_LONGITUDE],
current_weather=True,
daily=[
DailyParameters.PRECIPITATION_SUM,
DailyParameters.TEMPERATURE_2M_MAX,
DailyParameters.TEMPERATURE_2M_MIN,
DailyParameters.WEATHER_CODE,
DailyParameters.WIND_DIRECTION_10M_DOMINANT,
DailyParameters.WIND_SPEED_10M_MAX,
],
precipitation_unit=PrecipitationUnit.MILLIMETERS,
temperature_unit=TemperatureUnit.CELSIUS,
timezone="UTC",
wind_speed_unit=WindSpeedUnit.KILOMETERS_PER_HOUR,
)
except OpenMeteoError as err:
raise UpdateFailed("Open-Meteo API communication error") from err
Expand Down
40 changes: 39 additions & 1 deletion homeassistant/components/open_meteo/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from open_meteo import Forecast as OpenMeteoForecast

from homeassistant.components.weather import WeatherEntity
from homeassistant.components.weather import Forecast, WeatherEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import TEMP_CELSIUS
from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -78,3 +78,41 @@ def wind_bearing(self) -> float | str | None:
if not self.coordinator.data.current_weather:
return None
return self.coordinator.data.current_weather.wind_direction

@property
def forecast(self) -> list[Forecast] | None:
"""Return the forecast in native units."""
if self.coordinator.data.daily is None:
return None

forecasts: list[Forecast] = []
daily = self.coordinator.data.daily
for index, time in enumerate(self.coordinator.data.daily.time):

forecast = Forecast(
datetime=time.isoformat(),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do timezones need to be accounted for here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It already has been accounted for, see the initialization in the unit file

)

if daily.weathercode is not None:
forecast["condition"] = WMO_TO_HA_CONDITION_MAP.get(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is much benefit from that, as it already is a typed dictionary.
The reason we used constants in the past, is mostly to reduce typos, which are now already guarded.

daily.weathercode[index]
)

if daily.precipitation_sum is not None:
forecast["precipitation"] = daily.precipitation_sum[index]

if daily.temperature_2m_max is not None:
forecast["temperature"] = daily.temperature_2m_max[index]

if daily.temperature_2m_min is not None:
forecast["templow"] = daily.temperature_2m_min[index]

if daily.wind_direction_10m_dominant is not None:
forecast["wind_bearing"] = daily.wind_direction_10m_dominant[index]

if daily.wind_speed_10m_max is not None:
forecast["wind_speed"] = daily.wind_speed_10m_max[index]

forecasts.append(forecast)

return forecasts