mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 06:37:52 +00:00
Add a Jinja filter for relative time
This commit is contained in:
parent
a7292af3b1
commit
d5a1c52359
@ -6,6 +6,8 @@ import logging
|
|||||||
import jinja2
|
import jinja2
|
||||||
from jinja2.sandbox import ImmutableSandboxedEnvironment
|
from jinja2.sandbox import ImmutableSandboxedEnvironment
|
||||||
|
|
||||||
|
import humanize
|
||||||
|
|
||||||
from homeassistant.components import group
|
from homeassistant.components import group
|
||||||
from homeassistant.const import STATE_UNKNOWN, ATTR_LATITUDE, ATTR_LONGITUDE
|
from homeassistant.const import STATE_UNKNOWN, ATTR_LATITUDE, ATTR_LONGITUDE
|
||||||
from homeassistant.core import State
|
from homeassistant.core import State
|
||||||
@ -38,6 +40,11 @@ def render_with_possible_json_value(hass, template, value,
|
|||||||
return value if error_value is _SENTINEL else error_value
|
return value if error_value is _SENTINEL else error_value
|
||||||
|
|
||||||
|
|
||||||
|
def relative_time(end_time):
|
||||||
|
"""Return a relative (human readable) timestamp for the given time."""
|
||||||
|
return humanize.naturaltime(dt_util.now() - end_time)
|
||||||
|
|
||||||
|
|
||||||
def render(hass, template, variables=None, **kwargs):
|
def render(hass, template, variables=None, **kwargs):
|
||||||
"""Render given template."""
|
"""Render given template."""
|
||||||
if variables is not None:
|
if variables is not None:
|
||||||
@ -57,6 +64,7 @@ def render(hass, template, variables=None, **kwargs):
|
|||||||
'states': AllStates(hass),
|
'states': AllStates(hass),
|
||||||
'utcnow': utcnow,
|
'utcnow': utcnow,
|
||||||
'as_timestamp': dt_util.as_timestamp,
|
'as_timestamp': dt_util.as_timestamp,
|
||||||
|
'relative_time': relative_time
|
||||||
}).render(kwargs).strip()
|
}).render(kwargs).strip()
|
||||||
except jinja2.TemplateError as err:
|
except jinja2.TemplateError as err:
|
||||||
raise TemplateError(err)
|
raise TemplateError(err)
|
||||||
|
@ -6,6 +6,7 @@ pip>=7.0.0
|
|||||||
vincenty==0.1.4
|
vincenty==0.1.4
|
||||||
jinja2>=2.8
|
jinja2>=2.8
|
||||||
voluptuous==0.8.9
|
voluptuous==0.8.9
|
||||||
|
humanize==0.5.1
|
||||||
|
|
||||||
# homeassistant.components.isy994
|
# homeassistant.components.isy994
|
||||||
PyISY==1.0.5
|
PyISY==1.0.5
|
||||||
|
Loading…
x
Reference in New Issue
Block a user