Fix PEP257 issues
This commit is contained in:
parent
d6eab03a61
commit
9838697d2b
120 changed files with 1447 additions and 1297 deletions
|
@ -1,6 +1,4 @@
|
|||
"""
|
||||
Tests Home Assistant template helper methods.
|
||||
"""
|
||||
"""Test Home Assistant template helper methods."""
|
||||
# pylint: disable=too-many-public-methods
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
@ -14,8 +12,10 @@ from tests.common import get_test_home_assistant
|
|||
|
||||
|
||||
class TestUtilTemplate(unittest.TestCase):
|
||||
"""Test the Template."""
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
"""Setup the tests."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
|
@ -23,12 +23,14 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
self.hass.stop()
|
||||
|
||||
def test_referring_states_by_entity_id(self):
|
||||
"""."""
|
||||
self.hass.states.set('test.object', 'happy')
|
||||
self.assertEqual(
|
||||
'happy',
|
||||
template.render(self.hass, '{{ states.test.object.state }}'))
|
||||
|
||||
def test_iterating_all_states(self):
|
||||
"""."""
|
||||
self.hass.states.set('test.object', 'happy')
|
||||
self.hass.states.set('sensor.temperature', 10)
|
||||
|
||||
|
@ -39,6 +41,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
'{% for state in states %}{{ state.state }}{% endfor %}'))
|
||||
|
||||
def test_iterating_domain_states(self):
|
||||
"""."""
|
||||
self.hass.states.set('test.object', 'happy')
|
||||
self.hass.states.set('sensor.back_door', 'open')
|
||||
self.hass.states.set('sensor.temperature', 10)
|
||||
|
@ -52,6 +55,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
"""))
|
||||
|
||||
def test_float(self):
|
||||
"""."""
|
||||
self.hass.states.set('sensor.temperature', '12')
|
||||
|
||||
self.assertEqual(
|
||||
|
@ -67,6 +71,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
'{{ float(states.sensor.temperature.state) > 11 }}'))
|
||||
|
||||
def test_rounding_value(self):
|
||||
"""."""
|
||||
self.hass.states.set('sensor.temperature', 12.78)
|
||||
|
||||
self.assertEqual(
|
||||
|
@ -83,6 +88,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
))
|
||||
|
||||
def test_rounding_value_get_original_value_on_error(self):
|
||||
"""."""
|
||||
self.assertEqual(
|
||||
'None',
|
||||
template.render(
|
||||
|
@ -98,6 +104,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
))
|
||||
|
||||
def test_multiply(self):
|
||||
"""."""
|
||||
tests = {
|
||||
None: 'None',
|
||||
10: '100',
|
||||
|
@ -111,42 +118,50 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
'{{ %s | multiply(10) | round }}' % inp))
|
||||
|
||||
def test_passing_vars_as_keywords(self):
|
||||
"""."""
|
||||
self.assertEqual(
|
||||
'127', template.render(self.hass, '{{ hello }}', hello=127))
|
||||
|
||||
def test_passing_vars_as_vars(self):
|
||||
"""."""
|
||||
self.assertEqual(
|
||||
'127', template.render(self.hass, '{{ hello }}', {'hello': 127}))
|
||||
|
||||
def test_render_with_possible_json_value_with_valid_json(self):
|
||||
"""."""
|
||||
self.assertEqual(
|
||||
'world',
|
||||
template.render_with_possible_json_value(
|
||||
self.hass, '{{ value_json.hello }}', '{"hello": "world"}'))
|
||||
|
||||
def test_render_with_possible_json_value_with_invalid_json(self):
|
||||
"""."""
|
||||
self.assertEqual(
|
||||
'',
|
||||
template.render_with_possible_json_value(
|
||||
self.hass, '{{ value_json }}', '{ I AM NOT JSON }'))
|
||||
|
||||
def test_render_with_possible_json_value_with_template_error(self):
|
||||
"""."""
|
||||
self.assertEqual(
|
||||
'hello',
|
||||
template.render_with_possible_json_value(
|
||||
self.hass, '{{ value_json', 'hello'))
|
||||
|
||||
def test_render_with_possible_json_value_with_template_error_value(self):
|
||||
"""."""
|
||||
self.assertEqual(
|
||||
'-',
|
||||
template.render_with_possible_json_value(
|
||||
self.hass, '{{ value_json', 'hello', '-'))
|
||||
|
||||
def test_raise_exception_on_error(self):
|
||||
"""."""
|
||||
with self.assertRaises(TemplateError):
|
||||
template.render(self.hass, '{{ invalid_syntax')
|
||||
|
||||
def test_if_state_exists(self):
|
||||
"""."""
|
||||
self.hass.states.set('test.object', 'available')
|
||||
self.assertEqual(
|
||||
'exists',
|
||||
|
@ -157,6 +172,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
"""))
|
||||
|
||||
def test_is_state(self):
|
||||
"""."""
|
||||
self.hass.states.set('test.object', 'available')
|
||||
self.assertEqual(
|
||||
'yes',
|
||||
|
@ -167,6 +183,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
"""))
|
||||
|
||||
def test_is_state_attr(self):
|
||||
"""."""
|
||||
self.hass.states.set('test.object', 'available', {'mode': 'on'})
|
||||
self.assertEqual(
|
||||
'yes',
|
||||
|
@ -177,6 +194,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
"""))
|
||||
|
||||
def test_states_function(self):
|
||||
"""."""
|
||||
self.hass.states.set('test.object', 'available')
|
||||
self.assertEqual(
|
||||
'available',
|
||||
|
@ -189,6 +207,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
@patch('homeassistant.helpers.template.TemplateEnvironment.'
|
||||
'is_safe_callable', return_value=True)
|
||||
def test_now(self, mock_is_safe, mock_utcnow):
|
||||
"""."""
|
||||
self.assertEqual(
|
||||
dt_util.utcnow().isoformat(),
|
||||
template.render(self.hass, '{{ now.isoformat() }}'))
|
||||
|
@ -197,16 +216,19 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
@patch('homeassistant.helpers.template.TemplateEnvironment.'
|
||||
'is_safe_callable', return_value=True)
|
||||
def test_utcnow(self, mock_is_safe, mock_utcnow):
|
||||
"""."""
|
||||
self.assertEqual(
|
||||
dt_util.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', {
|
||||
'latitude': 32.87336,
|
||||
'longitude': -117.22943,
|
||||
|
@ -218,6 +240,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
self.hass, '{{ distance(states.test.object) | round }}'))
|
||||
|
||||
def test_distance_function_with_2_states(self):
|
||||
"""."""
|
||||
self.hass.states.set('test.object', 'happy', {
|
||||
'latitude': 32.87336,
|
||||
'longitude': -117.22943,
|
||||
|
@ -236,12 +259,14 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
'| round }}'))
|
||||
|
||||
def test_distance_function_with_1_coord(self):
|
||||
"""."""
|
||||
self.assertEqual(
|
||||
'187',
|
||||
template.render(
|
||||
self.hass, '{{ distance("32.87336", "-117.22943") | round }}'))
|
||||
|
||||
def test_distance_function_with_2_coords(self):
|
||||
"""."""
|
||||
self.assertEqual(
|
||||
'187',
|
||||
template.render(
|
||||
|
@ -250,6 +275,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
% (self.hass.config.latitude, self.hass.config.longitude)))
|
||||
|
||||
def test_distance_function_with_1_state_1_coord(self):
|
||||
"""."""
|
||||
self.hass.states.set('test.object_2', 'happy', {
|
||||
'latitude': self.hass.config.latitude,
|
||||
'longitude': self.hass.config.longitude,
|
||||
|
@ -270,6 +296,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
'| round }}'))
|
||||
|
||||
def test_distance_function_return_None_if_invalid_state(self):
|
||||
"""."""
|
||||
self.hass.states.set('test.object_2', 'happy', {
|
||||
'latitude': 10,
|
||||
})
|
||||
|
@ -281,6 +308,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
'{{ distance(states.test.object_2) | round }}'))
|
||||
|
||||
def test_distance_function_return_None_if_invalid_coord(self):
|
||||
"""."""
|
||||
self.assertEqual(
|
||||
'None',
|
||||
template.render(
|
||||
|
@ -305,6 +333,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
'{{ distance("123", states.test_object_2) }}'))
|
||||
|
||||
def test_closest_function_home_vs_domain(self):
|
||||
"""."""
|
||||
self.hass.states.set('test_domain.object', 'happy', {
|
||||
'latitude': self.hass.config.latitude + 0.1,
|
||||
'longitude': self.hass.config.longitude + 0.1,
|
||||
|
@ -321,6 +350,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
'{{ closest(states.test_domain).entity_id }}'))
|
||||
|
||||
def test_closest_function_home_vs_all_states(self):
|
||||
"""."""
|
||||
self.hass.states.set('test_domain.object', 'happy', {
|
||||
'latitude': self.hass.config.latitude + 0.1,
|
||||
'longitude': self.hass.config.longitude + 0.1,
|
||||
|
@ -337,6 +367,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
'{{ closest(states).entity_id }}'))
|
||||
|
||||
def test_closest_function_home_vs_group_entity_id(self):
|
||||
"""."""
|
||||
self.hass.states.set('test_domain.object', 'happy', {
|
||||
'latitude': self.hass.config.latitude + 0.1,
|
||||
'longitude': self.hass.config.longitude + 0.1,
|
||||
|
@ -355,6 +386,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
'{{ closest("group.location_group").entity_id }}'))
|
||||
|
||||
def test_closest_function_home_vs_group_state(self):
|
||||
"""."""
|
||||
self.hass.states.set('test_domain.object', 'happy', {
|
||||
'latitude': self.hass.config.latitude + 0.1,
|
||||
'longitude': self.hass.config.longitude + 0.1,
|
||||
|
@ -374,6 +406,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
'{{ closest(states.group.location_group).entity_id }}'))
|
||||
|
||||
def test_closest_function_to_coord(self):
|
||||
"""."""
|
||||
self.hass.states.set('test_domain.closest_home', 'happy', {
|
||||
'latitude': self.hass.config.latitude + 0.1,
|
||||
'longitude': self.hass.config.longitude + 0.1,
|
||||
|
@ -399,6 +432,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
)
|
||||
|
||||
def test_closest_function_to_entity_id(self):
|
||||
"""."""
|
||||
self.hass.states.set('test_domain.closest_home', 'happy', {
|
||||
'latitude': self.hass.config.latitude + 0.1,
|
||||
'longitude': self.hass.config.longitude + 0.1,
|
||||
|
@ -422,6 +456,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
)
|
||||
|
||||
def test_closest_function_to_state(self):
|
||||
"""."""
|
||||
self.hass.states.set('test_domain.closest_home', 'happy', {
|
||||
'latitude': self.hass.config.latitude + 0.1,
|
||||
'longitude': self.hass.config.longitude + 0.1,
|
||||
|
@ -446,6 +481,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
)
|
||||
|
||||
def test_closest_function_invalid_state(self):
|
||||
"""."""
|
||||
self.hass.states.set('test_domain.closest_home', 'happy', {
|
||||
'latitude': self.hass.config.latitude + 0.1,
|
||||
'longitude': self.hass.config.longitude + 0.1,
|
||||
|
@ -458,6 +494,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
self.hass, '{{ closest(%s, states) }}' % state))
|
||||
|
||||
def test_closest_function_state_with_invalid_location(self):
|
||||
"""."""
|
||||
self.hass.states.set('test_domain.closest_home', 'happy', {
|
||||
'latitude': 'invalid latitude',
|
||||
'longitude': self.hass.config.longitude + 0.1,
|
||||
|
@ -471,6 +508,7 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
'states) }}'))
|
||||
|
||||
def test_closest_function_invalid_coordinates(self):
|
||||
"""."""
|
||||
self.hass.states.set('test_domain.closest_home', 'happy', {
|
||||
'latitude': self.hass.config.latitude + 0.1,
|
||||
'longitude': self.hass.config.longitude + 0.1,
|
||||
|
@ -482,5 +520,6 @@ class TestUtilTemplate(unittest.TestCase):
|
|||
'{{ closest("invalid", "coord", states) }}'))
|
||||
|
||||
def test_closest_function_no_location_states(self):
|
||||
"""."""
|
||||
self.assertEqual('None',
|
||||
template.render(self.hass, '{{ closest(states) }}'))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue