Pytest tests (#17750)
* Convert core tests
* Convert component tests to use pytest assert
* Lint 🤷♂️
* Fix test
* Fix 3 typos in docs
This commit is contained in:
parent
4222f7562b
commit
08fe7c3ece
223 changed files with 6747 additions and 7237 deletions
|
@ -20,6 +20,7 @@ from homeassistant.const import (
|
|||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
import pytest
|
||||
|
||||
|
||||
class TestHelpersTemplate(unittest.TestCase):
|
||||
|
@ -41,21 +42,19 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
def test_referring_states_by_entity_id(self):
|
||||
"""Test referring states by entity id."""
|
||||
self.hass.states.set('test.object', 'happy')
|
||||
self.assertEqual(
|
||||
'happy',
|
||||
assert 'happy' == \
|
||||
template.Template(
|
||||
'{{ states.test.object.state }}', self.hass).render())
|
||||
'{{ states.test.object.state }}', self.hass).render()
|
||||
|
||||
def test_iterating_all_states(self):
|
||||
"""Test iterating all states."""
|
||||
self.hass.states.set('test.object', 'happy')
|
||||
self.hass.states.set('sensor.temperature', 10)
|
||||
|
||||
self.assertEqual(
|
||||
'10happy',
|
||||
assert '10happy' == \
|
||||
template.Template(
|
||||
'{% for state in states %}{{ state.state }}{% endfor %}',
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
def test_iterating_domain_states(self):
|
||||
"""Test iterating domain states."""
|
||||
|
@ -63,54 +62,47 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
self.hass.states.set('sensor.back_door', 'open')
|
||||
self.hass.states.set('sensor.temperature', 10)
|
||||
|
||||
self.assertEqual(
|
||||
'open10',
|
||||
assert 'open10' == \
|
||||
template.Template("""
|
||||
{% for state in states.sensor %}{{ state.state }}{% endfor %}
|
||||
""", self.hass).render())
|
||||
""", self.hass).render()
|
||||
|
||||
def test_float(self):
|
||||
"""Test float."""
|
||||
self.hass.states.set('sensor.temperature', '12')
|
||||
|
||||
self.assertEqual(
|
||||
'12.0',
|
||||
assert '12.0' == \
|
||||
template.Template(
|
||||
'{{ float(states.sensor.temperature.state) }}',
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
self.assertEqual(
|
||||
'True',
|
||||
assert 'True' == \
|
||||
template.Template(
|
||||
'{{ float(states.sensor.temperature.state) > 11 }}',
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
def test_rounding_value(self):
|
||||
"""Test rounding value."""
|
||||
self.hass.states.set('sensor.temperature', 12.78)
|
||||
|
||||
self.assertEqual(
|
||||
'12.8',
|
||||
assert '12.8' == \
|
||||
template.Template(
|
||||
'{{ states.sensor.temperature.state | round(1) }}',
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
self.assertEqual(
|
||||
'128',
|
||||
assert '128' == \
|
||||
template.Template(
|
||||
'{{ states.sensor.temperature.state | multiply(10) | round }}',
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
def test_rounding_value_get_original_value_on_error(self):
|
||||
"""Test rounding value get original value on error."""
|
||||
self.assertEqual(
|
||||
'None',
|
||||
template.Template('{{ None | round }}', self.hass).render())
|
||||
assert 'None' == \
|
||||
template.Template('{{ None | round }}', self.hass).render()
|
||||
|
||||
self.assertEqual(
|
||||
'no_number',
|
||||
assert 'no_number' == \
|
||||
template.Template(
|
||||
'{{ "no_number" | round }}', self.hass).render())
|
||||
'{{ "no_number" | round }}', self.hass).render()
|
||||
|
||||
def test_multiply(self):
|
||||
"""Test multiply."""
|
||||
|
@ -121,10 +113,9 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
}
|
||||
|
||||
for inp, out in tests.items():
|
||||
self.assertEqual(
|
||||
out,
|
||||
assert out == \
|
||||
template.Template('{{ %s | multiply(10) | round }}' % inp,
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
def test_logarithm(self):
|
||||
"""Test logarithm."""
|
||||
|
@ -137,17 +128,15 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
]
|
||||
|
||||
for value, base, expected in tests:
|
||||
self.assertEqual(
|
||||
expected,
|
||||
assert expected == \
|
||||
template.Template(
|
||||
'{{ %s | log(%s) | round(1) }}' % (value, base),
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
self.assertEqual(
|
||||
expected,
|
||||
assert expected == \
|
||||
template.Template(
|
||||
'{{ log(%s, %s) | round(1) }}' % (value, base),
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
def test_sine(self):
|
||||
"""Test sine."""
|
||||
|
@ -160,11 +149,10 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
]
|
||||
|
||||
for value, expected in tests:
|
||||
self.assertEqual(
|
||||
expected,
|
||||
assert expected == \
|
||||
template.Template(
|
||||
'{{ %s | sin | round(3) }}' % value,
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
def test_cos(self):
|
||||
"""Test cosine."""
|
||||
|
@ -177,11 +165,10 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
]
|
||||
|
||||
for value, expected in tests:
|
||||
self.assertEqual(
|
||||
expected,
|
||||
assert expected == \
|
||||
template.Template(
|
||||
'{{ %s | cos | round(3) }}' % value,
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
def test_tan(self):
|
||||
"""Test tangent."""
|
||||
|
@ -194,11 +181,10 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
]
|
||||
|
||||
for value, expected in tests:
|
||||
self.assertEqual(
|
||||
expected,
|
||||
assert expected == \
|
||||
template.Template(
|
||||
'{{ %s | tan | round(3) }}' % value,
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
def test_sqrt(self):
|
||||
"""Test square root."""
|
||||
|
@ -211,11 +197,10 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
]
|
||||
|
||||
for value, expected in tests:
|
||||
self.assertEqual(
|
||||
expected,
|
||||
assert expected == \
|
||||
template.Template(
|
||||
'{{ %s | sqrt | round(3) }}' % value,
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
def test_strptime(self):
|
||||
"""Test the parse timestamp method."""
|
||||
|
@ -239,9 +224,8 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
|
||||
temp = '{{ strptime(\'%s\', \'%s\') }}' % (inp, fmt)
|
||||
|
||||
self.assertEqual(
|
||||
str(expected),
|
||||
template.Template(temp, self.hass).render())
|
||||
assert str(expected) == \
|
||||
template.Template(temp, self.hass).render()
|
||||
|
||||
def test_timestamp_custom(self):
|
||||
"""Test the timestamps to custom filter."""
|
||||
|
@ -263,10 +247,8 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
else:
|
||||
fil = 'timestamp_custom'
|
||||
|
||||
self.assertEqual(
|
||||
out,
|
||||
template.Template('{{ %s | %s }}' % (inp, fil),
|
||||
self.hass).render())
|
||||
assert out == template.Template(
|
||||
'{{ %s | %s }}' % (inp, fil), self.hass).render()
|
||||
|
||||
def test_timestamp_local(self):
|
||||
"""Test the timestamps to local filter."""
|
||||
|
@ -276,24 +258,21 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
}
|
||||
|
||||
for inp, out in tests.items():
|
||||
self.assertEqual(
|
||||
out,
|
||||
assert out == \
|
||||
template.Template('{{ %s | timestamp_local }}' % inp,
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
def test_min(self):
|
||||
"""Test the min filter."""
|
||||
self.assertEqual(
|
||||
'1',
|
||||
assert '1' == \
|
||||
template.Template('{{ [1, 2, 3] | min }}',
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
def test_max(self):
|
||||
"""Test the max filter."""
|
||||
self.assertEqual(
|
||||
'3',
|
||||
assert '3' == \
|
||||
template.Template('{{ [1, 2, 3] | max }}',
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
def test_timestamp_utc(self):
|
||||
"""Test the timestamps to local filter."""
|
||||
|
@ -306,129 +285,115 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
}
|
||||
|
||||
for inp, out in tests.items():
|
||||
self.assertEqual(
|
||||
out,
|
||||
assert out == \
|
||||
template.Template('{{ %s | timestamp_utc }}' % inp,
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
def test_as_timestamp(self):
|
||||
"""Test the as_timestamp function."""
|
||||
self.assertEqual("None",
|
||||
template.Template('{{ as_timestamp("invalid") }}',
|
||||
self.hass).render())
|
||||
assert "None" == \
|
||||
template.Template(
|
||||
'{{ as_timestamp("invalid") }}', self.hass).render()
|
||||
self.hass.mock = None
|
||||
self.assertEqual("None",
|
||||
template.Template('{{ as_timestamp(states.mock) }}',
|
||||
self.hass).render())
|
||||
assert "None" == \
|
||||
template.Template('{{ as_timestamp(states.mock) }}',
|
||||
self.hass).render()
|
||||
|
||||
tpl = '{{ as_timestamp(strptime("2024-02-03T09:10:24+0000", ' \
|
||||
'"%Y-%m-%dT%H:%M:%S%z")) }}'
|
||||
self.assertEqual("1706951424.0",
|
||||
template.Template(tpl, self.hass).render())
|
||||
assert "1706951424.0" == \
|
||||
template.Template(tpl, self.hass).render()
|
||||
|
||||
@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'
|
||||
self.assertEqual('foo', tpl.render())
|
||||
assert 'foo' == tpl.render()
|
||||
test_choice.return_value = 'bar'
|
||||
self.assertEqual('bar', tpl.render())
|
||||
assert 'bar' == tpl.render()
|
||||
|
||||
def test_passing_vars_as_keywords(self):
|
||||
"""Test passing variables as keywords."""
|
||||
self.assertEqual(
|
||||
'127',
|
||||
template.Template('{{ hello }}', self.hass).render(hello=127))
|
||||
assert '127' == \
|
||||
template.Template('{{ hello }}', self.hass).render(hello=127)
|
||||
|
||||
def test_passing_vars_as_vars(self):
|
||||
"""Test passing variables as variables."""
|
||||
self.assertEqual(
|
||||
'127',
|
||||
template.Template('{{ hello }}', self.hass).render({'hello': 127}))
|
||||
assert '127' == \
|
||||
template.Template('{{ hello }}', self.hass).render({'hello': 127})
|
||||
|
||||
def test_passing_vars_as_list(self):
|
||||
"""Test passing variables as list."""
|
||||
self.assertEqual(
|
||||
"['foo', 'bar']",
|
||||
assert "['foo', 'bar']" == \
|
||||
template.render_complex(template.Template('{{ hello }}',
|
||||
self.hass), {'hello': ['foo', 'bar']}))
|
||||
self.hass), {'hello': ['foo', 'bar']})
|
||||
|
||||
def test_passing_vars_as_list_element(self):
|
||||
"""Test passing variables as list."""
|
||||
self.assertEqual(
|
||||
'bar',
|
||||
assert 'bar' == \
|
||||
template.render_complex(template.Template('{{ hello[1] }}',
|
||||
self.hass),
|
||||
{'hello': ['foo', 'bar']}))
|
||||
{'hello': ['foo', 'bar']})
|
||||
|
||||
def test_passing_vars_as_dict_element(self):
|
||||
"""Test passing variables as list."""
|
||||
self.assertEqual(
|
||||
'bar',
|
||||
assert 'bar' == \
|
||||
template.render_complex(template.Template('{{ hello.foo }}',
|
||||
self.hass),
|
||||
{'hello': {'foo': 'bar'}}))
|
||||
{'hello': {'foo': 'bar'}})
|
||||
|
||||
def test_passing_vars_as_dict(self):
|
||||
"""Test passing variables as list."""
|
||||
self.assertEqual(
|
||||
"{'foo': 'bar'}",
|
||||
assert "{'foo': 'bar'}" == \
|
||||
template.render_complex(template.Template('{{ hello }}',
|
||||
self.hass), {'hello': {'foo': 'bar'}}))
|
||||
self.hass), {'hello': {'foo': 'bar'}})
|
||||
|
||||
def test_render_with_possible_json_value_with_valid_json(self):
|
||||
"""Render with possible JSON value with valid JSON."""
|
||||
tpl = template.Template('{{ value_json.hello }}', self.hass)
|
||||
self.assertEqual(
|
||||
'world',
|
||||
tpl.render_with_possible_json_value('{"hello": "world"}'))
|
||||
assert 'world' == \
|
||||
tpl.render_with_possible_json_value('{"hello": "world"}')
|
||||
|
||||
def test_render_with_possible_json_value_with_invalid_json(self):
|
||||
"""Render with possible JSON value with invalid JSON."""
|
||||
tpl = template.Template('{{ value_json }}', self.hass)
|
||||
self.assertEqual(
|
||||
'',
|
||||
tpl.render_with_possible_json_value('{ I AM NOT JSON }'))
|
||||
assert '' == \
|
||||
tpl.render_with_possible_json_value('{ I AM NOT JSON }')
|
||||
|
||||
def test_render_with_possible_json_value_with_template_error_value(self):
|
||||
"""Render with possible JSON value with template error value."""
|
||||
tpl = template.Template('{{ non_existing.variable }}', self.hass)
|
||||
self.assertEqual(
|
||||
'-',
|
||||
tpl.render_with_possible_json_value('hello', '-'))
|
||||
assert '-' == \
|
||||
tpl.render_with_possible_json_value('hello', '-')
|
||||
|
||||
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)
|
||||
self.assertEqual(
|
||||
'',
|
||||
tpl.render_with_possible_json_value('{"hello": "world"}'))
|
||||
assert '' == \
|
||||
tpl.render_with_possible_json_value('{"hello": "world"}')
|
||||
|
||||
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)
|
||||
self.assertEqual(
|
||||
'world',
|
||||
tpl.render_with_possible_json_value('{"hello": "world"}'))
|
||||
assert 'world' == \
|
||||
tpl.render_with_possible_json_value('{"hello": "world"}')
|
||||
|
||||
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)
|
||||
self.assertEqual(
|
||||
'{"hello": "world"}',
|
||||
tpl.render_with_possible_json_value('{"hello": "world"}'))
|
||||
assert '{"hello": "world"}' == \
|
||||
tpl.render_with_possible_json_value('{"hello": "world"}')
|
||||
|
||||
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)
|
||||
self.assertEqual(
|
||||
'',
|
||||
tpl.render_with_possible_json_value('{"hello": "world"}', ''))
|
||||
assert '' == \
|
||||
tpl.render_with_possible_json_value('{"hello": "world"}', '')
|
||||
|
||||
def test_raise_exception_on_error(self):
|
||||
"""Test raising an exception on error."""
|
||||
with self.assertRaises(TemplateError):
|
||||
with pytest.raises(TemplateError):
|
||||
template.Template('{{ invalid_syntax').ensure_valid()
|
||||
|
||||
def test_if_state_exists(self):
|
||||
|
@ -437,7 +402,7 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
tpl = template.Template(
|
||||
'{% if states.test.object %}exists{% else %}not exists{% endif %}',
|
||||
self.hass)
|
||||
self.assertEqual('exists', tpl.render())
|
||||
assert 'exists' == tpl.render()
|
||||
|
||||
def test_is_state(self):
|
||||
"""Test is_state method."""
|
||||
|
@ -445,12 +410,12 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
tpl = template.Template("""
|
||||
{% if is_state("test.object", "available") %}yes{% else %}no{% endif %}
|
||||
""", self.hass)
|
||||
self.assertEqual('yes', tpl.render())
|
||||
assert 'yes' == tpl.render()
|
||||
|
||||
tpl = template.Template("""
|
||||
{{ is_state("test.noobject", "available") }}
|
||||
""", self.hass)
|
||||
self.assertEqual('False', tpl.render())
|
||||
assert 'False' == tpl.render()
|
||||
|
||||
def test_is_state_attr(self):
|
||||
"""Test is_state_attr method."""
|
||||
|
@ -458,12 +423,12 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
tpl = template.Template("""
|
||||
{% if is_state_attr("test.object", "mode", "on") %}yes{% else %}no{% endif %}
|
||||
""", self.hass)
|
||||
self.assertEqual('yes', tpl.render())
|
||||
assert 'yes' == tpl.render()
|
||||
|
||||
tpl = template.Template("""
|
||||
{{ is_state_attr("test.noobject", "mode", "on") }}
|
||||
""", self.hass)
|
||||
self.assertEqual('False', tpl.render())
|
||||
assert 'False' == tpl.render()
|
||||
|
||||
def test_state_attr(self):
|
||||
"""Test state_attr method."""
|
||||
|
@ -471,21 +436,21 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
tpl = template.Template("""
|
||||
{% if state_attr("test.object", "mode") == "on" %}yes{% else %}no{% endif %}
|
||||
""", self.hass)
|
||||
self.assertEqual('yes', tpl.render())
|
||||
assert 'yes' == tpl.render()
|
||||
|
||||
tpl = template.Template("""
|
||||
{{ state_attr("test.noobject", "mode") == None }}
|
||||
""", self.hass)
|
||||
self.assertEqual('True', tpl.render())
|
||||
assert 'True' == tpl.render()
|
||||
|
||||
def test_states_function(self):
|
||||
"""Test using states as a function."""
|
||||
self.hass.states.set('test.object', 'available')
|
||||
tpl = template.Template('{{ states("test.object") }}', self.hass)
|
||||
self.assertEqual('available', tpl.render())
|
||||
assert 'available' == tpl.render()
|
||||
|
||||
tpl2 = template.Template('{{ states("test.object2") }}', self.hass)
|
||||
self.assertEqual('unknown', tpl2.render())
|
||||
assert 'unknown' == tpl2.render()
|
||||
|
||||
@patch('homeassistant.helpers.template.TemplateEnvironment.'
|
||||
'is_safe_callable', return_value=True)
|
||||
|
@ -493,10 +458,9 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
"""Test now method."""
|
||||
now = dt_util.now()
|
||||
with patch.dict(template.ENV.globals, {'now': lambda: now}):
|
||||
self.assertEqual(
|
||||
now.isoformat(),
|
||||
assert now.isoformat() == \
|
||||
template.Template('{{ now().isoformat() }}',
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
@patch('homeassistant.helpers.template.TemplateEnvironment.'
|
||||
'is_safe_callable', return_value=True)
|
||||
|
@ -504,93 +468,92 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
"""Test utcnow method."""
|
||||
now = dt_util.utcnow()
|
||||
with patch.dict(template.ENV.globals, {'utcnow': lambda: now}):
|
||||
self.assertEqual(
|
||||
now.isoformat(),
|
||||
assert now.isoformat() == \
|
||||
template.Template('{{ utcnow().isoformat() }}',
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
def test_regex_match(self):
|
||||
"""Test regex_match method."""
|
||||
tpl = template.Template(r"""
|
||||
{{ '123-456-7890' | regex_match('(\\d{3})-(\\d{3})-(\\d{4})') }}
|
||||
""", self.hass)
|
||||
self.assertEqual('True', tpl.render())
|
||||
assert 'True' == tpl.render()
|
||||
|
||||
tpl = template.Template("""
|
||||
{{ 'home assistant test' | regex_match('Home', True) }}
|
||||
""", self.hass)
|
||||
self.assertEqual('True', tpl.render())
|
||||
assert 'True' == tpl.render()
|
||||
|
||||
tpl = template.Template("""
|
||||
{{ 'Another home assistant test' | regex_match('home') }}
|
||||
""", self.hass)
|
||||
self.assertEqual('False', tpl.render())
|
||||
assert 'False' == tpl.render()
|
||||
|
||||
def test_regex_search(self):
|
||||
"""Test regex_search method."""
|
||||
tpl = template.Template(r"""
|
||||
{{ '123-456-7890' | regex_search('(\\d{3})-(\\d{3})-(\\d{4})') }}
|
||||
""", self.hass)
|
||||
self.assertEqual('True', tpl.render())
|
||||
assert 'True' == tpl.render()
|
||||
|
||||
tpl = template.Template("""
|
||||
{{ 'home assistant test' | regex_search('Home', True) }}
|
||||
""", self.hass)
|
||||
self.assertEqual('True', tpl.render())
|
||||
assert 'True' == tpl.render()
|
||||
|
||||
tpl = template.Template("""
|
||||
{{ 'Another home assistant test' | regex_search('home') }}
|
||||
""", self.hass)
|
||||
self.assertEqual('True', tpl.render())
|
||||
assert 'True' == tpl.render()
|
||||
|
||||
def test_regex_replace(self):
|
||||
"""Test regex_replace method."""
|
||||
tpl = template.Template(r"""
|
||||
{{ 'Hello World' | regex_replace('(Hello\\s)',) }}
|
||||
""", self.hass)
|
||||
self.assertEqual('World', tpl.render())
|
||||
assert 'World' == tpl.render()
|
||||
|
||||
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)
|
||||
self.assertEqual('JFK', tpl.render())
|
||||
assert 'JFK' == tpl.render()
|
||||
|
||||
tpl = template.Template("""
|
||||
{{ 'Flight from JFK to LHR' | regex_findall_index('([A-Z]{3})', 1) }}
|
||||
""", self.hass)
|
||||
self.assertEqual('LHR', tpl.render())
|
||||
assert 'LHR' == tpl.render()
|
||||
|
||||
def test_bitwise_and(self):
|
||||
"""Test bitwise_and method."""
|
||||
tpl = template.Template("""
|
||||
{{ 8 | bitwise_and(8) }}
|
||||
""", self.hass)
|
||||
self.assertEqual(str(8 & 8), tpl.render())
|
||||
assert str(8 & 8) == tpl.render()
|
||||
tpl = template.Template("""
|
||||
{{ 10 | bitwise_and(2) }}
|
||||
""", self.hass)
|
||||
self.assertEqual(str(10 & 2), tpl.render())
|
||||
assert str(10 & 2) == tpl.render()
|
||||
tpl = template.Template("""
|
||||
{{ 8 | bitwise_and(2) }}
|
||||
""", self.hass)
|
||||
self.assertEqual(str(8 & 2), tpl.render())
|
||||
assert str(8 & 2) == tpl.render()
|
||||
|
||||
def test_bitwise_or(self):
|
||||
"""Test bitwise_or method."""
|
||||
tpl = template.Template("""
|
||||
{{ 8 | bitwise_or(8) }}
|
||||
""", self.hass)
|
||||
self.assertEqual(str(8 | 8), tpl.render())
|
||||
assert str(8 | 8) == tpl.render()
|
||||
tpl = template.Template("""
|
||||
{{ 10 | bitwise_or(2) }}
|
||||
""", self.hass)
|
||||
self.assertEqual(str(10 | 2), tpl.render())
|
||||
assert str(10 | 2) == tpl.render()
|
||||
tpl = template.Template("""
|
||||
{{ 8 | bitwise_or(2) }}
|
||||
""", self.hass)
|
||||
self.assertEqual(str(8 | 2), tpl.render())
|
||||
assert str(8 | 2) == tpl.render()
|
||||
|
||||
def test_distance_function_with_1_state(self):
|
||||
"""Test distance function with 1 state."""
|
||||
|
@ -600,7 +563,7 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
})
|
||||
tpl = template.Template('{{ distance(states.test.object) | round }}',
|
||||
self.hass)
|
||||
self.assertEqual('187', tpl.render())
|
||||
assert '187' == tpl.render()
|
||||
|
||||
def test_distance_function_with_2_states(self):
|
||||
"""Test distance function with 2 states."""
|
||||
|
@ -615,24 +578,22 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
tpl = template.Template(
|
||||
'{{ distance(states.test.object, states.test.object_2) | round }}',
|
||||
self.hass)
|
||||
self.assertEqual('187', tpl.render())
|
||||
assert '187' == tpl.render()
|
||||
|
||||
def test_distance_function_with_1_coord(self):
|
||||
"""Test distance function with 1 coord."""
|
||||
tpl = template.Template(
|
||||
'{{ distance("32.87336", "-117.22943") | round }}', self.hass)
|
||||
self.assertEqual(
|
||||
'187',
|
||||
tpl.render())
|
||||
assert '187' == \
|
||||
tpl.render()
|
||||
|
||||
def test_distance_function_with_2_coords(self):
|
||||
"""Test distance function with 2 coords."""
|
||||
self.assertEqual(
|
||||
'187',
|
||||
assert '187' == \
|
||||
template.Template(
|
||||
'{{ distance("32.87336", "-117.22943", %s, %s) | round }}'
|
||||
% (self.hass.config.latitude, self.hass.config.longitude),
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
def test_distance_function_with_1_state_1_coord(self):
|
||||
"""Test distance function with 1 state 1 coord."""
|
||||
|
@ -643,12 +604,12 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
tpl = template.Template(
|
||||
'{{ distance("32.87336", "-117.22943", states.test.object_2) '
|
||||
'| round }}', self.hass)
|
||||
self.assertEqual('187', tpl.render())
|
||||
assert '187' == tpl.render()
|
||||
|
||||
tpl2 = template.Template(
|
||||
'{{ distance(states.test.object_2, "32.87336", "-117.22943") '
|
||||
'| round }}', self.hass)
|
||||
self.assertEqual('187', tpl2.render())
|
||||
assert '187' == tpl2.render()
|
||||
|
||||
def test_distance_function_return_None_if_invalid_state(self):
|
||||
"""Test distance function return None if invalid state."""
|
||||
|
@ -657,20 +618,17 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
})
|
||||
tpl = template.Template('{{ distance(states.test.object_2) | round }}',
|
||||
self.hass)
|
||||
self.assertEqual(
|
||||
'None',
|
||||
tpl.render())
|
||||
assert 'None' == \
|
||||
tpl.render()
|
||||
|
||||
def test_distance_function_return_None_if_invalid_coord(self):
|
||||
"""Test distance function return None if invalid coord."""
|
||||
self.assertEqual(
|
||||
'None',
|
||||
assert 'None' == \
|
||||
template.Template(
|
||||
'{{ distance("123", "abc") }}', self.hass).render())
|
||||
'{{ distance("123", "abc") }}', self.hass).render()
|
||||
|
||||
self.assertEqual(
|
||||
'None',
|
||||
template.Template('{{ distance("123") }}', self.hass).render())
|
||||
assert 'None' == \
|
||||
template.Template('{{ distance("123") }}', self.hass).render()
|
||||
|
||||
self.hass.states.set('test.object_2', 'happy', {
|
||||
'latitude': self.hass.config.latitude,
|
||||
|
@ -678,9 +636,8 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
})
|
||||
tpl = template.Template('{{ distance("123", states.test_object_2) }}',
|
||||
self.hass)
|
||||
self.assertEqual(
|
||||
'None',
|
||||
tpl.render())
|
||||
assert 'None' == \
|
||||
tpl.render()
|
||||
|
||||
def test_distance_function_with_2_entity_ids(self):
|
||||
"""Test distance function with 2 entity ids."""
|
||||
|
@ -695,7 +652,7 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
tpl = template.Template(
|
||||
'{{ distance("test.object", "test.object_2") | round }}',
|
||||
self.hass)
|
||||
self.assertEqual('187', tpl.render())
|
||||
assert '187' == tpl.render()
|
||||
|
||||
def test_distance_function_with_1_entity_1_coord(self):
|
||||
"""Test distance function with 1 entity_id and 1 coord."""
|
||||
|
@ -706,7 +663,7 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
tpl = template.Template(
|
||||
'{{ distance("test.object", "32.87336", "-117.22943") | round }}',
|
||||
self.hass)
|
||||
self.assertEqual('187', tpl.render())
|
||||
assert '187' == tpl.render()
|
||||
|
||||
def test_closest_function_home_vs_domain(self):
|
||||
"""Test closest function home vs domain."""
|
||||
|
@ -720,10 +677,9 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
'longitude': self.hass.config.longitude,
|
||||
})
|
||||
|
||||
self.assertEqual(
|
||||
'test_domain.object',
|
||||
assert 'test_domain.object' == \
|
||||
template.Template('{{ closest(states.test_domain).entity_id }}',
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
def test_closest_function_home_vs_all_states(self):
|
||||
"""Test closest function home vs all states."""
|
||||
|
@ -737,10 +693,9 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
'longitude': self.hass.config.longitude,
|
||||
})
|
||||
|
||||
self.assertEqual(
|
||||
'test_domain_2.and_closer',
|
||||
assert 'test_domain_2.and_closer' == \
|
||||
template.Template('{{ closest(states).entity_id }}',
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
def test_closest_function_home_vs_group_entity_id(self):
|
||||
"""Test closest function home vs group entity id."""
|
||||
|
@ -757,11 +712,10 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
group.Group.create_group(
|
||||
self.hass, 'location group', ['test_domain.object'])
|
||||
|
||||
self.assertEqual(
|
||||
'test_domain.object',
|
||||
assert 'test_domain.object' == \
|
||||
template.Template(
|
||||
'{{ closest("group.location_group").entity_id }}',
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
def test_closest_function_home_vs_group_state(self):
|
||||
"""Test closest function home vs group state."""
|
||||
|
@ -778,11 +732,10 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
group.Group.create_group(
|
||||
self.hass, 'location group', ['test_domain.object'])
|
||||
|
||||
self.assertEqual(
|
||||
'test_domain.object',
|
||||
assert 'test_domain.object' == \
|
||||
template.Template(
|
||||
'{{ closest(states.group.location_group).entity_id }}',
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
def test_closest_function_to_coord(self):
|
||||
"""Test closest function to coord."""
|
||||
|
@ -806,9 +759,8 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
% (self.hass.config.latitude + 0.3,
|
||||
self.hass.config.longitude + 0.3), self.hass)
|
||||
|
||||
self.assertEqual(
|
||||
'test_domain.closest_zone',
|
||||
tpl.render())
|
||||
assert 'test_domain.closest_zone' == \
|
||||
tpl.render()
|
||||
|
||||
def test_closest_function_to_entity_id(self):
|
||||
"""Test closest function to entity id."""
|
||||
|
@ -827,11 +779,10 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
'longitude': self.hass.config.longitude + 0.3,
|
||||
})
|
||||
|
||||
self.assertEqual(
|
||||
'test_domain.closest_zone',
|
||||
assert 'test_domain.closest_zone' == \
|
||||
template.Template(
|
||||
'{{ closest("zone.far_away", '
|
||||
'states.test_domain).entity_id }}', self.hass).render())
|
||||
'states.test_domain).entity_id }}', self.hass).render()
|
||||
|
||||
def test_closest_function_to_state(self):
|
||||
"""Test closest function to state."""
|
||||
|
@ -850,11 +801,10 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
'longitude': self.hass.config.longitude + 0.3,
|
||||
})
|
||||
|
||||
self.assertEqual(
|
||||
'test_domain.closest_zone',
|
||||
assert 'test_domain.closest_zone' == \
|
||||
template.Template(
|
||||
'{{ closest(states.zone.far_away, '
|
||||
'states.test_domain).entity_id }}', self.hass).render())
|
||||
'states.test_domain).entity_id }}', self.hass).render()
|
||||
|
||||
def test_closest_function_invalid_state(self):
|
||||
"""Test closest function invalid state."""
|
||||
|
@ -864,10 +814,9 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
})
|
||||
|
||||
for state in ('states.zone.non_existing', '"zone.non_existing"'):
|
||||
self.assertEqual(
|
||||
'None',
|
||||
assert 'None' == \
|
||||
template.Template('{{ closest(%s, states) }}' % state,
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
def test_closest_function_state_with_invalid_location(self):
|
||||
"""Test closest function state with invalid location."""
|
||||
|
@ -876,11 +825,10 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
'longitude': self.hass.config.longitude + 0.1,
|
||||
})
|
||||
|
||||
self.assertEqual(
|
||||
'None',
|
||||
assert 'None' == \
|
||||
template.Template(
|
||||
'{{ closest(states.test_domain.closest_home, '
|
||||
'states) }}', self.hass).render())
|
||||
'states) }}', self.hass).render()
|
||||
|
||||
def test_closest_function_invalid_coordinates(self):
|
||||
"""Test closest function invalid coordinates."""
|
||||
|
@ -889,148 +837,129 @@ class TestHelpersTemplate(unittest.TestCase):
|
|||
'longitude': self.hass.config.longitude + 0.1,
|
||||
})
|
||||
|
||||
self.assertEqual(
|
||||
'None',
|
||||
assert 'None' == \
|
||||
template.Template('{{ closest("invalid", "coord", states) }}',
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
def test_closest_function_no_location_states(self):
|
||||
"""Test closest function without location states."""
|
||||
self.assertEqual(
|
||||
'',
|
||||
assert '' == \
|
||||
template.Template('{{ closest(states).entity_id }}',
|
||||
self.hass).render())
|
||||
self.hass).render()
|
||||
|
||||
def test_extract_entities_none_exclude_stuff(self):
|
||||
"""Test extract entities function with none or exclude stuff."""
|
||||
self.assertEqual(MATCH_ALL, template.extract_entities(None))
|
||||
assert MATCH_ALL == template.extract_entities(None)
|
||||
|
||||
self.assertEqual(
|
||||
MATCH_ALL,
|
||||
assert MATCH_ALL == \
|
||||
template.extract_entities(
|
||||
'{{ closest(states.zone.far_away, '
|
||||
'states.test_domain).entity_id }}'))
|
||||
'states.test_domain).entity_id }}')
|
||||
|
||||
self.assertEqual(
|
||||
MATCH_ALL,
|
||||
assert MATCH_ALL == \
|
||||
template.extract_entities(
|
||||
'{{ distance("123", states.test_object_2) }}'))
|
||||
'{{ distance("123", states.test_object_2) }}')
|
||||
|
||||
def test_extract_entities_no_match_entities(self):
|
||||
"""Test extract entities function with none entities stuff."""
|
||||
self.assertEqual(
|
||||
MATCH_ALL,
|
||||
assert MATCH_ALL == \
|
||||
template.extract_entities(
|
||||
"{{ value_json.tst | timestamp_custom('%Y' True) }}"))
|
||||
"{{ value_json.tst | timestamp_custom('%Y' True) }}")
|
||||
|
||||
self.assertEqual(
|
||||
MATCH_ALL,
|
||||
assert MATCH_ALL == \
|
||||
template.extract_entities("""
|
||||
{% for state in states.sensor %}
|
||||
{{ state.entity_id }}={{ state.state }},d
|
||||
{% endfor %}
|
||||
"""))
|
||||
""")
|
||||
|
||||
def test_extract_entities_match_entities(self):
|
||||
"""Test extract entities function with entities stuff."""
|
||||
self.assertListEqual(
|
||||
['device_tracker.phone_1'],
|
||||
assert ['device_tracker.phone_1'] == \
|
||||
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 %}
|
||||
"""))
|
||||
""")
|
||||
|
||||
self.assertListEqual(
|
||||
['binary_sensor.garage_door'],
|
||||
assert ['binary_sensor.garage_door'] == \
|
||||
template.extract_entities("""
|
||||
{{ as_timestamp(states.binary_sensor.garage_door.last_changed) }}
|
||||
"""))
|
||||
""")
|
||||
|
||||
self.assertListEqual(
|
||||
['binary_sensor.garage_door'],
|
||||
assert ['binary_sensor.garage_door'] == \
|
||||
template.extract_entities("""
|
||||
{{ states("binary_sensor.garage_door") }}
|
||||
"""))
|
||||
""")
|
||||
|
||||
self.assertListEqual(
|
||||
['device_tracker.phone_2'],
|
||||
assert ['device_tracker.phone_2'] == \
|
||||
template.extract_entities("""
|
||||
is_state_attr('device_tracker.phone_2', 'battery', 40)
|
||||
"""))
|
||||
""")
|
||||
|
||||
self.assertListEqual(
|
||||
sorted([
|
||||
assert sorted([
|
||||
'device_tracker.phone_1',
|
||||
'device_tracker.phone_2',
|
||||
]),
|
||||
]) == \
|
||||
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 %}
|
||||
""")))
|
||||
"""))
|
||||
|
||||
self.assertListEqual(
|
||||
sorted([
|
||||
assert sorted([
|
||||
'sensor.pick_humidity',
|
||||
'sensor.pick_temperature',
|
||||
]),
|
||||
]) == \
|
||||
sorted(template.extract_entities("""
|
||||
{{
|
||||
states.sensor.pick_temperature.state ~ „°C (“ ~
|
||||
states.sensor.pick_humidity.state ~ „ %“
|
||||
}}
|
||||
""")))
|
||||
"""))
|
||||
|
||||
self.assertListEqual(
|
||||
sorted([
|
||||
assert sorted([
|
||||
'sensor.luftfeuchtigkeit_mean',
|
||||
'input_number.luftfeuchtigkeit',
|
||||
]),
|
||||
]) == \
|
||||
sorted(template.extract_entities(
|
||||
"{% if (states('sensor.luftfeuchtigkeit_mean') | int)"
|
||||
" > (states('input_number.luftfeuchtigkeit') | int +1.5)"
|
||||
" %}true{% endif %}"
|
||||
)))
|
||||
))
|
||||
|
||||
def test_extract_entities_with_variables(self):
|
||||
"""Test extract entities function with variables and entities stuff."""
|
||||
self.assertEqual(
|
||||
['input_boolean.switch'],
|
||||
assert ['input_boolean.switch'] == \
|
||||
template.extract_entities(
|
||||
"{{ is_state('input_boolean.switch', 'off') }}", {}))
|
||||
"{{ is_state('input_boolean.switch', 'off') }}", {})
|
||||
|
||||
self.assertEqual(
|
||||
['trigger.entity_id'],
|
||||
assert ['trigger.entity_id'] == \
|
||||
template.extract_entities(
|
||||
"{{ is_state(trigger.entity_id, 'off') }}", {}))
|
||||
"{{ is_state(trigger.entity_id, 'off') }}", {})
|
||||
|
||||
self.assertEqual(
|
||||
MATCH_ALL,
|
||||
assert MATCH_ALL == \
|
||||
template.extract_entities(
|
||||
"{{ is_state(data, 'off') }}", {}))
|
||||
"{{ is_state(data, 'off') }}", {})
|
||||
|
||||
self.assertEqual(
|
||||
['input_boolean.switch'],
|
||||
assert ['input_boolean.switch'] == \
|
||||
template.extract_entities(
|
||||
"{{ is_state(data, 'off') }}",
|
||||
{'data': 'input_boolean.switch'}))
|
||||
{'data': 'input_boolean.switch'})
|
||||
|
||||
self.assertEqual(
|
||||
['input_boolean.switch'],
|
||||
assert ['input_boolean.switch'] == \
|
||||
template.extract_entities(
|
||||
"{{ is_state(trigger.entity_id, 'off') }}",
|
||||
{'trigger': {'entity_id': 'input_boolean.switch'}}))
|
||||
{'trigger': {'entity_id': 'input_boolean.switch'}})
|
||||
|
||||
self.assertEqual(
|
||||
MATCH_ALL,
|
||||
assert MATCH_ALL == \
|
||||
template.extract_entities(
|
||||
"{{ is_state('media_player.' ~ where , 'playing') }}",
|
||||
{'where': 'livingroom'}))
|
||||
{'where': 'livingroom'})
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue