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 Minute forecast action #128799

Draft
wants to merge 24 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
81c1239
Adding OpenWeatherMap Minutely forecast action
10100011 Oct 19, 2024
9026d16
Merge branch 'dev' into minutely-action
10100011 Oct 19, 2024
01debb8
Merge branch 'dev' into minutely-action
10100011 Oct 19, 2024
0b77ff1
Merge branch 'home-assistant:dev' into minutely-action
10100011 Oct 21, 2024
fdf3958
Actioning review comments
10100011 Oct 21, 2024
81a897b
Merge branch 'home-assistant:dev' into minutely-action
10100011 Oct 21, 2024
2b22638
Merge branch 'home-assistant:dev' into minutely-action
10100011 Oct 21, 2024
ede911f
Check for correct mode per action call
10100011 Oct 21, 2024
5e2a3dc
Merge branch 'home-assistant:dev' into minutely-action
10100011 Oct 21, 2024
bee6187
Merge branch 'home-assistant:dev' into minutely-action
10100011 Oct 22, 2024
956472e
Action may now be called per entity / location
10100011 Oct 22, 2024
b13d98b
Merge branch 'home-assistant:dev' into minutely-action
10100011 Oct 22, 2024
ebcfb96
Merge branch 'home-assistant:dev' into minutely-action
10100011 Oct 22, 2024
3686f66
v3.0 should provide greater and more relevant test coverage
10100011 Oct 22, 2024
edc0842
Prefer consistent strings
10100011 Oct 22, 2024
a547b7d
Additional test coverage
10100011 Oct 22, 2024
ff7f3ca
Merge branch 'home-assistant:dev' into minutely-action
10100011 Oct 22, 2024
012891d
Merge branch 'dev' into minutely-action
10100011 Oct 22, 2024
9884e22
Merge branch 'dev' into minutely-action
10100011 Oct 23, 2024
31f4364
Corrected return typing | Better use of MockConfigEntry
10100011 Oct 25, 2024
91bfaf7
Mocking only the HTTP request
10100011 Nov 1, 2024
1c1a0a3
Merge branch 'home-assistant:dev' into minutely-action
10100011 Dec 21, 2024
f4f7210
Merge branch 'home-assistant:dev' into minutely-action
10100011 Dec 22, 2024
19a9bf0
Merge branch 'home-assistant:dev' into minutely-action
10100011 Dec 22, 2024
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
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
21 changes: 21 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 @@ -27,10 +28,14 @@
ATTR_API_CONDITION,
ATTR_API_CURRENT,
ATTR_API_DAILY_FORECAST,
ATTR_API_DATETIME,
ATTR_API_DEW_POINT,
ATTR_API_FEELS_LIKE_TEMPERATURE,
ATTR_API_FORECAST,
ATTR_API_HOURLY_FORECAST,
ATTR_API_HUMIDITY,
ATTR_API_MINUTE_FORECAST,
ATTR_API_PRECIPITATION,
ATTR_API_PRECIPITATION_KIND,
ATTR_API_PRESSURE,
ATTR_API_RAIN,
Expand Down Expand Up @@ -94,6 +99,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 +114,17 @@ def _convert_weather_response(self, weather_report: WeatherReport):
],
}

def _get_minute_weather_data(self, minute_forecast: list[MinutelyWeatherForecast]):
return {
ATTR_API_FORECAST: [
{
ATTR_API_DATETIME: item.date_time,
ATTR_API_PRECIPITATION: round(item.precipitation, 2),
}
for item in minute_forecast
]
}

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_forecast": {
"service": "mdi:weather-snowy-rainy"
}
}
}
5 changes: 5 additions & 0 deletions homeassistant/components/openweathermap/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
get_minute_forecast:
target:
entity:
domain: weather
integration: openweathermap
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_forecast": {
"name": "Get minute forecast",
"description": "Get minute weather forecast."
}
},
"exceptions": {
"service_minute_forecast_mode": {
"message": "Minute forecast is available only when {name} mode is set to v3.0"
}
}
}
28 changes: 27 additions & 1 deletion homeassistant/components/openweathermap/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
UnitOfSpeed,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant, SupportsResponse, callback
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers import entity_platform
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback

Expand All @@ -28,6 +30,7 @@
ATTR_API_FEELS_LIKE_TEMPERATURE,
ATTR_API_HOURLY_FORECAST,
ATTR_API_HUMIDITY,
ATTR_API_MINUTE_FORECAST,
ATTR_API_PRESSURE,
ATTR_API_TEMPERATURE,
ATTR_API_VISIBILITY_DISTANCE,
Expand All @@ -44,6 +47,8 @@
)
from .coordinator import WeatherUpdateCoordinator

SERVICE_GET_MINUTE_FORECAST = "get_minute_forecast"


async def async_setup_entry(
hass: HomeAssistant,
Expand All @@ -61,6 +66,14 @@ async def async_setup_entry(

async_add_entities([owm_weather], False)

platform = entity_platform.async_get_current_platform()
platform.async_register_entity_service(
name=SERVICE_GET_MINUTE_FORECAST,
schema=None,
func="async_get_minute_forecast",
supports_response=SupportsResponse.ONLY,
)


class OpenWeatherMapWeather(SingleCoordinatorWeatherEntity[WeatherUpdateCoordinator]):
"""Implementation of an OpenWeatherMap sensor."""
Expand Down Expand Up @@ -91,6 +104,8 @@ def __init__(
manufacturer=MANUFACTURER,
name=DEFAULT_NAME,
)
self.mode = mode
self.weather_coordinator = weather_coordinator

if mode in (OWM_MODE_V30, OWM_MODE_V25):
self._attr_supported_features = (
Expand All @@ -100,6 +115,17 @@ def __init__(
elif mode == OWM_MODE_FREE_FORECAST:
self._attr_supported_features = WeatherEntityFeature.FORECAST_HOURLY

async def async_get_minute_forecast(self) -> dict[str, list[dict]] | dict:
"""Return Minute forecast."""

if self.mode == OWM_MODE_V30:
return self.weather_coordinator.data[ATTR_API_MINUTE_FORECAST]
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="service_minute_forecast_mode",
translation_placeholders={"name": DEFAULT_NAME},
)

@property
def condition(self) -> str | None:
"""Return the current condition."""
Expand Down
Loading