1 """Support for Open-Meteo weather."""
3 from __future__
import annotations
5 from open_meteo
import Forecast
as OpenMeteoForecast
8 ATTR_FORECAST_CONDITION,
9 ATTR_FORECAST_NATIVE_PRECIPITATION,
10 ATTR_FORECAST_NATIVE_TEMP,
11 ATTR_FORECAST_NATIVE_TEMP_LOW,
12 ATTR_FORECAST_NATIVE_WIND_SPEED,
13 ATTR_FORECAST_WIND_BEARING,
15 SingleCoordinatorWeatherEntity,
26 from .const
import DOMAIN, WMO_TO_HA_CONDITION_MAP
32 async_add_entities: AddEntitiesCallback,
34 """Set up Open-Meteo weather entity based on a config entry."""
35 coordinator = hass.data[DOMAIN][entry.entry_id]
40 SingleCoordinatorWeatherEntity[DataUpdateCoordinator[OpenMeteoForecast]]
42 """Defines an Open-Meteo weather entity."""
44 _attr_has_entity_name =
True
46 _attr_native_precipitation_unit = UnitOfPrecipitationDepth.MILLIMETERS
47 _attr_native_temperature_unit = UnitOfTemperature.CELSIUS
48 _attr_native_wind_speed_unit = UnitOfSpeed.KILOMETERS_PER_HOUR
49 _attr_supported_features = (
50 WeatherEntityFeature.FORECAST_DAILY | WeatherEntityFeature.FORECAST_HOURLY
57 coordinator: DataUpdateCoordinator[OpenMeteoForecast],
59 """Initialize Open-Meteo weather entity."""
60 super().
__init__(coordinator=coordinator)
64 entry_type=DeviceEntryType.SERVICE,
65 identifiers={(DOMAIN, entry.entry_id)},
66 manufacturer=
"Open-Meteo",
72 """Return the current condition."""
73 if not self.coordinator.data.current_weather:
75 return WMO_TO_HA_CONDITION_MAP.get(
76 self.coordinator.data.current_weather.weather_code
81 """Return the platform temperature."""
82 if not self.coordinator.data.current_weather:
84 return self.coordinator.data.current_weather.temperature
88 """Return the wind speed."""
89 if not self.coordinator.data.current_weather:
91 return self.coordinator.data.current_weather.wind_speed
95 """Return the wind bearing."""
96 if not self.coordinator.data.current_weather:
98 return self.coordinator.data.current_weather.wind_direction
102 """Return the daily forecast in native units."""
103 if self.coordinator.data.daily
is None:
106 forecasts: list[Forecast] = []
108 daily = self.coordinator.data.daily
109 for index, date
in enumerate(self.coordinator.data.daily.time):
111 datetime=date.isoformat(),
114 if daily.weathercode
is not None:
115 forecast[ATTR_FORECAST_CONDITION] = WMO_TO_HA_CONDITION_MAP.get(
116 daily.weathercode[index]
119 if daily.precipitation_sum
is not None:
120 forecast[ATTR_FORECAST_NATIVE_PRECIPITATION] = daily.precipitation_sum[
124 if daily.temperature_2m_max
is not None:
125 forecast[ATTR_FORECAST_NATIVE_TEMP] = daily.temperature_2m_max[index]
127 if daily.temperature_2m_min
is not None:
128 forecast[ATTR_FORECAST_NATIVE_TEMP_LOW] = daily.temperature_2m_min[
132 if daily.wind_direction_10m_dominant
is not None:
133 forecast[ATTR_FORECAST_WIND_BEARING] = (
134 daily.wind_direction_10m_dominant[index]
137 if daily.wind_speed_10m_max
is not None:
138 forecast[ATTR_FORECAST_NATIVE_WIND_SPEED] = daily.wind_speed_10m_max[
142 forecasts.append(forecast)
148 """Return the daily forecast in native units."""
149 if self.coordinator.data.hourly
is None:
152 forecasts: list[Forecast] = []
155 today = dt_util.utcnow()
157 hourly = self.coordinator.data.hourly
158 for index, datetime
in enumerate(self.coordinator.data.hourly.time):
159 if dt_util.as_utc(datetime) < today:
163 datetime=datetime.isoformat(),
166 if hourly.weather_code
is not None:
167 forecast[ATTR_FORECAST_CONDITION] = WMO_TO_HA_CONDITION_MAP.get(
168 hourly.weather_code[index]
171 if hourly.precipitation
is not None:
172 forecast[ATTR_FORECAST_NATIVE_PRECIPITATION] = hourly.precipitation[
176 if hourly.temperature_2m
is not None:
177 forecast[ATTR_FORECAST_NATIVE_TEMP] = hourly.temperature_2m[index]
179 forecasts.append(forecast)
list[Forecast]|None _async_forecast_hourly(self)
float|None native_wind_speed(self)
list[Forecast]|None _async_forecast_daily(self)
float|None native_temperature(self)
float|str|None wind_bearing(self)
None __init__(self, *ConfigEntry entry, DataUpdateCoordinator[OpenMeteoForecast] coordinator)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)