Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding OpenWeatherMap Minutely forecast action #128799

Draft
wants to merge 9 commits into
base: dev
Choose a base branch
from
3 changes: 3 additions & 0 deletions homeassistant/components/openweathermap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .const import CONFIG_FLOW_VERSION, OWM_MODE_V25, PLATFORMS
from .coordinator import WeatherUpdateCoordinator
from .repairs import async_create_issue, async_delete_issue
from .services import async_setup_services
from .utils import build_data_and_options

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -66,6 +67,8 @@ async def async_setup_entry(

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

await async_setup_services(hass, mode, weather_coordinator)

return True


Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/openweathermap/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
ATTR_API_CLOUD_COVERAGE = "cloud_coverage"
ATTR_API_FORECAST = "forecast"
ATTR_API_CURRENT = "current"
ATTR_API_MINUTE_FORECAST = "minute_forecast"
ATTR_API_HOURLY_FORECAST = "hourly_forecast"
ATTR_API_DAILY_FORECAST = "daily_forecast"
UPDATE_LISTENER = "update_listener"
Expand Down
16 changes: 16 additions & 0 deletions homeassistant/components/openweathermap/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
CurrentWeather,
DailyWeatherForecast,
HourlyWeatherForecast,
MinutelyWeatherForecast,
OWMClient,
RequestError,
WeatherReport,
Expand All @@ -17,6 +18,7 @@
ATTR_CONDITION_SUNNY,
Forecast,
)
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import sun
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
Expand All @@ -31,6 +33,7 @@
ATTR_API_FEELS_LIKE_TEMPERATURE,
ATTR_API_HOURLY_FORECAST,
ATTR_API_HUMIDITY,
ATTR_API_MINUTE_FORECAST,
ATTR_API_PRECIPITATION_KIND,
ATTR_API_PRESSURE,
ATTR_API_RAIN,
Expand Down Expand Up @@ -94,6 +97,11 @@ def _convert_weather_response(self, weather_report: WeatherReport):

return {
ATTR_API_CURRENT: current_weather,
ATTR_API_MINUTE_FORECAST: (
self._get_minute_weather_data(weather_report.minutely_forecast)
if weather_report.minutely_forecast is not None
else {}
),
ATTR_API_HOURLY_FORECAST: [
self._get_hourly_forecast_weather_data(item)
for item in weather_report.hourly_forecast
Expand All @@ -104,6 +112,14 @@ def _convert_weather_response(self, weather_report: WeatherReport):
],
}

def _get_minute_weather_data(self, minute_forecast: list[MinutelyWeatherForecast]):
forecasts = [
{"datetime": item.date_time, "precipitation": round(item.precipitation, 2)}
for item in minute_forecast
]

return {Platform.WEATHER + "." + DOMAIN: {"forecast": forecasts}}

def _get_current_weather_data(self, current_weather: CurrentWeather):
return {
ATTR_API_CONDITION: self._get_condition(current_weather.condition.id),
Expand Down
7 changes: 7 additions & 0 deletions homeassistant/components/openweathermap/icons.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"services": {
"get_minute_forecasts": {
"service": "mdi:weather-snowy-rainy"
}
}
}
36 changes: 36 additions & 0 deletions homeassistant/components/openweathermap/services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Services for OpenWeatherMap."""

from __future__ import annotations

from homeassistant.core import HomeAssistant, ServiceCall, SupportsResponse
from homeassistant.exceptions import ServiceValidationError

from . import WeatherUpdateCoordinator
from .const import ATTR_API_MINUTE_FORECAST, DEFAULT_NAME, DOMAIN, OWM_MODE_V30

SERVICE_GET_MINUTE_FORECAST = f"get_{ATTR_API_MINUTE_FORECAST}"


async def async_setup_services(
hass: HomeAssistant,
mode: str,
weather_coordinator: WeatherUpdateCoordinator,
) -> None:
"""Set up OpenWeatherMap services."""

def handle_get_minute_forecasts(call: ServiceCall) -> None:
"""Handle the service action call."""
if mode == OWM_MODE_V30:
return weather_coordinator.data[ATTR_API_MINUTE_FORECAST]
raise ServiceValidationError(

Check warning on line 25 in homeassistant/components/openweathermap/services.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/openweathermap/services.py#L23-L25

Added lines #L23 - L25 were not covered by tests
translation_domain=DOMAIN,
translation_key="service_minute_forecast_mode",
translation_placeholders={"name": DEFAULT_NAME},
)

hass.services.async_register(
domain=DOMAIN,
service=SERVICE_GET_MINUTE_FORECAST,
service_func=handle_get_minute_forecasts,
supports_response=SupportsResponse.ONLY,
)
1 change: 1 addition & 0 deletions homeassistant/components/openweathermap/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
get_minute_forecasts:
11 changes: 11 additions & 0 deletions homeassistant/components/openweathermap/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,16 @@
}
}
}
},
"services": {
"get_minute_forecasts": {
"name": "Get minute forecasts",
"description": "Get minute weather forecasts."
}
},
"exceptions": {
"service_minute_forecast_mode": {
"message": "Minute forecasts are available only when {name} mode is set to v3.0"
}
}
}