Skip to content

Update weatherkit to fetch hourly data for 7 days#164494

Merged
zxdavb merged 8 commits intohome-assistant:devfrom
joelhawksley:weatherkit_hourly_week
Mar 7, 2026
Merged

Update weatherkit to fetch hourly data for 7 days#164494
zxdavb merged 8 commits intohome-assistant:devfrom
joelhawksley:weatherkit_hourly_week

Conversation

@joelhawksley
Copy link
Copy Markdown
Contributor

@joelhawksley joelhawksley commented Feb 28, 2026

Proposed change

This PR updates weatherkit to fetch hourly data for 7 days instead of 1.

While WeatherKit will return up to 10 days, I figured a week was fine. I want to be able to display hourly weather for the next couple of days, not just the next 24 hours.

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

N/A

Checklist

  • I understand the code I am submitting and can explain how it works.
  • 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.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

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 diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

While WeatherKit will return up to 10 days, I figured a week
was fine. I want to be able to display hourly weather for the
next couple of days, not just the next 24 hours.
@home-assistant
Copy link
Copy Markdown
Contributor

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

Code owner commands

Code owners of weatherkit 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 weatherkit 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, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

Comment thread homeassistant/components/weatherkit/coordinator.py Outdated
Comment thread homeassistant/components/weatherkit/coordinator.py Outdated
@joelhawksley joelhawksley marked this pull request as ready for review February 28, 2026 21:24
Copilot AI review requested due to automatic review settings February 28, 2026 21:24
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR extends the WeatherKit integration’s hourly forecast request window to cover the next 7 days (instead of the default shorter window), enabling longer-range hourly forecasts to be exposed via the existing coordinator-backed weather entity.

Changes:

  • Add an explicit hourly_start/hourly_end range to the WeatherKit get_weather_data call.
  • Use a UTC “now” timestamp to define the 7-day hourly forecast window.
Comments suppressed due to low confidence (3)

homeassistant/components/weatherkit/coordinator.py:77

  • This change introduces new behavior (explicit hourly range and 7-day window) but there is no test asserting the get_weather_data call uses the expected hourly_start/hourly_end values (and that they’re omitted when hourly forecasts aren’t supported). Adding a coordinator-level test that inspects the mocked call args would help prevent regressions.
            now = datetime.now(tz=UTC)
            updated_data = await self.client.get_weather_data(
                self.config_entry.data[CONF_LATITUDE],
                self.config_entry.data[CONF_LONGITUDE],
                self.supported_data_sets,
                hourly_start=now,
                hourly_end=now + timedelta(days=7),
            )

homeassistant/components/weatherkit/coordinator.py:77

  • Requesting 7 days of hourly forecast data on every coordinator refresh can significantly increase response size and API usage (update interval is 5 minutes). Consider reducing the refresh frequency when hourly_end is extended, or splitting current conditions vs forecast into separate refresh paths/coordinators so current weather can remain frequent while the 7‑day forecast updates less often.
            now = datetime.now(tz=UTC)
            updated_data = await self.client.get_weather_data(
                self.config_entry.data[CONF_LATITUDE],
                self.config_entry.data[CONF_LONGITUDE],
                self.supported_data_sets,
                hourly_start=now,
                hourly_end=now + timedelta(days=7),
            )

homeassistant/components/weatherkit/coordinator.py:77

  • hourly_start/hourly_end are passed even when supported_data_sets does not include DataSetType.HOURLY_FORECAST (e.g., locations where hourly is unavailable). It would be safer to only include these kwargs when requesting the hourly forecast dataset to avoid sending unused parameters and potential upstream validation errors.
            updated_data = await self.client.get_weather_data(
                self.config_entry.data[CONF_LATITUDE],
                self.config_entry.data[CONF_LONGITUDE],
                self.supported_data_sets,
                hourly_start=now,
                hourly_end=now + timedelta(days=7),
            )

@zxdavb
Copy link
Copy Markdown
Member

zxdavb commented Mar 1, 2026

I think you have to make some comment as to the risk (or absence os such) of getting tagged for API abuse by the vendor...

This will affect all users of this integration - they're going to be obligated to pull data that - you could reasonably argue - most people don't want...

Maybe a better solution would be to provide an action to enable this functionality? Or have it in config flow?

@joelhawksley
Copy link
Copy Markdown
Contributor Author

@zxdavb that's a totally fair point. That being said:

  1. The default response from WeatherKit is to include 10 days of hourly data.
  2. I've been fetching 10 days of hourly data every 60s from WeatherKit for at least a year without issue.

Copy link
Copy Markdown
Contributor

@tjhorner tjhorner left a comment

Choose a reason for hiding this comment

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

Seems reasonable to me.

Copilot AI review requested due to automatic review settings March 5, 2026 13:19
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (3)

homeassistant/components/weatherkit/coordinator.py:77

  • Fetching 7 days of hourly forecast data on every coordinator refresh (currently update_interval=5 minutes) will greatly increase response size and the size of the weather entity’s forecast attributes. This can increase bandwidth/API usage and significantly bloat state/recorder storage over time. Consider limiting the hourly range (for example, a smaller window such as 48–72 hours) and/or separating hourly forecast fetching into a less-frequent refresh cadence or cached update that is not re-downloaded every 5 minutes.
            now = datetime.now(tz=UTC)
            updated_data = await self.client.get_weather_data(
                self.config_entry.data[CONF_LATITUDE],
                self.config_entry.data[CONF_LONGITUDE],
                self.supported_data_sets,
                hourly_start=now,
                hourly_end=now + timedelta(days=7),
            )

