From 7805d2800c6e417549f064bf1662ced93ff1809b Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 21 Feb 2016 09:32:43 -0800 Subject: [PATCH] Expose current time object instead of method to retrieve (thanks @fabaff) --- homeassistant/util/template.py | 5 +++-- tests/util/test_template.py | 17 +++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/homeassistant/util/template.py b/homeassistant/util/template.py index 0cbddb2aa16..6fa4b6d55bf 100644 --- a/homeassistant/util/template.py +++ b/homeassistant/util/template.py @@ -44,6 +44,7 @@ def render(hass, template, variables=None, **kwargs): kwargs.update(variables) location_helper = LocationHelpers(hass) + utcnow = dt_util.utcnow() try: return ENV.from_string(template, { @@ -51,8 +52,8 @@ def render(hass, template, variables=None, **kwargs): 'is_state': hass.states.is_state, 'is_state_attr': hass.states.is_state_attr, 'states': AllStates(hass), - 'now': dt_util.now, - 'utcnow': dt_util.utcnow, + 'now': dt_util.as_local(utcnow), + 'utcnow': utcnow, }).render(kwargs).strip() except jinja2.TemplateError as err: raise TemplateError(err) diff --git a/tests/util/test_template.py b/tests/util/test_template.py index 737bb87fce9..d6882c9a219 100644 --- a/tests/util/test_template.py +++ b/tests/util/test_template.py @@ -159,21 +159,26 @@ class TestUtilTemplate(unittest.TestCase): 'unknown', 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', return_value=True) - def test_now_function(self, mock_is_safe, mock_now): + def test_now(self, mock_is_safe, mock_utcnow): self.assertEqual( - dt_util.now().isoformat(), - template.render(self.hass, '{{ now().isoformat() }}')) + dt_util.utcnow().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): + def test_utcnow(self, mock_is_safe, mock_utcnow): self.assertEqual( 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): self.hass.states.set('test.object', 'happy', {