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

Conversation

10100011
Copy link
Contributor

@10100011 10100011 commented Oct 19, 2024

Proposed change

This PR adds functionality to the OpenWeatherMap integration to expose Minute foreasts as an Action. Minute 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 Minute functionality
  • New Service async_get_minute_forecast in weather.py
  • Extraction of existing minute_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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Largely rewritten, based on the requirement for an OpenWeatherMap weather entity as action target.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Split into separate PR

@@ -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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Split into separate PR

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed. Mode now checked on a per-call, not per registration, basis. ServiceValidationError raised if mode is not v3.0

@@ -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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Very good point. Action now requires an OpenWeatherMap weather entity as a target, and location is derived from there.

@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.

@10100011 10100011 changed the title Adding OpenWeatherMap Minutely forecast action Adding OpenWeatherMap Minute forecast action Oct 22, 2024
@10100011 10100011 marked this pull request as ready for review October 22, 2024 18:38
@home-assistant home-assistant bot requested a review from joostlek October 22, 2024 18:38
@10100011 10100011 requested a review from bouwew October 24, 2024 14:25
@@ -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) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

return typing is incorrect, we do return something

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Return typing corrected

Comment on lines 30 to 79
@pytest.fixture
def mock_hass():
"""Create a mock HomeAssistant instance."""
return MagicMock(spec=HomeAssistant)


@pytest.fixture
def mock_coordinator():
"""Create a mock WeatherUpdateCoordinator."""
coordinator = MagicMock(spec=WeatherUpdateCoordinator)
coordinator.data = MOCK_WEATHER_DATA
return coordinator


@pytest.fixture
def mock_config_entry():
"""Create a mock OpenweathermapConfigEntry."""
config_entry = MagicMock()
config_entry.unique_id = MOCK_UNIQUE_ID
config_entry.runtime_data = MagicMock()
config_entry.runtime_data.name = MOCK_NAME
config_entry.runtime_data.mode = OWM_MODE_V30
config_entry.runtime_data.coordinator = MagicMock(spec=WeatherUpdateCoordinator)
config_entry.runtime_data.coordinator.data = MOCK_WEATHER_DATA
return config_entry


@pytest.fixture
def mock_add_entities():
"""Create a mock AddEntitiesCallback."""
return MagicMock(spec=AddEntitiesCallback)


async def test_async_get_minute_forecast(mock_coordinator) -> None:
"""Test the async_get_minute_forecast method."""
weather = OpenWeatherMapWeather(
name=MOCK_NAME,
unique_id=MOCK_UNIQUE_ID,
mode=OWM_MODE_V30,
weather_coordinator=mock_coordinator,
)

# Test successful minute forecast retrieval
result = await weather.async_get_minute_forecast()
assert result == mock_coordinator.data[ATTR_API_MINUTE_FORECAST]

# Test exception when mode is not OWM_MODE_V30
weather.mode = OWM_MODE_V25
with pytest.raises(ServiceValidationError):
await weather.async_get_minute_forecast()
Copy link
Member

Choose a reason for hiding this comment

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

On testing: We don't want to make our own hass and stuff. We want to create a MockConfigEntry, and have Home Assistant setup the integration naturally. For that to work we need to patch the library so it returns what we want.

Copy link
Contributor Author

@10100011 10100011 Oct 26, 2024

Choose a reason for hiding this comment

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

I've rewritten this testing to make better use of MockConfigEntry, getting rid of MagicMock and so not creating our own hass.

As a result of other comments, I have also introduced end-to-end testing based on a fixture.

@home-assistant home-assistant bot marked this pull request as draft October 24, 2024 18:06
@10100011 10100011 requested a review from joostlek October 26, 2024 10:04
@10100011 10100011 marked this pull request as ready for review October 28, 2024 22:30
def mock_weather_update():
"""Mock the WeatherUpdateCoordinator to prevent API calls."""
with patch(
"homeassistant.components.openweathermap.coordinator.WeatherUpdateCoordinator._async_update_data",
Copy link
Member

Choose a reason for hiding this comment

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

We should not patch internals, we should only patch the external library.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Patching is now limited to the HTTP call with the data provided by a static JSON file. This has had the added benefit of increasing the test coverage.

# Test the async_get_minute_forecast service
# We can call the service and assert the result
with patch(
"homeassistant.components.openweathermap.weather.OpenWeatherMapWeather.async_get_minute_forecast",
Copy link
Member

Choose a reason for hiding this comment

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

Same comments as above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same reply as above. :)

@home-assistant home-assistant bot marked this pull request as draft October 28, 2024 22:34
@10100011 10100011 marked this pull request as ready for review November 1, 2024 17:17
@frenck frenck added the smash Indicator this PR is close to finish for merging or closing label Nov 9, 2024
Comment on lines +82 to +102
# Test exception when mode is not OWM_MODE_V30
hass.config_entries.async_update_entry(
mock_config_entry,
options={**mock_config_entry.options, CONF_MODE: OWM_MODE_V25},
)
await hass.config_entries.async_reload(mock_config_entry.entry_id)
await hass.async_block_till_done()

# Expect a ServiceValidationError when mode is not OWM_MODE_V30
with pytest.raises(ServiceValidationError):
await hass.services.async_call(
DOMAIN,
SERVICE_GET_MINUTE_FORECAST,
{"entity_id": ENTITY_ID},
blocking=True,
return_response=True,
)

# Cleanup
await hass.config_entries.async_unload(mock_config_entry.entry_id)
await hass.async_block_till_done()
Copy link
Member

Choose a reason for hiding this comment

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

Let's have this be a separate test with its own mock config entry

blocking=True,
return_response=True,
)
assert result == MINUTE_FORECAST
Copy link
Member

Choose a reason for hiding this comment

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

You can use snapshot here

Suggested change
assert result == MINUTE_FORECAST
assert result == snapshot

If you then execute pytest ./tests/components/openweathermap --snapshot-update it will generate a .ambr file which is all the data you asserted but then serialized

Comment on lines +69 to +71
entity_registry = er.async_get(hass)
entry = entity_registry.async_get(ENTITY_ID)
assert entry 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.

You can also check this by just doing assert hass.states.get(ENTITY_ID) is not None

Comment on lines +52 to +55
@patch(
"pyopenweathermap.http_client.HttpClient.request",
new_callable=AsyncMock,
return_value=json.loads(load_fixture("openweathermap.json", "openweathermap")),
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 patch this at the place where the library is used? So when I look in the coordinator, self._owm_client.get_weather is called. So we want to patch that at homeassistant.components.openweathermap.OWMClient.get_weather and return what that function would return. This way we don't rely on the internals of the library which decreases coupling and increases code quality

return_value=json.loads(load_fixture("openweathermap.json", "openweathermap")),
)
async def test_minute_forecast(
mock_api_response, hass: HomeAssistant, mock_config_entry
Copy link
Member

Choose a reason for hiding this comment

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

please have hass as the first parameter

)


@pytest.mark.asyncio
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@pytest.mark.asyncio

Not needed

@home-assistant home-assistant bot marked this pull request as draft December 17, 2024 16:21
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.

6 participants