homeassistant/components/weatherkit/coordinator.py:76

  • This change introduces new request parameters (hourly_start/hourly_end) and a new 7-day window, but the WeatherKit tests do not assert that get_weather_data is called with the expected arguments/window. Adding a coordinator test that freezes time and verifies the API client call parameters would guard against regressions and accidental range changes.
            updated_data = await self.client.get_weather_data(
                self.config_entry.data[CONF_LATITUDE],
                self.config_entry.data[CONF_LONGITUDE],
                self.supported_data_sets,
                hourly_start=now,
                hourly_end=now + timedelta(days=7),

homeassistant/components/weatherkit/coordinator.py:71

  • now is computed as a timezone-aware UTC datetime, but this coordinator still uses naive datetime.now() for last_updated_at and the stale-data threshold checks. Mixing aware/naive datetimes in the same method is easy to trip over later and can lead to subtle time math issues if more logic is added. Consider using a consistent UTC time source for all timestamps in this coordinator (for example, keep everything UTC-aware or everything UTC-naive) and reusing the same now value for the update bookkeeping.
            now = datetime.now(tz=UTC)
            updated_data = await self.client.get_weather_data(

@zxdavb
Copy link
Copy Markdown
Member

zxdavb commented Mar 6, 2026

Out of curiosity, I checked the API docs:

dailyEnd: if absent, daily forecasts run for 10 days.
hourlyEnd: if absent, hourly forecasts run for 24 hours or the length of the daily forecast, whichever is longer.

So the default hourly window is actually 10 days; this PR is less than the default. Fair point, @joelhawksley

However, my concern isn't the window size per se, it's that this data is fetched on every coordinator refresh. Pulling a week of hourly forecasts every 5 minutes means re-fetching ~168 data points whose values are essentially unchanged, with only the leading edge stale.

That doesn't smell right to me and - once merged - all users of this integration are opted in with no way out.

@tjhorner, what do you think? HA has a responsibility to be a good citizen with vendor APIs. The principled fix would be either a separate (slower) refresh cadence for the extended forecast (e.g. hourly data >24h is fetched every hour), or exposing this as a service/action that users can schedule themselves if they want it.

@tjhorner
Copy link
Copy Markdown
Contributor

tjhorner commented Mar 6, 2026

@zxdavb

So the default hourly window is actually 10 days; this PR is less than the default

Not true, because the library defaults to 1 day if not provided.

~168 data points whose values are essentially unchanged, with only the leading edge stale

This is not necessarily true. The predictions will change based on current conditions.

HA has a responsibility to be a good citizen with vendor APIs

I agree in principle, but in practice I don't think this would place much additional stress on a service from a company the size of Apple. The WeatherKit traffic from Home Assistant instances will be imperceptible compared to all Apple devices globally pinging it at regular intervals.

The primary concern, in my opinion, is staying within the stated API limits. Since the limits are metered per request, the changes in this PR will not affect that. Keep in mind that end users are paying for access to this service as well; as long as we adhere to the WeatherKit ToS then I don't see an issue.

Comment thread homeassistant/components/weatherkit/coordinator.py Outdated
Comment thread homeassistant/components/weatherkit/coordinator.py Outdated
@home-assistant home-assistant Bot marked this pull request as draft March 6, 2026 19:34
@home-assistant
Copy link
Copy Markdown
Contributor

home-assistant Bot commented Mar 6, 2026

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.

@joelhawksley joelhawksley marked this pull request as ready for review March 6, 2026 22:18
Copilot AI review requested due to automatic review settings March 6, 2026 22:18
@home-assistant home-assistant Bot requested a review from zxdavb March 6, 2026 22:18
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Comment thread homeassistant/components/weatherkit/coordinator.py
Comment thread homeassistant/components/weatherkit/coordinator.py
Copy link
Copy Markdown
Member

@zxdavb zxdavb left a comment

Choose a reason for hiding this comment

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

Re tests: if the integration is now retrieving 7 days of hourly data instead of 24 hours - why hasn't there been a need to update a fixture or a snapshot?

@joelhawksley
Copy link
Copy Markdown
Contributor Author

@zxdavb from what I can tell, the test hardcodes the response data size so there was no need to change a fixture or snapshot.

@zxdavb
Copy link
Copy Markdown
Member

zxdavb commented Mar 7, 2026

@zxdavb from what I can tell, the test hardcodes the response data size so there was no need to change a fixture or snapshot.

I think there is no value in taking that further.

@zxdavb
Copy link
Copy Markdown
Member

zxdavb commented Mar 7, 2026

LGTM.

@zxdavb zxdavb merged commit 2a8b045 into home-assistant:dev Mar 7, 2026
36 of 37 checks passed
@joelhawksley joelhawksley deleted the weatherkit_hourly_week branch March 8, 2026 20:00
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 9, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants