mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
parent
7f81122af6
commit
6847dac582
@ -12,6 +12,7 @@ from jinja2.sandbox import ImmutableSandboxedEnvironment
|
||||
|
||||
from homeassistant.const import STATE_UNKNOWN
|
||||
from homeassistant.exceptions import TemplateError
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
_SENTINEL = object()
|
||||
@ -45,7 +46,9 @@ def render(hass, template, variables=None, **kwargs):
|
||||
return ENV.from_string(template, {
|
||||
'states': AllStates(hass),
|
||||
'is_state': hass.states.is_state,
|
||||
'is_state_attr': hass.states.is_state_attr
|
||||
'is_state_attr': hass.states.is_state_attr,
|
||||
'now': dt_util.now,
|
||||
'utcnow': dt_util.utcnow,
|
||||
}).render(kwargs).strip()
|
||||
except jinja2.TemplateError as err:
|
||||
raise TemplateError(err)
|
||||
|
@ -6,8 +6,11 @@ Tests Home Assistant template util methods.
|
||||
"""
|
||||
# pylint: disable=too-many-public-methods
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.exceptions import TemplateError
|
||||
from homeassistant.util import template
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
@ -143,3 +146,19 @@ class TestUtilTemplate(unittest.TestCase):
|
||||
self.assertEqual(
|
||||
'unknown',
|
||||
template.render(self.hass, '{{ states("test.object2") }}'))
|
||||
|
||||
@patch('homeassistant.core.dt_util.now', return_value=dt_util.now())
|
||||
@patch('homeassistant.util.template.TemplateEnvironment.is_safe_callable',
|
||||
return_value=True)
|
||||
def test_now_function(self, mock_is_safe, mock_now):
|
||||
self.assertEqual(
|
||||
dt_util.now().isoformat(),
|
||||
template.render(self.hass, '{{ now().isoformat() }}'))
|
||||
|
||||
@patch('homeassistant.core.dt_util.utcnow', return_value=dt_util.utcnow())
|
||||
@patch('homeassistant.util.template.TemplateEnvironment.is_safe_callable',
|
||||
return_value=True)
|
||||
def test_utcnow_function(self, mock_is_safe, mock_utcnow):
|
||||
self.assertEqual(
|
||||
dt_util.utcnow().isoformat(),
|
||||
template.render(self.hass, '{{ utcnow().isoformat() }}'))
|
||||
|
Loading…
x
Reference in New Issue
Block a user