mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 16:57:53 +00:00
Expose current time object instead of method to retrieve (thanks @fabaff)
This commit is contained in:
parent
9ad2cf7b7a
commit
7805d2800c
@ -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)
|
||||
|
@ -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', {
|
||||
|
Loading…
x
Reference in New Issue
Block a user