2016-03-09 09:25:50 +00:00
|
|
|
"""Test Home Assistant template helper methods."""
|
2017-08-18 06:19:35 +00:00
|
|
|
import asyncio
|
2016-11-11 06:57:44 +00:00
|
|
|
from datetime import datetime
|
2015-12-10 00:20:09 +00:00
|
|
|
import unittest
|
2017-05-23 17:32:06 +00:00
|
|
|
import random
|
2017-11-28 05:29:01 +00:00
|
|
|
import math
|
2019-01-21 00:46:14 +00:00
|
|
|
import pytz
|
2016-02-21 04:58:01 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2016-02-21 19:13:40 +00:00
|
|
|
from homeassistant.components import group
|
2015-12-12 03:07:03 +00:00
|
|
|
from homeassistant.exceptions import TemplateError
|
2016-02-23 20:06:50 +00:00
|
|
|
from homeassistant.helpers import template
|
2016-08-09 03:42:25 +00:00
|
|
|
from homeassistant.util.unit_system import UnitSystem
|
2016-07-31 20:24:49 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
LENGTH_METERS,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
MASS_GRAMS,
|
|
|
|
VOLUME_LITERS,
|
2016-09-28 04:29:55 +00:00
|
|
|
MATCH_ALL,
|
2016-07-31 20:24:49 +00:00
|
|
|
)
|
2016-02-21 04:58:01 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2015-12-10 00:20:09 +00:00
|
|
|
|
2016-02-14 23:08:23 +00:00
|
|
|
from tests.common import get_test_home_assistant
|
2018-10-24 10:10:05 +00:00
|
|
|
import pytest
|
2016-02-14 23:08:23 +00:00
|
|
|
|
2015-12-10 00:20:09 +00:00
|
|
|
|
2016-09-28 04:29:55 +00:00
|
|
|
class TestHelpersTemplate(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the Template."""
|
2015-12-10 00:20:09 +00:00
|
|
|
|
2016-10-30 21:18:53 +00:00
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def setUp(self):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up the tests."""
|
2016-02-14 23:08:23 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2016-07-31 20:24:49 +00:00
|
|
|
self.hass.config.units = UnitSystem('custom', TEMP_CELSIUS,
|
|
|
|
LENGTH_METERS, VOLUME_LITERS,
|
|
|
|
MASS_GRAMS)
|
2015-12-10 00:20:09 +00:00
|
|
|
|
2016-10-30 21:18:53 +00:00
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def tearDown(self):
|
2016-02-23 20:06:50 +00:00
|
|
|
"""Stop down stuff we started."""
|
2015-12-10 00:20:09 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
def test_referring_states_by_entity_id(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test referring states by entity id."""
|
2015-12-10 00:20:09 +00:00
|
|
|
self.hass.states.set('test.object', 'happy')
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'happy' == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template(
|
2018-10-24 10:10:05 +00:00
|
|
|
'{{ states.test.object.state }}', self.hass).render()
|
2015-12-10 00:20:09 +00:00
|
|
|
|
|
|
|
def test_iterating_all_states(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test iterating all states."""
|
2015-12-10 00:20:09 +00:00
|
|
|
self.hass.states.set('test.object', 'happy')
|
|
|
|
self.hass.states.set('sensor.temperature', 10)
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert '10happy' == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template(
|
|
|
|
'{% for state in states %}{{ state.state }}{% endfor %}',
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2015-12-10 00:20:09 +00:00
|
|
|
|
|
|
|
def test_iterating_domain_states(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test iterating domain states."""
|
2015-12-10 00:20:09 +00:00
|
|
|
self.hass.states.set('test.object', 'happy')
|
|
|
|
self.hass.states.set('sensor.back_door', 'open')
|
|
|
|
self.hass.states.set('sensor.temperature', 10)
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'open10' == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template("""
|
2016-02-14 21:07:21 +00:00
|
|
|
{% for state in states.sensor %}{{ state.state }}{% endfor %}
|
2018-10-24 10:10:05 +00:00
|
|
|
""", self.hass).render()
|
2015-12-10 00:20:09 +00:00
|
|
|
|
2016-02-24 18:41:49 +00:00
|
|
|
def test_float(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test float."""
|
2016-02-24 18:41:49 +00:00
|
|
|
self.hass.states.set('sensor.temperature', '12')
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert '12.0' == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template(
|
|
|
|
'{{ float(states.sensor.temperature.state) }}',
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2016-02-24 18:41:49 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'True' == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template(
|
|
|
|
'{{ float(states.sensor.temperature.state) > 11 }}',
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2016-02-24 18:41:49 +00:00
|
|
|
|
2015-12-10 00:20:09 +00:00
|
|
|
def test_rounding_value(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test rounding value."""
|
2015-12-11 04:46:15 +00:00
|
|
|
self.hass.states.set('sensor.temperature', 12.78)
|
2015-12-10 00:20:09 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert '12.8' == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template(
|
|
|
|
'{{ states.sensor.temperature.state | round(1) }}',
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2015-12-10 00:36:47 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert '128' == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template(
|
|
|
|
'{{ states.sensor.temperature.state | multiply(10) | round }}',
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2015-12-11 05:16:05 +00:00
|
|
|
|
2016-02-21 05:59:16 +00:00
|
|
|
def test_rounding_value_get_original_value_on_error(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test rounding value get original value on error."""
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'None' == \
|
|
|
|
template.Template('{{ None | round }}', self.hass).render()
|
2016-02-21 05:59:16 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'no_number' == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template(
|
2018-10-24 10:10:05 +00:00
|
|
|
'{{ "no_number" | round }}', self.hass).render()
|
2016-02-21 05:59:16 +00:00
|
|
|
|
2016-02-21 19:12:37 +00:00
|
|
|
def test_multiply(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test multiply."""
|
2016-02-21 19:12:37 +00:00
|
|
|
tests = {
|
|
|
|
None: 'None',
|
|
|
|
10: '100',
|
|
|
|
'"abcd"': 'abcd'
|
|
|
|
}
|
|
|
|
|
|
|
|
for inp, out in tests.items():
|
2018-10-24 10:10:05 +00:00
|
|
|
assert out == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template('{{ %s | multiply(10) | round }}' % inp,
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2016-02-21 19:12:37 +00:00
|
|
|
|
2017-11-28 05:29:01 +00:00
|
|
|
def test_logarithm(self):
|
|
|
|
"""Test logarithm."""
|
|
|
|
tests = [
|
|
|
|
(4, 2, '2.0'),
|
|
|
|
(1000, 10, '3.0'),
|
|
|
|
(math.e, '', '1.0'),
|
|
|
|
('"invalid"', '_', 'invalid'),
|
|
|
|
(10, '"invalid"', '10.0'),
|
|
|
|
]
|
|
|
|
|
|
|
|
for value, base, expected in tests:
|
2018-10-24 10:10:05 +00:00
|
|
|
assert expected == \
|
2017-11-28 05:29:01 +00:00
|
|
|
template.Template(
|
|
|
|
'{{ %s | log(%s) | round(1) }}' % (value, base),
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2017-11-28 05:29:01 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert expected == \
|
2017-11-28 05:29:01 +00:00
|
|
|
template.Template(
|
|
|
|
'{{ log(%s, %s) | round(1) }}' % (value, base),
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2017-11-28 05:29:01 +00:00
|
|
|
|
2018-04-15 16:51:45 +00:00
|
|
|
def test_sine(self):
|
|
|
|
"""Test sine."""
|
|
|
|
tests = [
|
|
|
|
(0, '0.0'),
|
|
|
|
(math.pi / 2, '1.0'),
|
|
|
|
(math.pi, '0.0'),
|
|
|
|
(math.pi * 1.5, '-1.0'),
|
|
|
|
(math.pi / 10, '0.309')
|
|
|
|
]
|
|
|
|
|
|
|
|
for value, expected in tests:
|
2018-10-24 10:10:05 +00:00
|
|
|
assert expected == \
|
2018-04-15 16:51:45 +00:00
|
|
|
template.Template(
|
|
|
|
'{{ %s | sin | round(3) }}' % value,
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2018-04-15 16:51:45 +00:00
|
|
|
|
|
|
|
def test_cos(self):
|
|
|
|
"""Test cosine."""
|
|
|
|
tests = [
|
|
|
|
(0, '1.0'),
|
|
|
|
(math.pi / 2, '0.0'),
|
|
|
|
(math.pi, '-1.0'),
|
|
|
|
(math.pi * 1.5, '-0.0'),
|
|
|
|
(math.pi / 10, '0.951')
|
|
|
|
]
|
|
|
|
|
|
|
|
for value, expected in tests:
|
2018-10-24 10:10:05 +00:00
|
|
|
assert expected == \
|
2018-04-15 16:51:45 +00:00
|
|
|
template.Template(
|
|
|
|
'{{ %s | cos | round(3) }}' % value,
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2018-04-15 16:51:45 +00:00
|
|
|
|
|
|
|
def test_tan(self):
|
|
|
|
"""Test tangent."""
|
|
|
|
tests = [
|
|
|
|
(0, '0.0'),
|
|
|
|
(math.pi, '-0.0'),
|
|
|
|
(math.pi / 180 * 45, '1.0'),
|
|
|
|
(math.pi / 180 * 90, '1.633123935319537e+16'),
|
|
|
|
(math.pi / 180 * 135, '-1.0')
|
|
|
|
]
|
|
|
|
|
|
|
|
for value, expected in tests:
|
2018-10-24 10:10:05 +00:00
|
|
|
assert expected == \
|
2018-04-15 16:51:45 +00:00
|
|
|
template.Template(
|
|
|
|
'{{ %s | tan | round(3) }}' % value,
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2018-04-15 16:51:45 +00:00
|
|
|
|
|
|
|
def test_sqrt(self):
|
|
|
|
"""Test square root."""
|
|
|
|
tests = [
|
|
|
|
(0, '0.0'),
|
|
|
|
(1, '1.0'),
|
|
|
|
(2, '1.414'),
|
|
|
|
(10, '3.162'),
|
|
|
|
(100, '10.0'),
|
|
|
|
]
|
|
|
|
|
|
|
|
for value, expected in tests:
|
2018-10-24 10:10:05 +00:00
|
|
|
assert expected == \
|
2018-04-15 16:51:45 +00:00
|
|
|
template.Template(
|
|
|
|
'{{ %s | sqrt | round(3) }}' % value,
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2018-04-15 16:51:45 +00:00
|
|
|
|
2016-11-11 06:57:44 +00:00
|
|
|
def test_strptime(self):
|
|
|
|
"""Test the parse timestamp method."""
|
|
|
|
tests = [
|
|
|
|
('2016-10-19 15:22:05.588122 UTC',
|
|
|
|
'%Y-%m-%d %H:%M:%S.%f %Z', None),
|
|
|
|
('2016-10-19 15:22:05.588122+0100',
|
|
|
|
'%Y-%m-%d %H:%M:%S.%f%z', None),
|
|
|
|
('2016-10-19 15:22:05.588122',
|
|
|
|
'%Y-%m-%d %H:%M:%S.%f', None),
|
|
|
|
('2016-10-19', '%Y-%m-%d', None),
|
|
|
|
('2016', '%Y', None),
|
|
|
|
('15:22:05', '%H:%M:%S', None),
|
|
|
|
('1469119144', '%Y', '1469119144'),
|
|
|
|
('invalid', '%Y', 'invalid')
|
|
|
|
]
|
|
|
|
|
|
|
|
for inp, fmt, expected in tests:
|
|
|
|
if expected is None:
|
|
|
|
expected = datetime.strptime(inp, fmt)
|
|
|
|
|
|
|
|
temp = '{{ strptime(\'%s\', \'%s\') }}' % (inp, fmt)
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert str(expected) == \
|
|
|
|
template.Template(temp, self.hass).render()
|
2016-11-11 06:57:44 +00:00
|
|
|
|
2016-09-09 00:49:02 +00:00
|
|
|
def test_timestamp_custom(self):
|
|
|
|
"""Test the timestamps to custom filter."""
|
2017-03-24 03:56:39 +00:00
|
|
|
now = dt_util.utcnow()
|
2016-09-09 00:49:02 +00:00
|
|
|
tests = [
|
|
|
|
(None, None, None, 'None'),
|
|
|
|
(1469119144, None, True, '2016-07-21 16:39:04'),
|
|
|
|
(1469119144, '%Y', True, '2016'),
|
|
|
|
(1469119144, 'invalid', True, 'invalid'),
|
2017-03-24 03:56:39 +00:00
|
|
|
(dt_util.as_timestamp(now), None, False,
|
|
|
|
now.strftime('%Y-%m-%d %H:%M:%S'))
|
2016-09-09 00:49:02 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
for inp, fmt, local, out in tests:
|
|
|
|
if fmt:
|
|
|
|
fil = 'timestamp_custom(\'{}\')'.format(fmt)
|
|
|
|
elif fmt and local:
|
|
|
|
fil = 'timestamp_custom(\'{0}\', {1})'.format(fmt, local)
|
|
|
|
else:
|
|
|
|
fil = 'timestamp_custom'
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert out == template.Template(
|
|
|
|
'{{ %s | %s }}' % (inp, fil), self.hass).render()
|
2016-09-09 00:49:02 +00:00
|
|
|
|
2016-07-23 02:47:43 +00:00
|
|
|
def test_timestamp_local(self):
|
|
|
|
"""Test the timestamps to local filter."""
|
|
|
|
tests = {
|
|
|
|
None: 'None',
|
|
|
|
1469119144: '2016-07-21 16:39:04',
|
|
|
|
}
|
|
|
|
|
|
|
|
for inp, out in tests.items():
|
2018-10-24 10:10:05 +00:00
|
|
|
assert out == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template('{{ %s | timestamp_local }}' % inp,
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2016-07-23 02:47:43 +00:00
|
|
|
|
2017-02-07 08:25:47 +00:00
|
|
|
def test_min(self):
|
|
|
|
"""Test the min filter."""
|
2018-10-24 10:10:05 +00:00
|
|
|
assert '1' == \
|
2017-02-07 08:25:47 +00:00
|
|
|
template.Template('{{ [1, 2, 3] | min }}',
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2017-02-07 08:25:47 +00:00
|
|
|
|
|
|
|
def test_max(self):
|
|
|
|
"""Test the max filter."""
|
2018-10-24 10:10:05 +00:00
|
|
|
assert '3' == \
|
2017-02-07 08:25:47 +00:00
|
|
|
template.Template('{{ [1, 2, 3] | max }}',
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2017-02-07 08:25:47 +00:00
|
|
|
|
2018-12-01 09:38:10 +00:00
|
|
|
def test_base64_encode(self):
|
|
|
|
"""Test the base64_encode filter."""
|
|
|
|
self.assertEqual(
|
|
|
|
'aG9tZWFzc2lzdGFudA==',
|
|
|
|
template.Template('{{ "homeassistant" | base64_encode }}',
|
|
|
|
self.hass).render())
|
|
|
|
|
|
|
|
def test_base64_decode(self):
|
|
|
|
"""Test the base64_decode filter."""
|
|
|
|
self.assertEqual(
|
|
|
|
'homeassistant',
|
|
|
|
template.Template('{{ "aG9tZWFzc2lzdGFudA==" | base64_decode }}',
|
|
|
|
self.hass).render())
|
|
|
|
|
|
|
|
def test_ordinal(self):
|
|
|
|
"""Test the ordinal filter."""
|
|
|
|
tests = [
|
|
|
|
(1, '1st'),
|
|
|
|
(2, '2nd'),
|
|
|
|
(3, '3rd'),
|
|
|
|
(4, '4th'),
|
|
|
|
(5, '5th'),
|
|
|
|
]
|
|
|
|
|
|
|
|
for value, expected in tests:
|
|
|
|
self.assertEqual(
|
|
|
|
expected,
|
|
|
|
template.Template(
|
|
|
|
'{{ %s | ordinal }}' % value,
|
|
|
|
self.hass).render())
|
|
|
|
|
2016-07-23 02:47:43 +00:00
|
|
|
def test_timestamp_utc(self):
|
|
|
|
"""Test the timestamps to local filter."""
|
2017-03-24 03:56:39 +00:00
|
|
|
now = dt_util.utcnow()
|
2016-07-23 02:47:43 +00:00
|
|
|
tests = {
|
|
|
|
None: 'None',
|
|
|
|
1469119144: '2016-07-21 16:39:04',
|
2017-03-24 03:56:39 +00:00
|
|
|
dt_util.as_timestamp(now):
|
|
|
|
now.strftime('%Y-%m-%d %H:%M:%S')
|
2016-07-23 02:47:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for inp, out in tests.items():
|
2018-10-24 10:10:05 +00:00
|
|
|
assert out == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template('{{ %s | timestamp_utc }}' % inp,
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2016-07-23 02:47:43 +00:00
|
|
|
|
2017-03-03 06:18:01 +00:00
|
|
|
def test_as_timestamp(self):
|
|
|
|
"""Test the as_timestamp function."""
|
2018-10-24 10:10:05 +00:00
|
|
|
assert "None" == \
|
|
|
|
template.Template(
|
|
|
|
'{{ as_timestamp("invalid") }}', self.hass).render()
|
2017-03-03 06:18:01 +00:00
|
|
|
self.hass.mock = None
|
2018-10-24 10:10:05 +00:00
|
|
|
assert "None" == \
|
|
|
|
template.Template('{{ as_timestamp(states.mock) }}',
|
|
|
|
self.hass).render()
|
2017-03-03 06:18:01 +00:00
|
|
|
|
|
|
|
tpl = '{{ as_timestamp(strptime("2024-02-03T09:10:24+0000", ' \
|
|
|
|
'"%Y-%m-%dT%H:%M:%S%z")) }}'
|
2018-10-24 10:10:05 +00:00
|
|
|
assert "1706951424.0" == \
|
|
|
|
template.Template(tpl, self.hass).render()
|
2017-03-03 06:18:01 +00:00
|
|
|
|
2017-05-23 17:32:06 +00:00
|
|
|
@patch.object(random, 'choice')
|
|
|
|
def test_random_every_time(self, test_choice):
|
|
|
|
"""Ensure the random filter runs every time, not just once."""
|
|
|
|
tpl = template.Template('{{ [1,2] | random }}', self.hass)
|
|
|
|
test_choice.return_value = 'foo'
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'foo' == tpl.render()
|
2017-05-23 17:32:06 +00:00
|
|
|
test_choice.return_value = 'bar'
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'bar' == tpl.render()
|
2017-05-23 17:32:06 +00:00
|
|
|
|
2015-12-11 05:16:05 +00:00
|
|
|
def test_passing_vars_as_keywords(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test passing variables as keywords."""
|
2018-10-24 10:10:05 +00:00
|
|
|
assert '127' == \
|
|
|
|
template.Template('{{ hello }}', self.hass).render(hello=127)
|
2015-12-11 05:16:05 +00:00
|
|
|
|
|
|
|
def test_passing_vars_as_vars(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test passing variables as variables."""
|
2018-10-24 10:10:05 +00:00
|
|
|
assert '127' == \
|
|
|
|
template.Template('{{ hello }}', self.hass).render({'hello': 127})
|
2015-12-11 05:38:35 +00:00
|
|
|
|
2018-01-19 06:13:14 +00:00
|
|
|
def test_passing_vars_as_list(self):
|
|
|
|
"""Test passing variables as list."""
|
2018-10-24 10:10:05 +00:00
|
|
|
assert "['foo', 'bar']" == \
|
2018-01-19 06:13:14 +00:00
|
|
|
template.render_complex(template.Template('{{ hello }}',
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass), {'hello': ['foo', 'bar']})
|
2018-01-19 06:13:14 +00:00
|
|
|
|
|
|
|
def test_passing_vars_as_list_element(self):
|
|
|
|
"""Test passing variables as list."""
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'bar' == \
|
2018-01-19 06:13:14 +00:00
|
|
|
template.render_complex(template.Template('{{ hello[1] }}',
|
|
|
|
self.hass),
|
2018-10-24 10:10:05 +00:00
|
|
|
{'hello': ['foo', 'bar']})
|
2018-01-19 06:13:14 +00:00
|
|
|
|
|
|
|
def test_passing_vars_as_dict_element(self):
|
|
|
|
"""Test passing variables as list."""
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'bar' == \
|
2018-01-19 06:13:14 +00:00
|
|
|
template.render_complex(template.Template('{{ hello.foo }}',
|
|
|
|
self.hass),
|
2018-10-24 10:10:05 +00:00
|
|
|
{'hello': {'foo': 'bar'}})
|
2018-01-19 06:13:14 +00:00
|
|
|
|
|
|
|
def test_passing_vars_as_dict(self):
|
|
|
|
"""Test passing variables as list."""
|
2018-10-24 10:10:05 +00:00
|
|
|
assert "{'foo': 'bar'}" == \
|
2018-01-19 06:13:14 +00:00
|
|
|
template.render_complex(template.Template('{{ hello }}',
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass), {'hello': {'foo': 'bar'}})
|
2018-01-19 06:13:14 +00:00
|
|
|
|
2015-12-11 05:38:35 +00:00
|
|
|
def test_render_with_possible_json_value_with_valid_json(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Render with possible JSON value with valid JSON."""
|
2016-09-28 04:29:55 +00:00
|
|
|
tpl = template.Template('{{ value_json.hello }}', self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'world' == \
|
|
|
|
tpl.render_with_possible_json_value('{"hello": "world"}')
|
2015-12-11 05:38:35 +00:00
|
|
|
|
|
|
|
def test_render_with_possible_json_value_with_invalid_json(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Render with possible JSON value with invalid JSON."""
|
2016-09-28 04:29:55 +00:00
|
|
|
tpl = template.Template('{{ value_json }}', self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert '' == \
|
|
|
|
tpl.render_with_possible_json_value('{ I AM NOT JSON }')
|
2015-12-12 18:35:15 +00:00
|
|
|
|
2016-02-14 21:07:21 +00:00
|
|
|
def test_render_with_possible_json_value_with_template_error_value(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Render with possible JSON value with template error value."""
|
2016-09-28 04:29:55 +00:00
|
|
|
tpl = template.Template('{{ non_existing.variable }}', self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert '-' == \
|
|
|
|
tpl.render_with_possible_json_value('hello', '-')
|
2015-12-12 18:35:15 +00:00
|
|
|
|
2016-10-15 13:38:11 +00:00
|
|
|
def test_render_with_possible_json_value_with_missing_json_value(self):
|
|
|
|
"""Render with possible JSON value with unknown JSON object."""
|
|
|
|
tpl = template.Template('{{ value_json.goodbye }}', self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert '' == \
|
|
|
|
tpl.render_with_possible_json_value('{"hello": "world"}')
|
2016-10-15 13:38:11 +00:00
|
|
|
|
|
|
|
def test_render_with_possible_json_value_valid_with_is_defined(self):
|
|
|
|
"""Render with possible JSON value with known JSON object."""
|
|
|
|
tpl = template.Template('{{ value_json.hello|is_defined }}', self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'world' == \
|
|
|
|
tpl.render_with_possible_json_value('{"hello": "world"}')
|
2016-10-15 13:38:11 +00:00
|
|
|
|
|
|
|
def test_render_with_possible_json_value_undefined_json(self):
|
|
|
|
"""Render with possible JSON value with unknown JSON object."""
|
|
|
|
tpl = template.Template('{{ value_json.bye|is_defined }}', self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert '{"hello": "world"}' == \
|
|
|
|
tpl.render_with_possible_json_value('{"hello": "world"}')
|
2016-10-15 13:38:11 +00:00
|
|
|
|
|
|
|
def test_render_with_possible_json_value_undefined_json_error_value(self):
|
|
|
|
"""Render with possible JSON value with unknown JSON object."""
|
|
|
|
tpl = template.Template('{{ value_json.bye|is_defined }}', self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert '' == \
|
|
|
|
tpl.render_with_possible_json_value('{"hello": "world"}', '')
|
2016-10-15 13:38:11 +00:00
|
|
|
|
2019-01-21 00:46:14 +00:00
|
|
|
def test_render_with_possible_json_value_non_string_value(self):
|
|
|
|
"""Render with possible JSON value with non-string value."""
|
|
|
|
tpl = template.Template("""
|
|
|
|
{{ strptime(value~'+0000', '%Y-%m-%d %H:%M:%S%z') }}
|
|
|
|
""", self.hass)
|
|
|
|
value = datetime(2019, 1, 18, 12, 13, 14)
|
|
|
|
expected = str(pytz.utc.localize(value))
|
|
|
|
assert expected == \
|
|
|
|
tpl.render_with_possible_json_value(value)
|
|
|
|
|
2015-12-12 03:07:03 +00:00
|
|
|
def test_raise_exception_on_error(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test raising an exception on error."""
|
2018-10-24 10:10:05 +00:00
|
|
|
with pytest.raises(TemplateError):
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template('{{ invalid_syntax').ensure_valid()
|
2015-12-13 06:19:12 +00:00
|
|
|
|
2015-12-13 06:19:37 +00:00
|
|
|
def test_if_state_exists(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test if state exists works."""
|
2015-12-13 06:19:37 +00:00
|
|
|
self.hass.states.set('test.object', 'available')
|
2016-09-28 04:29:55 +00:00
|
|
|
tpl = template.Template(
|
|
|
|
'{% if states.test.object %}exists{% else %}not exists{% endif %}',
|
|
|
|
self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'exists' == tpl.render()
|
2015-12-13 06:19:37 +00:00
|
|
|
|
2015-12-13 06:19:12 +00:00
|
|
|
def test_is_state(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test is_state method."""
|
2015-12-13 06:19:12 +00:00
|
|
|
self.hass.states.set('test.object', 'available')
|
2016-09-28 04:29:55 +00:00
|
|
|
tpl = template.Template("""
|
2016-02-14 21:07:21 +00:00
|
|
|
{% if is_state("test.object", "available") %}yes{% else %}no{% endif %}
|
2016-09-28 04:29:55 +00:00
|
|
|
""", self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'yes' == tpl.render()
|
2015-12-13 06:19:37 +00:00
|
|
|
|
2017-04-16 23:36:15 +00:00
|
|
|
tpl = template.Template("""
|
|
|
|
{{ is_state("test.noobject", "available") }}
|
|
|
|
""", self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'False' == tpl.render()
|
2017-04-16 23:36:15 +00:00
|
|
|
|
2015-12-31 20:58:18 +00:00
|
|
|
def test_is_state_attr(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test is_state_attr method."""
|
2015-12-31 20:58:18 +00:00
|
|
|
self.hass.states.set('test.object', 'available', {'mode': 'on'})
|
2016-09-28 04:29:55 +00:00
|
|
|
tpl = template.Template("""
|
2016-02-14 21:07:21 +00:00
|
|
|
{% if is_state_attr("test.object", "mode", "on") %}yes{% else %}no{% endif %}
|
2016-09-28 04:29:55 +00:00
|
|
|
""", self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'yes' == tpl.render()
|
2015-12-31 20:58:18 +00:00
|
|
|
|
2017-04-16 23:36:15 +00:00
|
|
|
tpl = template.Template("""
|
|
|
|
{{ is_state_attr("test.noobject", "mode", "on") }}
|
|
|
|
""", self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'False' == tpl.render()
|
2017-04-16 23:36:15 +00:00
|
|
|
|
2018-03-28 07:04:18 +00:00
|
|
|
def test_state_attr(self):
|
|
|
|
"""Test state_attr method."""
|
|
|
|
self.hass.states.set('test.object', 'available', {'mode': 'on'})
|
|
|
|
tpl = template.Template("""
|
|
|
|
{% if state_attr("test.object", "mode") == "on" %}yes{% else %}no{% endif %}
|
|
|
|
""", self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'yes' == tpl.render()
|
2018-03-28 07:04:18 +00:00
|
|
|
|
|
|
|
tpl = template.Template("""
|
|
|
|
{{ state_attr("test.noobject", "mode") == None }}
|
|
|
|
""", self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'True' == tpl.render()
|
2018-03-28 07:04:18 +00:00
|
|
|
|
2015-12-13 06:19:37 +00:00
|
|
|
def test_states_function(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test using states as a function."""
|
2015-12-13 06:19:37 +00:00
|
|
|
self.hass.states.set('test.object', 'available')
|
2016-09-28 04:29:55 +00:00
|
|
|
tpl = template.Template('{{ states("test.object") }}', self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'available' == tpl.render()
|
2016-09-28 04:29:55 +00:00
|
|
|
|
|
|
|
tpl2 = template.Template('{{ states("test.object2") }}', self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'unknown' == tpl2.render()
|
2016-02-21 04:58:01 +00:00
|
|
|
|
2016-02-23 21:06:45 +00:00
|
|
|
@patch('homeassistant.helpers.template.TemplateEnvironment.'
|
|
|
|
'is_safe_callable', return_value=True)
|
2016-09-28 04:29:55 +00:00
|
|
|
def test_now(self, mock_is_safe):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test now method."""
|
2016-09-28 04:29:55 +00:00
|
|
|
now = dt_util.now()
|
|
|
|
with patch.dict(template.ENV.globals, {'now': lambda: now}):
|
2018-10-24 10:10:05 +00:00
|
|
|
assert now.isoformat() == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template('{{ now().isoformat() }}',
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2016-02-21 04:58:01 +00:00
|
|
|
|
2016-02-23 21:06:45 +00:00
|
|
|
@patch('homeassistant.helpers.template.TemplateEnvironment.'
|
|
|
|
'is_safe_callable', return_value=True)
|
2016-09-28 04:29:55 +00:00
|
|
|
def test_utcnow(self, mock_is_safe):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test utcnow method."""
|
2016-09-28 04:29:55 +00:00
|
|
|
now = dt_util.utcnow()
|
|
|
|
with patch.dict(template.ENV.globals, {'utcnow': lambda: now}):
|
2018-10-24 10:10:05 +00:00
|
|
|
assert now.isoformat() == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template('{{ utcnow().isoformat() }}',
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2016-02-21 05:58:53 +00:00
|
|
|
|
2018-04-04 13:34:01 +00:00
|
|
|
def test_regex_match(self):
|
|
|
|
"""Test regex_match method."""
|
2018-08-20 14:34:18 +00:00
|
|
|
tpl = template.Template(r"""
|
|
|
|
{{ '123-456-7890' | regex_match('(\\d{3})-(\\d{3})-(\\d{4})') }}
|
2018-04-04 13:34:01 +00:00
|
|
|
""", self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'True' == tpl.render()
|
2018-04-04 13:34:01 +00:00
|
|
|
|
|
|
|
tpl = template.Template("""
|
|
|
|
{{ 'home assistant test' | regex_match('Home', True) }}
|
|
|
|
""", self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'True' == tpl.render()
|
2018-04-04 13:34:01 +00:00
|
|
|
|
|
|
|
tpl = template.Template("""
|
|
|
|
{{ 'Another home assistant test' | regex_match('home') }}
|
|
|
|
""", self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'False' == tpl.render()
|
2018-04-04 13:34:01 +00:00
|
|
|
|
|
|
|
def test_regex_search(self):
|
|
|
|
"""Test regex_search method."""
|
2018-08-20 14:34:18 +00:00
|
|
|
tpl = template.Template(r"""
|
|
|
|
{{ '123-456-7890' | regex_search('(\\d{3})-(\\d{3})-(\\d{4})') }}
|
2018-04-04 13:34:01 +00:00
|
|
|
""", self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'True' == tpl.render()
|
2018-04-04 13:34:01 +00:00
|
|
|
|
|
|
|
tpl = template.Template("""
|
|
|
|
{{ 'home assistant test' | regex_search('Home', True) }}
|
|
|
|
""", self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'True' == tpl.render()
|
2018-04-04 13:34:01 +00:00
|
|
|
|
|
|
|
tpl = template.Template("""
|
|
|
|
{{ 'Another home assistant test' | regex_search('home') }}
|
|
|
|
""", self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'True' == tpl.render()
|
2018-04-04 13:34:01 +00:00
|
|
|
|
|
|
|
def test_regex_replace(self):
|
|
|
|
"""Test regex_replace method."""
|
2018-08-20 14:34:18 +00:00
|
|
|
tpl = template.Template(r"""
|
|
|
|
{{ 'Hello World' | regex_replace('(Hello\\s)',) }}
|
2018-04-04 13:34:01 +00:00
|
|
|
""", self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'World' == tpl.render()
|
2018-04-04 13:34:01 +00:00
|
|
|
|
|
|
|
def test_regex_findall_index(self):
|
|
|
|
"""Test regex_findall_index method."""
|
|
|
|
tpl = template.Template("""
|
|
|
|
{{ 'Flight from JFK to LHR' | regex_findall_index('([A-Z]{3})', 0) }}
|
|
|
|
""", self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'JFK' == tpl.render()
|
2018-04-04 13:34:01 +00:00
|
|
|
|
|
|
|
tpl = template.Template("""
|
|
|
|
{{ 'Flight from JFK to LHR' | regex_findall_index('([A-Z]{3})', 1) }}
|
|
|
|
""", self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'LHR' == tpl.render()
|
2018-04-04 13:34:01 +00:00
|
|
|
|
2018-09-26 09:57:16 +00:00
|
|
|
def test_bitwise_and(self):
|
|
|
|
"""Test bitwise_and method."""
|
|
|
|
tpl = template.Template("""
|
|
|
|
{{ 8 | bitwise_and(8) }}
|
|
|
|
""", self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert str(8 & 8) == tpl.render()
|
2018-09-26 09:57:16 +00:00
|
|
|
tpl = template.Template("""
|
|
|
|
{{ 10 | bitwise_and(2) }}
|
|
|
|
""", self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert str(10 & 2) == tpl.render()
|
2018-09-26 09:57:16 +00:00
|
|
|
tpl = template.Template("""
|
|
|
|
{{ 8 | bitwise_and(2) }}
|
|
|
|
""", self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert str(8 & 2) == tpl.render()
|
2018-09-26 09:57:16 +00:00
|
|
|
|
|
|
|
def test_bitwise_or(self):
|
|
|
|
"""Test bitwise_or method."""
|
|
|
|
tpl = template.Template("""
|
|
|
|
{{ 8 | bitwise_or(8) }}
|
|
|
|
""", self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert str(8 | 8) == tpl.render()
|
2018-09-26 09:57:16 +00:00
|
|
|
tpl = template.Template("""
|
|
|
|
{{ 10 | bitwise_or(2) }}
|
|
|
|
""", self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert str(10 | 2) == tpl.render()
|
2018-09-26 09:57:16 +00:00
|
|
|
tpl = template.Template("""
|
|
|
|
{{ 8 | bitwise_or(2) }}
|
|
|
|
""", self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert str(8 | 2) == tpl.render()
|
2018-09-26 09:57:16 +00:00
|
|
|
|
2016-02-21 05:58:53 +00:00
|
|
|
def test_distance_function_with_1_state(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test distance function with 1 state."""
|
2016-02-21 05:58:53 +00:00
|
|
|
self.hass.states.set('test.object', 'happy', {
|
|
|
|
'latitude': 32.87336,
|
|
|
|
'longitude': -117.22943,
|
|
|
|
})
|
2016-09-28 04:29:55 +00:00
|
|
|
tpl = template.Template('{{ distance(states.test.object) | round }}',
|
|
|
|
self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert '187' == tpl.render()
|
2016-02-21 05:58:53 +00:00
|
|
|
|
|
|
|
def test_distance_function_with_2_states(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test distance function with 2 states."""
|
2016-02-21 05:58:53 +00:00
|
|
|
self.hass.states.set('test.object', 'happy', {
|
|
|
|
'latitude': 32.87336,
|
|
|
|
'longitude': -117.22943,
|
|
|
|
})
|
|
|
|
self.hass.states.set('test.object_2', 'happy', {
|
|
|
|
'latitude': self.hass.config.latitude,
|
|
|
|
'longitude': self.hass.config.longitude,
|
|
|
|
})
|
2016-09-28 04:29:55 +00:00
|
|
|
tpl = template.Template(
|
|
|
|
'{{ distance(states.test.object, states.test.object_2) | round }}',
|
|
|
|
self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert '187' == tpl.render()
|
2016-02-21 05:58:53 +00:00
|
|
|
|
|
|
|
def test_distance_function_with_1_coord(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test distance function with 1 coord."""
|
2016-09-28 04:29:55 +00:00
|
|
|
tpl = template.Template(
|
|
|
|
'{{ distance("32.87336", "-117.22943") | round }}', self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert '187' == \
|
|
|
|
tpl.render()
|
2016-02-21 05:58:53 +00:00
|
|
|
|
|
|
|
def test_distance_function_with_2_coords(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test distance function with 2 coords."""
|
2018-10-24 10:10:05 +00:00
|
|
|
assert '187' == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template(
|
2016-02-21 05:58:53 +00:00
|
|
|
'{{ distance("32.87336", "-117.22943", %s, %s) | round }}'
|
2016-09-28 04:29:55 +00:00
|
|
|
% (self.hass.config.latitude, self.hass.config.longitude),
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2016-02-21 05:58:53 +00:00
|
|
|
|
|
|
|
def test_distance_function_with_1_state_1_coord(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test distance function with 1 state 1 coord."""
|
2016-02-21 05:58:53 +00:00
|
|
|
self.hass.states.set('test.object_2', 'happy', {
|
|
|
|
'latitude': self.hass.config.latitude,
|
|
|
|
'longitude': self.hass.config.longitude,
|
|
|
|
})
|
2016-09-28 04:29:55 +00:00
|
|
|
tpl = template.Template(
|
|
|
|
'{{ distance("32.87336", "-117.22943", states.test.object_2) '
|
|
|
|
'| round }}', self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert '187' == tpl.render()
|
2016-02-21 05:58:53 +00:00
|
|
|
|
2016-09-28 04:29:55 +00:00
|
|
|
tpl2 = template.Template(
|
|
|
|
'{{ distance(states.test.object_2, "32.87336", "-117.22943") '
|
|
|
|
'| round }}', self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert '187' == tpl2.render()
|
2016-02-21 05:58:53 +00:00
|
|
|
|
|
|
|
def test_distance_function_return_None_if_invalid_state(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test distance function return None if invalid state."""
|
2016-02-21 05:58:53 +00:00
|
|
|
self.hass.states.set('test.object_2', 'happy', {
|
|
|
|
'latitude': 10,
|
|
|
|
})
|
2016-09-28 04:29:55 +00:00
|
|
|
tpl = template.Template('{{ distance(states.test.object_2) | round }}',
|
|
|
|
self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'None' == \
|
|
|
|
tpl.render()
|
2016-02-21 05:58:53 +00:00
|
|
|
|
|
|
|
def test_distance_function_return_None_if_invalid_coord(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test distance function return None if invalid coord."""
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'None' == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template(
|
2018-10-24 10:10:05 +00:00
|
|
|
'{{ distance("123", "abc") }}', self.hass).render()
|
2016-02-21 19:13:40 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'None' == \
|
|
|
|
template.Template('{{ distance("123") }}', self.hass).render()
|
2016-02-21 19:13:40 +00:00
|
|
|
|
|
|
|
self.hass.states.set('test.object_2', 'happy', {
|
|
|
|
'latitude': self.hass.config.latitude,
|
|
|
|
'longitude': self.hass.config.longitude,
|
|
|
|
})
|
2016-09-28 04:29:55 +00:00
|
|
|
tpl = template.Template('{{ distance("123", states.test_object_2) }}',
|
|
|
|
self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'None' == \
|
|
|
|
tpl.render()
|
2016-02-21 19:13:40 +00:00
|
|
|
|
2018-10-10 09:49:24 +00:00
|
|
|
def test_distance_function_with_2_entity_ids(self):
|
|
|
|
"""Test distance function with 2 entity ids."""
|
|
|
|
self.hass.states.set('test.object', 'happy', {
|
|
|
|
'latitude': 32.87336,
|
|
|
|
'longitude': -117.22943,
|
|
|
|
})
|
|
|
|
self.hass.states.set('test.object_2', 'happy', {
|
|
|
|
'latitude': self.hass.config.latitude,
|
|
|
|
'longitude': self.hass.config.longitude,
|
|
|
|
})
|
|
|
|
tpl = template.Template(
|
|
|
|
'{{ distance("test.object", "test.object_2") | round }}',
|
|
|
|
self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert '187' == tpl.render()
|
2018-10-10 09:49:24 +00:00
|
|
|
|
|
|
|
def test_distance_function_with_1_entity_1_coord(self):
|
|
|
|
"""Test distance function with 1 entity_id and 1 coord."""
|
|
|
|
self.hass.states.set('test.object', 'happy', {
|
|
|
|
'latitude': self.hass.config.latitude,
|
|
|
|
'longitude': self.hass.config.longitude,
|
|
|
|
})
|
|
|
|
tpl = template.Template(
|
|
|
|
'{{ distance("test.object", "32.87336", "-117.22943") | round }}',
|
|
|
|
self.hass)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert '187' == tpl.render()
|
2018-10-10 09:49:24 +00:00
|
|
|
|
2016-02-21 19:13:40 +00:00
|
|
|
def test_closest_function_home_vs_domain(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test closest function home vs domain."""
|
2016-02-21 19:13:40 +00:00
|
|
|
self.hass.states.set('test_domain.object', 'happy', {
|
|
|
|
'latitude': self.hass.config.latitude + 0.1,
|
|
|
|
'longitude': self.hass.config.longitude + 0.1,
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.states.set('not_test_domain.but_closer', 'happy', {
|
|
|
|
'latitude': self.hass.config.latitude,
|
|
|
|
'longitude': self.hass.config.longitude,
|
|
|
|
})
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'test_domain.object' == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template('{{ closest(states.test_domain).entity_id }}',
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2016-02-21 19:13:40 +00:00
|
|
|
|
|
|
|
def test_closest_function_home_vs_all_states(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test closest function home vs all states."""
|
2016-02-21 19:13:40 +00:00
|
|
|
self.hass.states.set('test_domain.object', 'happy', {
|
|
|
|
'latitude': self.hass.config.latitude + 0.1,
|
|
|
|
'longitude': self.hass.config.longitude + 0.1,
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.states.set('test_domain_2.and_closer', 'happy', {
|
|
|
|
'latitude': self.hass.config.latitude,
|
|
|
|
'longitude': self.hass.config.longitude,
|
|
|
|
})
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'test_domain_2.and_closer' == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template('{{ closest(states).entity_id }}',
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2016-02-21 19:13:40 +00:00
|
|
|
|
|
|
|
def test_closest_function_home_vs_group_entity_id(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test closest function home vs group entity id."""
|
2016-02-21 19:13:40 +00:00
|
|
|
self.hass.states.set('test_domain.object', 'happy', {
|
|
|
|
'latitude': self.hass.config.latitude + 0.1,
|
|
|
|
'longitude': self.hass.config.longitude + 0.1,
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.states.set('not_in_group.but_closer', 'happy', {
|
|
|
|
'latitude': self.hass.config.latitude,
|
|
|
|
'longitude': self.hass.config.longitude,
|
|
|
|
})
|
|
|
|
|
2016-10-16 16:35:46 +00:00
|
|
|
group.Group.create_group(
|
|
|
|
self.hass, 'location group', ['test_domain.object'])
|
2016-02-21 19:13:40 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'test_domain.object' == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template(
|
|
|
|
'{{ closest("group.location_group").entity_id }}',
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2016-02-21 19:13:40 +00:00
|
|
|
|
|
|
|
def test_closest_function_home_vs_group_state(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test closest function home vs group state."""
|
2016-02-21 19:13:40 +00:00
|
|
|
self.hass.states.set('test_domain.object', 'happy', {
|
|
|
|
'latitude': self.hass.config.latitude + 0.1,
|
|
|
|
'longitude': self.hass.config.longitude + 0.1,
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.states.set('not_in_group.but_closer', 'happy', {
|
|
|
|
'latitude': self.hass.config.latitude,
|
|
|
|
'longitude': self.hass.config.longitude,
|
|
|
|
})
|
|
|
|
|
2016-10-16 16:35:46 +00:00
|
|
|
group.Group.create_group(
|
|
|
|
self.hass, 'location group', ['test_domain.object'])
|
2016-02-21 19:13:40 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'test_domain.object' == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template(
|
|
|
|
'{{ closest(states.group.location_group).entity_id }}',
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2016-02-21 19:13:40 +00:00
|
|
|
|
|
|
|
def test_closest_function_to_coord(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test closest function to coord."""
|
2016-02-21 19:13:40 +00:00
|
|
|
self.hass.states.set('test_domain.closest_home', 'happy', {
|
|
|
|
'latitude': self.hass.config.latitude + 0.1,
|
|
|
|
'longitude': self.hass.config.longitude + 0.1,
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.states.set('test_domain.closest_zone', 'happy', {
|
|
|
|
'latitude': self.hass.config.latitude + 0.2,
|
|
|
|
'longitude': self.hass.config.longitude + 0.2,
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.states.set('zone.far_away', 'zoning', {
|
|
|
|
'latitude': self.hass.config.latitude + 0.3,
|
|
|
|
'longitude': self.hass.config.longitude + 0.3,
|
|
|
|
})
|
|
|
|
|
2016-09-28 04:29:55 +00:00
|
|
|
tpl = template.Template(
|
|
|
|
'{{ closest("%s", %s, states.test_domain).entity_id }}'
|
|
|
|
% (self.hass.config.latitude + 0.3,
|
|
|
|
self.hass.config.longitude + 0.3), self.hass)
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'test_domain.closest_zone' == \
|
|
|
|
tpl.render()
|
2016-02-21 19:13:40 +00:00
|
|
|
|
|
|
|
def test_closest_function_to_entity_id(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test closest function to entity id."""
|
2016-02-21 19:13:40 +00:00
|
|
|
self.hass.states.set('test_domain.closest_home', 'happy', {
|
|
|
|
'latitude': self.hass.config.latitude + 0.1,
|
|
|
|
'longitude': self.hass.config.longitude + 0.1,
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.states.set('test_domain.closest_zone', 'happy', {
|
|
|
|
'latitude': self.hass.config.latitude + 0.2,
|
|
|
|
'longitude': self.hass.config.longitude + 0.2,
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.states.set('zone.far_away', 'zoning', {
|
|
|
|
'latitude': self.hass.config.latitude + 0.3,
|
|
|
|
'longitude': self.hass.config.longitude + 0.3,
|
|
|
|
})
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'test_domain.closest_zone' == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template(
|
|
|
|
'{{ closest("zone.far_away", '
|
2018-10-24 10:10:05 +00:00
|
|
|
'states.test_domain).entity_id }}', self.hass).render()
|
2016-02-21 19:13:40 +00:00
|
|
|
|
|
|
|
def test_closest_function_to_state(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test closest function to state."""
|
2016-02-21 19:13:40 +00:00
|
|
|
self.hass.states.set('test_domain.closest_home', 'happy', {
|
|
|
|
'latitude': self.hass.config.latitude + 0.1,
|
|
|
|
'longitude': self.hass.config.longitude + 0.1,
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.states.set('test_domain.closest_zone', 'happy', {
|
|
|
|
'latitude': self.hass.config.latitude + 0.2,
|
|
|
|
'longitude': self.hass.config.longitude + 0.2,
|
|
|
|
})
|
|
|
|
|
|
|
|
self.hass.states.set('zone.far_away', 'zoning', {
|
|
|
|
'latitude': self.hass.config.latitude + 0.3,
|
|
|
|
'longitude': self.hass.config.longitude + 0.3,
|
|
|
|
})
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'test_domain.closest_zone' == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template(
|
2016-02-21 19:13:40 +00:00
|
|
|
'{{ closest(states.zone.far_away, '
|
2018-10-24 10:10:05 +00:00
|
|
|
'states.test_domain).entity_id }}', self.hass).render()
|
2016-02-21 19:13:40 +00:00
|
|
|
|
|
|
|
def test_closest_function_invalid_state(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test closest function invalid state."""
|
2016-02-21 19:13:40 +00:00
|
|
|
self.hass.states.set('test_domain.closest_home', 'happy', {
|
|
|
|
'latitude': self.hass.config.latitude + 0.1,
|
|
|
|
'longitude': self.hass.config.longitude + 0.1,
|
|
|
|
})
|
|
|
|
|
|
|
|
for state in ('states.zone.non_existing', '"zone.non_existing"'):
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'None' == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template('{{ closest(%s, states) }}' % state,
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2016-02-21 19:13:40 +00:00
|
|
|
|
|
|
|
def test_closest_function_state_with_invalid_location(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test closest function state with invalid location."""
|
2016-02-21 19:13:40 +00:00
|
|
|
self.hass.states.set('test_domain.closest_home', 'happy', {
|
|
|
|
'latitude': 'invalid latitude',
|
|
|
|
'longitude': self.hass.config.longitude + 0.1,
|
|
|
|
})
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'None' == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template(
|
2016-02-21 19:13:40 +00:00
|
|
|
'{{ closest(states.test_domain.closest_home, '
|
2018-10-24 10:10:05 +00:00
|
|
|
'states) }}', self.hass).render()
|
2016-02-21 19:13:40 +00:00
|
|
|
|
|
|
|
def test_closest_function_invalid_coordinates(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test closest function invalid coordinates."""
|
2016-02-21 19:13:40 +00:00
|
|
|
self.hass.states.set('test_domain.closest_home', 'happy', {
|
|
|
|
'latitude': self.hass.config.latitude + 0.1,
|
|
|
|
'longitude': self.hass.config.longitude + 0.1,
|
|
|
|
})
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 'None' == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.Template('{{ closest("invalid", "coord", states) }}',
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2016-02-21 19:13:40 +00:00
|
|
|
|
|
|
|
def test_closest_function_no_location_states(self):
|
2016-09-25 20:33:01 +00:00
|
|
|
"""Test closest function without location states."""
|
2018-10-24 10:10:05 +00:00
|
|
|
assert '' == \
|
2017-08-18 06:19:35 +00:00
|
|
|
template.Template('{{ closest(states).entity_id }}',
|
2018-10-24 10:10:05 +00:00
|
|
|
self.hass).render()
|
2016-09-28 04:29:55 +00:00
|
|
|
|
|
|
|
def test_extract_entities_none_exclude_stuff(self):
|
|
|
|
"""Test extract entities function with none or exclude stuff."""
|
2018-10-30 11:03:27 +00:00
|
|
|
assert [] == template.extract_entities(None)
|
|
|
|
|
|
|
|
assert [] == template.extract_entities("mdi:water")
|
2016-09-28 04:29:55 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert MATCH_ALL == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.extract_entities(
|
|
|
|
'{{ closest(states.zone.far_away, '
|
2018-10-24 10:10:05 +00:00
|
|
|
'states.test_domain).entity_id }}')
|
2016-09-28 04:29:55 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert MATCH_ALL == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.extract_entities(
|
2018-10-24 10:10:05 +00:00
|
|
|
'{{ distance("123", states.test_object_2) }}')
|
2016-09-28 04:29:55 +00:00
|
|
|
|
|
|
|
def test_extract_entities_no_match_entities(self):
|
|
|
|
"""Test extract entities function with none entities stuff."""
|
2018-10-24 10:10:05 +00:00
|
|
|
assert MATCH_ALL == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.extract_entities(
|
2018-10-24 10:10:05 +00:00
|
|
|
"{{ value_json.tst | timestamp_custom('%Y' True) }}")
|
2016-09-28 04:29:55 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert MATCH_ALL == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.extract_entities("""
|
|
|
|
{% for state in states.sensor %}
|
2017-10-12 14:57:18 +00:00
|
|
|
{{ state.entity_id }}={{ state.state }},d
|
2016-09-28 04:29:55 +00:00
|
|
|
{% endfor %}
|
2018-10-24 10:10:05 +00:00
|
|
|
""")
|
2016-09-28 04:29:55 +00:00
|
|
|
|
|
|
|
def test_extract_entities_match_entities(self):
|
|
|
|
"""Test extract entities function with entities stuff."""
|
2018-10-24 10:10:05 +00:00
|
|
|
assert ['device_tracker.phone_1'] == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.extract_entities("""
|
|
|
|
{% if is_state('device_tracker.phone_1', 'home') %}
|
|
|
|
Ha, Hercules is home!
|
|
|
|
{% else %}
|
|
|
|
Hercules is at {{ states('device_tracker.phone_1') }}.
|
|
|
|
{% endif %}
|
2018-10-24 10:10:05 +00:00
|
|
|
""")
|
2016-09-28 04:29:55 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert ['binary_sensor.garage_door'] == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.extract_entities("""
|
|
|
|
{{ as_timestamp(states.binary_sensor.garage_door.last_changed) }}
|
2018-10-24 10:10:05 +00:00
|
|
|
""")
|
2016-09-28 04:29:55 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert ['binary_sensor.garage_door'] == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.extract_entities("""
|
|
|
|
{{ states("binary_sensor.garage_door") }}
|
2018-10-24 10:10:05 +00:00
|
|
|
""")
|
2016-09-28 04:29:55 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert ['device_tracker.phone_2'] == \
|
2016-09-28 04:29:55 +00:00
|
|
|
template.extract_entities("""
|
2018-10-30 11:03:27 +00:00
|
|
|
{{ is_state_attr('device_tracker.phone_2', 'battery', 40) }}
|
2018-10-24 10:10:05 +00:00
|
|
|
""")
|
2016-09-28 04:29:55 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert sorted([
|
2016-09-28 04:29:55 +00:00
|
|
|
'device_tracker.phone_1',
|
|
|
|
'device_tracker.phone_2',
|
2018-10-24 10:10:05 +00:00
|
|
|
]) == \
|
2016-09-28 04:29:55 +00:00
|
|
|
sorted(template.extract_entities("""
|
|
|
|
{% if is_state('device_tracker.phone_1', 'home') %}
|
|
|
|
Ha, Hercules is home!
|
|
|
|
{% elif states.device_tracker.phone_2.attributes.battery < 40 %}
|
|
|
|
Hercules you power goes done!.
|
|
|
|
{% endif %}
|
2018-10-24 10:10:05 +00:00
|
|
|
"""))
|
2016-09-28 04:29:55 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert sorted([
|
2016-09-28 04:29:55 +00:00
|
|
|
'sensor.pick_humidity',
|
|
|
|
'sensor.pick_temperature',
|
2018-10-24 10:10:05 +00:00
|
|
|
]) == \
|
2016-09-28 04:29:55 +00:00
|
|
|
sorted(template.extract_entities("""
|
|
|
|
{{
|
|
|
|
states.sensor.pick_temperature.state ~ „°C (“ ~
|
|
|
|
states.sensor.pick_humidity.state ~ „ %“
|
|
|
|
}}
|
2018-10-24 10:10:05 +00:00
|
|
|
"""))
|
2017-02-05 13:24:38 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert sorted([
|
2017-02-05 13:24:38 +00:00
|
|
|
'sensor.luftfeuchtigkeit_mean',
|
2017-10-03 19:34:13 +00:00
|
|
|
'input_number.luftfeuchtigkeit',
|
2018-10-24 10:10:05 +00:00
|
|
|
]) == \
|
2017-02-05 13:24:38 +00:00
|
|
|
sorted(template.extract_entities(
|
|
|
|
"{% if (states('sensor.luftfeuchtigkeit_mean') | int)"
|
2017-10-03 19:34:13 +00:00
|
|
|
" > (states('input_number.luftfeuchtigkeit') | int +1.5)"
|
2017-02-05 13:24:38 +00:00
|
|
|
" %}true{% endif %}"
|
2018-10-24 10:10:05 +00:00
|
|
|
))
|
2017-08-18 06:19:35 +00:00
|
|
|
|
2017-10-12 14:57:18 +00:00
|
|
|
def test_extract_entities_with_variables(self):
|
|
|
|
"""Test extract entities function with variables and entities stuff."""
|
2018-10-24 10:10:05 +00:00
|
|
|
assert ['input_boolean.switch'] == \
|
2017-10-12 14:57:18 +00:00
|
|
|
template.extract_entities(
|
2018-10-24 10:10:05 +00:00
|
|
|
"{{ is_state('input_boolean.switch', 'off') }}", {})
|
2017-10-12 14:57:18 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert ['trigger.entity_id'] == \
|
2017-10-12 14:57:18 +00:00
|
|
|
template.extract_entities(
|
2018-10-24 10:10:05 +00:00
|
|
|
"{{ is_state(trigger.entity_id, 'off') }}", {})
|
2017-10-12 14:57:18 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert MATCH_ALL == \
|
2017-10-12 14:57:18 +00:00
|
|
|
template.extract_entities(
|
2018-10-24 10:10:05 +00:00
|
|
|
"{{ is_state(data, 'off') }}", {})
|
2017-10-12 14:57:18 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert ['input_boolean.switch'] == \
|
2017-10-12 14:57:18 +00:00
|
|
|
template.extract_entities(
|
|
|
|
"{{ is_state(data, 'off') }}",
|
2018-10-24 10:10:05 +00:00
|
|
|
{'data': 'input_boolean.switch'})
|
2017-10-12 14:57:18 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert ['input_boolean.switch'] == \
|
2017-10-12 14:57:18 +00:00
|
|
|
template.extract_entities(
|
|
|
|
"{{ is_state(trigger.entity_id, 'off') }}",
|
2018-10-24 10:10:05 +00:00
|
|
|
{'trigger': {'entity_id': 'input_boolean.switch'}})
|
2017-10-12 14:57:18 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert MATCH_ALL == \
|
2018-03-25 10:51:11 +00:00
|
|
|
template.extract_entities(
|
|
|
|
"{{ is_state('media_player.' ~ where , 'playing') }}",
|
2018-10-24 10:10:05 +00:00
|
|
|
{'where': 'livingroom'})
|
2018-03-25 10:51:11 +00:00
|
|
|
|
2018-10-30 18:13:20 +00:00
|
|
|
def test_jinja_namespace(self):
|
|
|
|
"""Test Jinja's namespace command can be used."""
|
|
|
|
test_template = template.Template(
|
|
|
|
(
|
|
|
|
"{% set ns = namespace(a_key='') %}"
|
|
|
|
"{% set ns.a_key = states.sensor.dummy.state %}"
|
|
|
|
"{{ ns.a_key }}"
|
|
|
|
),
|
|
|
|
self.hass
|
|
|
|
)
|
|
|
|
|
|
|
|
self.hass.states.set('sensor.dummy', 'a value')
|
|
|
|
assert 'a value' == test_template.render()
|
|
|
|
|
|
|
|
self.hass.states.set('sensor.dummy', 'another value')
|
|
|
|
assert 'another value' == test_template.render()
|
|
|
|
|
2017-08-18 06:19:35 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_state_with_unit(hass):
|
|
|
|
"""Test the state_with_unit property helper."""
|
|
|
|
hass.states.async_set('sensor.test', '23', {
|
|
|
|
'unit_of_measurement': 'beers',
|
|
|
|
})
|
|
|
|
hass.states.async_set('sensor.test2', 'wow')
|
|
|
|
|
|
|
|
tpl = template.Template(
|
|
|
|
'{{ states.sensor.test.state_with_unit }}', hass)
|
|
|
|
|
|
|
|
assert tpl.async_render() == '23 beers'
|
|
|
|
|
|
|
|
tpl = template.Template(
|
|
|
|
'{{ states.sensor.test2.state_with_unit }}', hass)
|
|
|
|
|
|
|
|
assert tpl.async_render() == 'wow'
|
|
|
|
|
|
|
|
tpl = template.Template(
|
|
|
|
'{% for state in states %}{{ state.state_with_unit }} {% endfor %}',
|
|
|
|
hass)
|
|
|
|
|
|
|
|
assert tpl.async_render() == '23 beers wow'
|
|
|
|
|
|
|
|
tpl = template.Template('{{ states.sensor.non_existing.state_with_unit }}',
|
|
|
|
hass)
|
|
|
|
|
|
|
|
assert tpl.async_render() == ''
|
2017-08-27 16:33:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_length_of_states(hass):
|
|
|
|
"""Test fetching the length of states."""
|
|
|
|
hass.states.async_set('sensor.test', '23')
|
|
|
|
hass.states.async_set('sensor.test2', 'wow')
|
|
|
|
hass.states.async_set('climate.test2', 'cooling')
|
|
|
|
|
|
|
|
tpl = template.Template('{{ states | length }}', hass)
|
|
|
|
assert tpl.async_render() == '3'
|
|
|
|
|
|
|
|
tpl = template.Template('{{ states.sensor | length }}', hass)
|
|
|
|
assert tpl.async_render() == '2'
|