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

Don't raise errors when using datetime objects in as_datetime Jinja function/filter #109062

Merged
merged 3 commits into from
Apr 23, 2024

Conversation

TheFes
Copy link
Contributor

@TheFes TheFes commented Jan 29, 2024

Proposed change

The as_datetime function/filter currently errors when a datetime object is used as value. This can be very annoying when it is unclear if a value is already a datetime object, or a string. Attribute values of entities can be native types, but in case of datetime objects they are shown as isoformat datetime strings in developer tools > states.
Different (custom) integrations use this differently, eg some custom integration with energy price forecast attributes use datetime objects (Nordpool) and others use datetime isoformat string (ENTSO-E).

This PR adds support for datetime objects to as_datetime. In case a full datetime object is used as value, it will retrun the object without changes. In case a datetime.date is used as value it will add midnight (just like it would for a date string like "2024-01-29")

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.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@TheFes TheFes changed the title Don't raise errors when using datetime objecst in as_datetime Jinja function/filter Don't raise errors when using datetime objects in as_datetime Jinja function/filter Jan 29, 2024
@TheFes TheFes marked this pull request as ready for review January 29, 2024 18:45
@TheFes TheFes requested a review from a team as a code owner January 29, 2024 18:45
Copy link

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
If you are the author of this PR, please leave a comment if you want to keep it open. Also, please rebase your PR onto the latest dev branch to ensure that it's up to date with the latest changes.
Thank you for your contribution!

@github-actions github-actions bot added the stale label Mar 29, 2024
@TheFes
Copy link
Contributor Author

TheFes commented Mar 29, 2024

Still waiting for a review :)

@github-actions github-actions bot removed the stale label Mar 29, 2024
@TheFes
Copy link
Contributor Author

TheFes commented Apr 9, 2024

@MartinHjelmare
Sorry for the tag, but as you did the review on the PR on which I initially added this, but on your request split into only adding the default parameter, could you please have a look at this one as well?

The previous PR was #107229

Copy link
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

Looks good, thanks @TheFes 👍

@emontnemery
Copy link
Contributor

The PR can be merged when there's a PR updating the documentation, can you look into that @TheFes ?

@TheFes
Copy link
Contributor Author

TheFes commented Apr 23, 2024

Docs added

@emontnemery
Copy link
Contributor

Failing tests are unrelated

@emontnemery emontnemery merged commit e90d76b into home-assistant:dev Apr 23, 2024
33 of 34 checks passed
tr4nt0r pushed a commit to tr4nt0r/ha-core that referenced this pull request Apr 23, 2024
… function/filter (home-assistant#109062)

* add support for datetime objects to as_datetime

* change import of datetime.date

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
@github-actions github-actions bot locked and limited conversation to collaborators Apr 24, 2024
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.

None yet

2 participants