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

* add support for datetime objects to as_datetime

* change import of datetime.date

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
Martijn van der Pol
2024-04-23 09:55:58 +02:00
committed by GitHub
parent 3d59303433
commit e90d76b18d
2 changed files with 39 additions and 4 deletions

View File

@@ -1198,6 +1198,35 @@ def test_as_datetime_from_timestamp(
)
@pytest.mark.parametrize(
("input", "output"),
[
(
"{% set dt = as_datetime('2024-01-01 16:00:00-08:00') %}",
"2024-01-01 16:00:00-08:00",
),
(
"{% set dt = as_datetime('2024-01-29').date() %}",
"2024-01-29 00:00:00",
),
],
)
def test_as_datetime_from_datetime(
hass: HomeAssistant, input: str, output: str
) -> None:
"""Test using datetime.datetime or datetime.date objects as input."""
assert (
template.Template(f"{input}{{{{ dt | as_datetime }}}}", hass).async_render()
== output
)
assert (
template.Template(f"{input}{{{{ as_datetime(dt) }}}}", hass).async_render()
== output
)
@pytest.mark.parametrize(
("input", "default", "output"),
[