Add as_local convenience function to jinja templates (#39618)

This commit is contained in:
J. Nick Koston 2020-09-03 14:35:16 -05:00 committed by GitHub
parent a9cc882394
commit 35e84d0427
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 0 deletions

View File

@ -1051,6 +1051,7 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment):
self.filters["atan2"] = arc_tangent2
self.filters["sqrt"] = square_root
self.filters["as_timestamp"] = forgiving_as_timestamp
self.filters["as_local"] = dt_util.as_local
self.filters["timestamp_custom"] = timestamp_custom
self.filters["timestamp_local"] = timestamp_local
self.filters["timestamp_utc"] = timestamp_utc
@ -1084,6 +1085,7 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment):
self.globals["atan2"] = arc_tangent2
self.globals["float"] = forgiving_float
self.globals["now"] = dt_util.now
self.globals["as_local"] = dt_util.as_local
self.globals["utcnow"] = dt_util.utcnow
self.globals["as_timestamp"] = forgiving_as_timestamp
self.globals["relative_time"] = relative_time

View File

@ -511,6 +511,19 @@ def test_timestamp_local(hass):
)
def test_as_local(hass):
"""Test converting time to local."""
hass.states.async_set("test.object", "available")
last_updated = hass.states.get("test.object").last_updated
assert template.Template(
"{{ as_local(states.test.object.last_updated) }}", hass
).async_render() == str(dt_util.as_local(last_updated))
assert template.Template(
"{{ states.test.object.last_updated | as_local }}", hass
).async_render() == str(dt_util.as_local(last_updated))
def test_to_json(hass):
"""Test the object to JSON string filter."""