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

Conversation

10100011
Copy link
Contributor

@10100011 10100011 commented Oct 19, 2024

Proposed change

This PR adds functionality to the OpenWeatherMap integration to expose Minutely foreasts as an Action. Minutely forecasts compliment Daily and Hourly forecasts by providing precipitation (rain or snow) levels for each minute of the next hour.

Instead of creating 60 separate sensors, or even 60 state attributes of a single sensor, this PR creates a new Service accessible as an OpenWeatherMap Action. It returns a response in the same format as weather.get_forecasts, with 60 objects containing a datetime in UTC (starting now) and precipitation in mm/h.

Key changes:

  • Upgrade to pyopenweathermap v0.2.1 library (comparison), which added Minutely functionality
  • New Service handle_get_minutely_forecast in services.py
  • Extraction of existing minutely_forecast data in coordinator.py (no additional API data is requested)

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@10100011 10100011 requested a review from fabaff as a code owner October 19, 2024 18:51
Copy link

@home-assistant home-assistant bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @10100011

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant home-assistant bot marked this pull request as draft October 19, 2024 18:51
@home-assistant
Copy link

Hey there @fabaff, @freekode, @nzapponi, mind taking a look at this pull request as it has been labeled with an integration (openweathermap) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of openweathermap can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign openweathermap Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

Copy link

@home-assistant home-assistant bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @10100011

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

Copy link
Contributor

@bouwew bouwew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is Minutely a proper word?

Better to use the OpenWeatherMap lingo: Minute forecast.

I now see that they use minutely as a parameter name. It's fine it use that word in the code.
But I suggest using Minute in texts that a User will see.

@@ -104,6 +111,14 @@ def _convert_weather_response(self, weather_report: WeatherReport):
],
}

def _get_minutely_weather_data(self, minutely_forecast):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add typing to the function?

for item in minutely_forecast
]

return {f"{Platform.WEATHER}.{DOMAIN}": {"forecast": forecasts}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how much this adds if its static

return round(precipitation["3h"], 2)
if "1h" in precipitation:
return round(precipitation["1h"], 2)
if precipitation is not None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would it be none?

Copy link
Contributor Author

@10100011 10100011 Oct 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pyopenweathermap library >0.2.0 seems to have introduced the possibility of None values being included for rain and snow: wind_gust=14.92, wind_bearing=140, rain=None, snow=None.

I'll raise, and possibly fix, the issue on pyopenweathermap.

Edit: rain and snow only appear in the response from the API if they are due / above zero, which appears to cause the None. Previously, they were empty dictionaries.

@@ -6,5 +6,5 @@
"documentation": "https://www.home-assistant.io/integrations/openweathermap",
"iot_class": "cloud_polling",
"loggers": ["pyopenweathermap"],
"requirements": ["pyopenweathermap==0.1.1"]
"requirements": ["pyopenweathermap==0.2.1"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we bump this in a separate preliminary PR?

Copy link
Contributor Author

@10100011 10100011 Oct 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do, pending resolution of the comment above (None values) as it may require a new version.

Comment on lines 24 to 35
if mode == OWM_MODE_V30:
hass.services.async_register(
domain=DOMAIN,
service=SERVICE_GET_MINUTELY_FORECAST,
service_func=handle_get_minutely_forecasts,
supports_response=SupportsResponse.ONLY,
)
else:
hass.services.async_remove(
domain=DOMAIN,
service=SERVICE_GET_MINUTELY_FORECAST,
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Services should be registered in async_setup and should not be removed on runtime

Copy link
Contributor Author

@10100011 10100011 Oct 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only called when the user makes a change to the integration configuration (Settings -> Devices & Services -> OpenWeatherMap -> Configure) and changes the forecast mode of the integration to a mode which does not support Minute forecasting. As such, this isn't really on runtime: it's only when the user reconfigures the integration without first deleting it.

If the service isn't removed when using an unsupported mode, the user will simply be returned an empty forecast - with no feedback as to the reason. I would argue that is worse UX.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the mode of the user is incorrect, you can raise a ServiceValidationError, explaining why it failed. More info about this can be found for now in https://github.com/home-assistant/developers.home-assistant/pull/2400/files

@@ -0,0 +1 @@
get_minutely_forecasts:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to add a location or anything? What if I have OWM setup twice?

@home-assistant home-assistant bot marked this pull request as draft October 20, 2024 13:27
@10100011
Copy link
Contributor Author

Is Minutely a proper word?

Better to use the OpenWeatherMap lingo: Minute forecast.

I now see that they use minutely as a parameter name. It's fine it use that word in the code. But I suggest using Minute in texts that a User will see.

Agreed. I had thought that it fitted neatly with "daily" and "hourly", but you're right: it does not mean "minute-by-minute". Will change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants