mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 23:27:37 +00:00
Correct today_at template function / filter (#60291)
Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
parent
d33457b7bc
commit
7b57033265
@ -1689,16 +1689,16 @@ def random_every_time(context, values):
|
|||||||
|
|
||||||
def today_at(time_str: str = "") -> datetime:
|
def today_at(time_str: str = "") -> datetime:
|
||||||
"""Record fetching now where the time has been replaced with value."""
|
"""Record fetching now where the time has been replaced with value."""
|
||||||
start = dt_util.start_of_local_day(datetime.now())
|
today = dt_util.start_of_local_day()
|
||||||
|
if not time_str:
|
||||||
|
return today
|
||||||
|
|
||||||
dttime = start.time() if time_str == "" else dt_util.parse_time(time_str)
|
if (time_today := dt_util.parse_time(time_str)) is None:
|
||||||
|
raise ValueError(
|
||||||
|
f"could not convert {type(time_str).__name__} to datetime: '{time_str}'"
|
||||||
|
)
|
||||||
|
|
||||||
if dttime:
|
return datetime.combine(today, time_today, today.tzinfo)
|
||||||
return datetime.combine(start.date(), dttime, tzinfo=dt_util.DEFAULT_TIME_ZONE)
|
|
||||||
|
|
||||||
raise ValueError(
|
|
||||||
f"could not convert {type(time_str).__name__} to datetime: '{time_str}'"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def relative_time(value):
|
def relative_time(value):
|
||||||
|
@ -5,6 +5,7 @@ import math
|
|||||||
import random
|
import random
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from freezegun import freeze_time
|
||||||
import pytest
|
import pytest
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -1149,42 +1150,68 @@ def test_utcnow(mock_is_safe, hass):
|
|||||||
assert info.has_time is True
|
assert info.has_time is True
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"now, expected, expected_midnight, timezone_str",
|
||||||
|
[
|
||||||
|
# Host clock in UTC
|
||||||
|
(
|
||||||
|
"2021-11-24 03:00:00+00:00",
|
||||||
|
"2021-11-23T10:00:00-08:00",
|
||||||
|
"2021-11-23T00:00:00-08:00",
|
||||||
|
"America/Los_Angeles",
|
||||||
|
),
|
||||||
|
# Host clock in local time
|
||||||
|
(
|
||||||
|
"2021-11-23 19:00:00-08:00",
|
||||||
|
"2021-11-23T10:00:00-08:00",
|
||||||
|
"2021-11-23T00:00:00-08:00",
|
||||||
|
"America/Los_Angeles",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
@patch(
|
@patch(
|
||||||
"homeassistant.helpers.template.TemplateEnvironment.is_safe_callable",
|
"homeassistant.helpers.template.TemplateEnvironment.is_safe_callable",
|
||||||
return_value=True,
|
return_value=True,
|
||||||
)
|
)
|
||||||
def test_today_at(mock_is_safe, hass):
|
def test_today_at(mock_is_safe, hass, now, expected, expected_midnight, timezone_str):
|
||||||
"""Test today_at method."""
|
"""Test today_at method."""
|
||||||
now = dt_util.now()
|
freezer = freeze_time(now)
|
||||||
with patch("homeassistant.util.dt.now", return_value=now):
|
freezer.start()
|
||||||
now = now.replace(hour=10, minute=0, second=0, microsecond=0)
|
|
||||||
result = template.Template(
|
|
||||||
"{{ today_at('10:00').isoformat() }}",
|
|
||||||
hass,
|
|
||||||
).async_render()
|
|
||||||
assert result == now.isoformat()
|
|
||||||
|
|
||||||
result = template.Template(
|
original_tz = dt_util.DEFAULT_TIME_ZONE
|
||||||
"{{ today_at('10:00:00').isoformat() }}",
|
|
||||||
hass,
|
|
||||||
).async_render()
|
|
||||||
assert result == now.isoformat()
|
|
||||||
|
|
||||||
result = template.Template(
|
timezone = dt_util.get_time_zone(timezone_str)
|
||||||
"{{ ('10:00:00' | today_at).isoformat() }}",
|
dt_util.set_default_time_zone(timezone)
|
||||||
hass,
|
|
||||||
).async_render()
|
|
||||||
assert result == now.isoformat()
|
|
||||||
|
|
||||||
now = now.replace(hour=0)
|
result = template.Template(
|
||||||
result = template.Template(
|
"{{ today_at('10:00').isoformat() }}",
|
||||||
"{{ today_at().isoformat() }}",
|
hass,
|
||||||
hass,
|
).async_render()
|
||||||
).async_render()
|
assert result == expected
|
||||||
assert result == now.isoformat()
|
|
||||||
|
|
||||||
with pytest.raises(TemplateError):
|
result = template.Template(
|
||||||
template.Template("{{ today_at('bad') }}", hass).async_render()
|
"{{ today_at('10:00:00').isoformat() }}",
|
||||||
|
hass,
|
||||||
|
).async_render()
|
||||||
|
assert result == expected
|
||||||
|
|
||||||
|
result = template.Template(
|
||||||
|
"{{ ('10:00:00' | today_at).isoformat() }}",
|
||||||
|
hass,
|
||||||
|
).async_render()
|
||||||
|
assert result == expected
|
||||||
|
|
||||||
|
result = template.Template(
|
||||||
|
"{{ today_at().isoformat() }}",
|
||||||
|
hass,
|
||||||
|
).async_render()
|
||||||
|
assert result == expected_midnight
|
||||||
|
|
||||||
|
with pytest.raises(TemplateError):
|
||||||
|
template.Template("{{ today_at('bad') }}", hass).async_render()
|
||||||
|
|
||||||
|
freezer.stop()
|
||||||
|
dt_util.set_default_time_zone(original_tz)
|
||||||
|
|
||||||
|
|
||||||
@patch(
|
@patch(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user