Skip to content

Avoid a naive datetime.now() in buienradar - #178368

Draft
soldier2008 wants to merge 3 commits into
home-assistant:devfrom
soldier2008:fix-buienradar-naive-now
Draft

Avoid a naive datetime.now() in buienradar#178368
soldier2008 wants to merge 3 commits into
home-assistant:devfrom
soldier2008:fix-buienradar-naive-now

Conversation

@soldier2008

@soldier2008 soldier2008 commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Proposed change

This check exists to suppress the parse warning during the midnight hour, when
Buienradar is known to return no data, so what it wants is the local hour.

dt_util.now().hour says that directly, instead of formatting a naive datetime
to %H and parsing the string back into an int. The module already imports
dt_util and uses it a few lines above.

Not a pure refactor

Worth calling out: datetime.now() reads the system time zone, while
dt_util.now() reads the one configured in Home Assistant. For an instance
where those two differ, the hour the warning is suppressed in moves from the
host's midnight to the user's. That looks like the intended behaviour to me —
the suppression is about when Buienradar stops serving data locally — but it is
a behaviour change, so I would rather flag it than bury it.

There was no test on this branch at all. The new one pins the configured zone
to America/Regina so it disagrees with the host, which makes it fail on dev
and pass here.

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

  • 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:

The check only suppresses the parse warning during the midnight hour, so
it wants the local hour. dt_util.now().hour expresses that directly,
without formatting a naive datetime and parsing the result back to an
int.

Part of home-assistant/epics#117
Copilot AI balanced review requested due to automatic review settings August 6, 2026 13:49
@home-assistant home-assistant Bot added bugfix cla-signed integration: buienradar small-pr PRs with less than 30 lines. Top 200 Integration is ranked within the top 200 by usage Quality Scale: No score labels Aug 6, 2026
@home-assistant

home-assistant Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

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

Code owner commands

Code owners of buienradar can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign buienradar Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Replaces a naive local-time call with Home Assistant’s timezone-aware utility.

Changes:

  • Uses dt_util.now().hour for the midnight warning suppression.
  • Removes the unused datetime import and lint suppression.

Copilot AI review requested due to automatic review settings August 6, 2026 19:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 2 out of 2 changed files in this pull request and generated no new comments.

Suppressed comments (1)

tests/components/buienradar/test_util.py:22

  • Rename the first parameter ID to reflect that it tests a non-midnight hour; 05:30 UTC is 23:30 in America/Regina, so daytime mislabels the scenario.
    ids=["daytime", "midnight_hour"],

The midnight-hour branch had no test, and the change is not a pure
refactor: datetime.now() read the system time zone while dt_util.now()
reads the one configured in Home Assistant. The test pins the configured
zone to America/Regina so the two disagree, and fails on dev.

Part of home-assistant/epics#117
@soldier2008
soldier2008 force-pushed the fix-buienradar-naive-now branch from 2ef417d to b7fbb95 Compare August 6, 2026 23:38
@keranm

keranm commented Aug 7, 2026

Copy link
Copy Markdown

looks good to me

_LOGGER.debug("Buienradar parsed data: %s", result)
if result.get(SUCCESS) is not True:
if int(datetime.now().strftime("%H")) > 0: # pylint: disable=home-assistant-enforce-naive-now
if dt_util.now().hour > 0:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the intention is to suppress warnings during the midnight hour specifically in Europe (Netherlands) since that's where the Buienradar service is located.

The commit message that added this condition said:

Suppress an error that often occers between 00:00 and 01:00 CE(S)T during that time, probably because buienradar.nl is then updating its forcast for the next day. The API does not always work between these times (in the middle of the night).

#15555

So the correct adjustment would be to check the time in that timezone specifically, regardless of the system time or configured local time.

CC @starkillerOG

@home-assistant

Copy link
Copy Markdown
Contributor

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 August 11, 2026 11:43
The warning is suppressed between 00:00 and 01:00 because buienradar.nl
updates its forecast then, so the hour that decides it belongs to the
service rather than to the user's configured time zone.

Tests pin both edges of the quiet hour in CET, repeat it in CEST so the
offset is not assumed, and cover the case where it is the quiet hour
where the user lives but not in Amsterdam.
Copilot AI review requested due to automatic review settings August 11, 2026 16:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (2)

tests/components/buienradar/test_util.py:52

  • Condense this docstring to state only the non-obvious timezone choice. The parameter IDs and values already document the boundary and DST cases, so the detailed narration duplicates straightforward test data.
    """Test the parse failure warning is suppressed in the midnight hour.

    buienradar.nl serves no data while it updates its forecast between 00:00 and
    01:00 CE(S)T, so the warning is only interesting outside that hour. The hour
    that decides this belongs to the service, so the configured time zone here is

homeassistant/components/buienradar/util.py:166

  • Align the timezone behavior with the PR description. This explicitly uses Europe/Amsterdam, while the description says the warning window should follow Home Assistant's configured timezone via dt_util.now(), so the implemented behavior and advertised behavior differ for users outside that zone; either use the configured timezone or update the PR rationale to establish the service timezone as the requirement.
            service_tz = await dt_util.async_get_time_zone(SERVICE_TIME_ZONE)
            if service_tz is None or dt_util.utcnow().astimezone(service_tz).hour > 0:

await hass.config.async_set_time_zone("America/Regina")
freezer.move_to(frozen_time)

data = BrData(hass, {CONF_LATITUDE: 51.5, CONF_LONGITUDE: 5.5}, 60, [])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This isn't an isolated utility, despite its location. It (incorrectly) stores entities on its instance.

So we should not test it directly but indirectly by setting up the config entry, while patching the client library, and then asserting the log messages after moving time forward to force an update.

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

Labels

bugfix cla-signed integration: buienradar Quality Scale: No score small-pr PRs with less than 30 lines. Top 200 Integration is ranked within the top 200 by usage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid a naive datetime.now() in buienradar

4 participants