Remove deprecation warnings for relative_time (#116144)

* Remove deprecation warnings for relative_time

* Update homeassistant/helpers/template.py

Co-authored-by: Simon <80467011+sorgfresser@users.noreply.github.com>

---------

Co-authored-by: Simon <80467011+sorgfresser@users.noreply.github.com>
This commit is contained in:
Erik Montnemery 2024-04-25 13:02:18 +02:00 committed by Paulus Schoutsen
parent 74f32cfa90
commit c8d025f525
3 changed files with 4 additions and 32 deletions

View File

@ -56,10 +56,6 @@
"config_entry_reauth": { "config_entry_reauth": {
"title": "[%key:common::config_flow::title::reauth%]", "title": "[%key:common::config_flow::title::reauth%]",
"description": "Reauthentication is needed" "description": "Reauthentication is needed"
},
"template_function_relative_time_deprecated": {
"title": "The {relative_time} template function is deprecated",
"description": "The {relative_time} template function is deprecated in Home Assistant. Please use the {time_since} or {time_until} template functions instead."
} }
}, },
"system_health": { "system_health": {

View File

@ -59,7 +59,6 @@ from homeassistant.const import (
UnitOfLength, UnitOfLength,
) )
from homeassistant.core import ( from homeassistant.core import (
DOMAIN as HA_DOMAIN,
Context, Context,
HomeAssistant, HomeAssistant,
State, State,
@ -2480,30 +2479,11 @@ def relative_time(hass: HomeAssistant, value: Any) -> Any:
Make sure date is not in the future, or else it will return None. Make sure date is not in the future, or else it will return None.
If the input are not a datetime object the input will be returned unmodified. If the input are not a datetime object the input will be returned unmodified.
Note: This template function is deprecated in favor of `time_until`, but is still
supported so as not to break old templates.
""" """
def warn_relative_time_deprecated() -> None:
ir = issue_registry.async_get(hass)
issue_id = "template_function_relative_time_deprecated"
if ir.async_get_issue(HA_DOMAIN, issue_id):
return
issue_registry.async_create_issue(
hass,
HA_DOMAIN,
issue_id,
breaks_in_ha_version="2024.11.0",
is_fixable=False,
severity=issue_registry.IssueSeverity.WARNING,
translation_key=issue_id,
translation_placeholders={
"relative_time": "relative_time()",
"time_since": "time_since()",
"time_until": "time_until()",
},
)
_LOGGER.warning("Template function 'relative_time' is deprecated")
warn_relative_time_deprecated()
if (render_info := _render_info.get()) is not None: if (render_info := _render_info.get()) is not None:
render_info.has_time = True render_info.has_time = True

View File

@ -31,7 +31,7 @@ from homeassistant.const import (
UnitOfTemperature, UnitOfTemperature,
UnitOfVolume, UnitOfVolume,
) )
from homeassistant.core import DOMAIN as HA_DOMAIN, HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import TemplateError from homeassistant.exceptions import TemplateError
from homeassistant.helpers import ( from homeassistant.helpers import (
area_registry as ar, area_registry as ar,
@ -2240,7 +2240,6 @@ def test_relative_time(mock_is_safe, hass: HomeAssistant) -> None:
"""Test relative_time method.""" """Test relative_time method."""
hass.config.set_time_zone("UTC") hass.config.set_time_zone("UTC")
now = datetime.strptime("2000-01-01 10:00:00 +00:00", "%Y-%m-%d %H:%M:%S %z") now = datetime.strptime("2000-01-01 10:00:00 +00:00", "%Y-%m-%d %H:%M:%S %z")
issue_registry = ir.async_get(hass)
relative_time_template = ( relative_time_template = (
'{{relative_time(strptime("2000-01-01 09:00:00", "%Y-%m-%d %H:%M:%S"))}}' '{{relative_time(strptime("2000-01-01 09:00:00", "%Y-%m-%d %H:%M:%S"))}}'
) )
@ -2250,9 +2249,6 @@ def test_relative_time(mock_is_safe, hass: HomeAssistant) -> None:
hass, hass,
).async_render() ).async_render()
assert result == "1 hour" assert result == "1 hour"
assert issue_registry.async_get_issue(
HA_DOMAIN, "template_function_relative_time_deprecated"
)
result = template.Template( result = template.Template(
( (
"{{" "{{"