Expose current time object instead of method to retrieve (thanks @fabaff)

This commit is contained in:
Paulus Schoutsen 2016-02-21 09:32:43 -08:00
parent 9ad2cf7b7a
commit 7805d2800c
2 changed files with 14 additions and 8 deletions

View File

@ -44,6 +44,7 @@ def render(hass, template, variables=None, **kwargs):
kwargs.update(variables) kwargs.update(variables)
location_helper = LocationHelpers(hass) location_helper = LocationHelpers(hass)
utcnow = dt_util.utcnow()
try: try:
return ENV.from_string(template, { return ENV.from_string(template, {
@ -51,8 +52,8 @@ def render(hass, template, variables=None, **kwargs):
'is_state': hass.states.is_state, 'is_state': hass.states.is_state,
'is_state_attr': hass.states.is_state_attr, 'is_state_attr': hass.states.is_state_attr,
'states': AllStates(hass), 'states': AllStates(hass),
'now': dt_util.now, 'now': dt_util.as_local(utcnow),
'utcnow': dt_util.utcnow, 'utcnow': utcnow,
}).render(kwargs).strip() }).render(kwargs).strip()
except jinja2.TemplateError as err: except jinja2.TemplateError as err:
raise TemplateError(err) raise TemplateError(err)

View File

@ -159,21 +159,26 @@ class TestUtilTemplate(unittest.TestCase):
'unknown', 'unknown',
template.render(self.hass, '{{ states("test.object2") }}')) template.render(self.hass, '{{ states("test.object2") }}'))
@patch('homeassistant.core.dt_util.now', return_value=dt_util.now()) @patch('homeassistant.core.dt_util.utcnow', return_value=dt_util.utcnow())
@patch('homeassistant.util.template.TemplateEnvironment.is_safe_callable', @patch('homeassistant.util.template.TemplateEnvironment.is_safe_callable',
return_value=True) return_value=True)
def test_now_function(self, mock_is_safe, mock_now): def test_now(self, mock_is_safe, mock_utcnow):
self.assertEqual( self.assertEqual(
dt_util.now().isoformat(), dt_util.utcnow().isoformat(),
template.render(self.hass, '{{ now().isoformat() }}')) template.render(self.hass, '{{ now.isoformat() }}'))
@patch('homeassistant.core.dt_util.utcnow', return_value=dt_util.utcnow()) @patch('homeassistant.core.dt_util.utcnow', return_value=dt_util.utcnow())
@patch('homeassistant.util.template.TemplateEnvironment.is_safe_callable', @patch('homeassistant.util.template.TemplateEnvironment.is_safe_callable',
return_value=True) return_value=True)
def test_utcnow_function(self, mock_is_safe, mock_utcnow): def test_utcnow(self, mock_is_safe, mock_utcnow):
self.assertEqual( self.assertEqual(
dt_util.utcnow().isoformat(), dt_util.utcnow().isoformat(),
template.render(self.hass, '{{ utcnow().isoformat() }}')) template.render(self.hass, '{{ utcnow.isoformat() }}'))
def test_utcnow_is_exactly_now(self):
self.assertEqual(
'True',
template.render(self.hass, '{{ utcnow == now }}'))
def test_distance_function_with_1_state(self): def test_distance_function_with_1_state(self):
self.hass.states.set('test.object', 'happy', { self.hass.states.set('test.object', 'happy', {