1 """Weather data coordinator for the OpenWeatherMap (OWM) service."""
3 from datetime
import timedelta
6 from pyopenweathermap
import (
16 ATTR_CONDITION_CLEAR_NIGHT,
29 ATTR_API_DAILY_FORECAST,
31 ATTR_API_FEELS_LIKE_TEMPERATURE,
32 ATTR_API_HOURLY_FORECAST,
34 ATTR_API_PRECIPITATION_KIND,
40 ATTR_API_VISIBILITY_DISTANCE,
42 ATTR_API_WEATHER_CODE,
43 ATTR_API_WIND_BEARING,
48 WEATHER_CODE_SUNNY_OR_CLEAR_NIGHT,
51 _LOGGER = logging.getLogger(__name__)
57 """Weather data update coordinator."""
61 owm_client: OWMClient,
66 """Initialize coordinator."""
72 hass, _LOGGER, name=DOMAIN, update_interval=WEATHER_UPDATE_INTERVAL
76 """Update the data."""
78 weather_report = await self.
_owm_client_owm_client.get_weather(
81 except RequestError
as error:
86 """Format the weather response correctly."""
87 _LOGGER.debug(
"OWM weather response: %s", weather_report)
91 if weather_report.current
is not None
96 ATTR_API_CURRENT: current_weather,
97 ATTR_API_HOURLY_FORECAST: [
99 for item
in weather_report.hourly_forecast
101 ATTR_API_DAILY_FORECAST: [
103 for item
in weather_report.daily_forecast
109 ATTR_API_CONDITION: self.
_get_condition_get_condition(current_weather.condition.id),
110 ATTR_API_TEMPERATURE: current_weather.temperature,
111 ATTR_API_FEELS_LIKE_TEMPERATURE: current_weather.feels_like,
112 ATTR_API_PRESSURE: current_weather.pressure,
113 ATTR_API_HUMIDITY: current_weather.humidity,
114 ATTR_API_DEW_POINT: current_weather.dew_point,
115 ATTR_API_CLOUDS: current_weather.cloud_coverage,
116 ATTR_API_WIND_SPEED: current_weather.wind_speed,
117 ATTR_API_WIND_GUST: current_weather.wind_gust,
118 ATTR_API_WIND_BEARING: current_weather.wind_bearing,
119 ATTR_API_WEATHER: current_weather.condition.description,
120 ATTR_API_WEATHER_CODE: current_weather.condition.id,
121 ATTR_API_UV_INDEX: current_weather.uv_index,
122 ATTR_API_VISIBILITY_DISTANCE: current_weather.visibility,
126 current_weather.rain, current_weather.snow
131 uv_index =
float(forecast.uv_index)
if forecast.uv_index
is not None else None
134 datetime=forecast.date_time.isoformat(),
135 condition=self.
_get_condition_get_condition(forecast.condition.id),
136 temperature=forecast.temperature,
137 native_apparent_temperature=forecast.feels_like,
138 pressure=forecast.pressure,
139 humidity=forecast.humidity,
140 native_dew_point=forecast.dew_point,
141 cloud_coverage=forecast.cloud_coverage,
142 wind_speed=forecast.wind_speed,
143 native_wind_gust_speed=forecast.wind_gust,
144 wind_bearing=forecast.wind_bearing,
146 precipitation_probability=round(forecast.precipitation_probability * 100),
151 uv_index =
float(forecast.uv_index)
if forecast.uv_index
is not None else None
154 datetime=forecast.date_time.isoformat(),
155 condition=self.
_get_condition_get_condition(forecast.condition.id),
156 temperature=forecast.temperature.max,
157 templow=forecast.temperature.min,
158 native_apparent_temperature=forecast.feels_like,
159 pressure=forecast.pressure,
160 humidity=forecast.humidity,
161 native_dew_point=forecast.dew_point,
162 cloud_coverage=forecast.cloud_coverage,
163 wind_speed=forecast.wind_speed,
164 native_wind_gust_speed=forecast.wind_gust,
165 wind_bearing=forecast.wind_bearing,
167 precipitation_probability=round(forecast.precipitation_probability * 100),
168 precipitation=round(forecast.rain + forecast.snow, 2),
173 """Calculate the precipitation."""
174 rain_value = WeatherUpdateCoordinator._get_precipitation_value(rain)
175 snow_value = WeatherUpdateCoordinator._get_precipitation_value(snow)
176 return round(rain_value + snow_value, 2)
180 """Determine the precipitation kind."""
181 rain_value = WeatherUpdateCoordinator._get_precipitation_value(rain)
182 snow_value = WeatherUpdateCoordinator._get_precipitation_value(snow)
185 return "Snow and Rain"
194 """Get precipitation value from weather data."""
195 if precipitation
is not None:
196 if "all" in precipitation:
197 return round(precipitation[
"all"], 2)
198 if "3h" in precipitation:
199 return round(precipitation[
"3h"], 2)
200 if "1h" in precipitation:
201 return round(precipitation[
"1h"], 2)
205 """Get weather condition from weather data."""
206 if weather_code == WEATHER_CODE_SUNNY_OR_CLEAR_NIGHT:
208 timestamp = dt_util.utc_from_timestamp(timestamp)
210 if sun.is_up(self.
hasshass, timestamp):
211 return ATTR_CONDITION_SUNNY
212 return ATTR_CONDITION_CLEAR_NIGHT
214 return CONDITION_MAP.get(weather_code)
def _get_current_weather_data(self, CurrentWeather current_weather)
def _get_daily_forecast_weather_data(self, DailyWeatherForecast forecast)
def _get_hourly_forecast_weather_data(self, HourlyWeatherForecast forecast)
def _calc_precipitation_kind(rain, snow)
def _async_update_data(self)
def _get_precipitation_value(precipitation)
None __init__(self, OWMClient owm_client, latitude, longitude, HomeAssistant hass)
def _calc_precipitation(rain, snow)
def _get_condition(self, weather_code, timestamp=None)
def _convert_weather_response(self, WeatherReport weather_report)