diff --git a/homeassistant/components/openweathermap/weather.py b/homeassistant/components/openweathermap/weather.py index c6f95555954..bf1ae5ca7da 100644 --- a/homeassistant/components/openweathermap/weather.py +++ b/homeassistant/components/openweathermap/weather.py @@ -17,7 +17,8 @@ from homeassistant.components.weather import ( ATTR_FORECAST_TIME, ATTR_FORECAST_WIND_BEARING, Forecast, - WeatherEntity, + SingleCoordinatorWeatherEntity, + WeatherEntityFeature, ) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( @@ -26,10 +27,9 @@ from homeassistant.const import ( UnitOfSpeed, UnitOfTemperature, ) -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import ( ATTR_API_CLOUDS, @@ -60,6 +60,8 @@ from .const import ( DOMAIN, ENTRY_NAME, ENTRY_WEATHER_COORDINATOR, + FORECAST_MODE_DAILY, + FORECAST_MODE_ONECALL_DAILY, MANUFACTURER, ) from .weather_update_coordinator import WeatherUpdateCoordinator @@ -96,7 +98,7 @@ async def async_setup_entry( async_add_entities([owm_weather], False) -class OpenWeatherMapWeather(CoordinatorEntity[WeatherUpdateCoordinator], WeatherEntity): +class OpenWeatherMapWeather(SingleCoordinatorWeatherEntity[WeatherUpdateCoordinator]): """Implementation of an OpenWeatherMap sensor.""" _attr_attribution = ATTRIBUTION @@ -123,6 +125,13 @@ class OpenWeatherMapWeather(CoordinatorEntity[WeatherUpdateCoordinator], Weather manufacturer=MANUFACTURER, name=DEFAULT_NAME, ) + if weather_coordinator.forecast_mode in ( + FORECAST_MODE_DAILY, + FORECAST_MODE_ONECALL_DAILY, + ): + self._attr_supported_features = WeatherEntityFeature.FORECAST_DAILY + else: # FORECAST_MODE_DAILY or FORECAST_MODE_ONECALL_HOURLY + self._attr_supported_features = WeatherEntityFeature.FORECAST_HOURLY @property def condition(self) -> str | None: @@ -187,3 +196,13 @@ class OpenWeatherMapWeather(CoordinatorEntity[WeatherUpdateCoordinator], Weather for forecast in api_forecasts ] return cast(list[Forecast], forecasts) + + @callback + def _async_forecast_daily(self) -> list[Forecast] | None: + """Return the daily forecast in native units.""" + return self.forecast + + @callback + def _async_forecast_hourly(self) -> list[Forecast] | None: + """Return the hourly forecast in native units.""" + return self.forecast diff --git a/homeassistant/components/openweathermap/weather_update_coordinator.py b/homeassistant/components/openweathermap/weather_update_coordinator.py index cf0c941f0df..56519c46fd9 100644 --- a/homeassistant/components/openweathermap/weather_update_coordinator.py +++ b/homeassistant/components/openweathermap/weather_update_coordinator.py @@ -68,7 +68,7 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator): self._owm_client = owm self._latitude = latitude self._longitude = longitude - self._forecast_mode = forecast_mode + self.forecast_mode = forecast_mode self._forecast_limit = None if forecast_mode == FORECAST_MODE_DAILY: self._forecast_limit = 15 @@ -90,7 +90,7 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator): async def _get_owm_weather(self): """Poll weather data from OWM.""" - if self._forecast_mode in ( + if self.forecast_mode in ( FORECAST_MODE_ONECALL_HOURLY, FORECAST_MODE_ONECALL_DAILY, ): @@ -106,17 +106,17 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator): def _get_legacy_weather_and_forecast(self): """Get weather and forecast data from OWM.""" - interval = self._get_forecast_interval() + interval = self._get_legacy_forecast_interval() weather = self._owm_client.weather_at_coords(self._latitude, self._longitude) forecast = self._owm_client.forecast_at_coords( self._latitude, self._longitude, interval, self._forecast_limit ) return LegacyWeather(weather.weather, forecast.forecast.weathers) - def _get_forecast_interval(self): + def _get_legacy_forecast_interval(self): """Get the correct forecast interval depending on the forecast mode.""" interval = "daily" - if self._forecast_mode == FORECAST_MODE_HOURLY: + if self.forecast_mode == FORECAST_MODE_HOURLY: interval = "3h" return interval @@ -153,9 +153,9 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator): def _get_forecast_from_weather_response(self, weather_response): """Extract the forecast data from the weather response.""" forecast_arg = "forecast" - if self._forecast_mode == FORECAST_MODE_ONECALL_HOURLY: + if self.forecast_mode == FORECAST_MODE_ONECALL_HOURLY: forecast_arg = "forecast_hourly" - elif self._forecast_mode == FORECAST_MODE_ONECALL_DAILY: + elif self.forecast_mode == FORECAST_MODE_ONECALL_DAILY: forecast_arg = "forecast_daily" return [ self._convert_forecast(x) for x in getattr(weather_response, forecast_arg)