Optimize template 2 (#3521)
* Enforce compiling templates * Refactor templates * Add template validator to Logbook service * Some more fixes * Lint * Allow easy skipping of rfxtrx tests * Fix template bug in AND & OR conditions * add entities extractor Conflicts: tests/helpers/test_template.py * fix unittest * Convert template to be async * Fix Farcy * Lint fix * Limit template updates to related entities * Make template automation async
This commit is contained in:
parent
6694b0470e
commit
00e298206e
52 changed files with 841 additions and 562 deletions
|
@ -4,6 +4,7 @@ import unittest
|
|||
from homeassistant.const import (STATE_ON, STATE_OFF)
|
||||
from homeassistant.components.binary_sensor import command_line
|
||||
from homeassistant import bootstrap
|
||||
from homeassistant.helpers import template
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
|
@ -56,7 +57,7 @@ class TestCommandSensorBinarySensor(unittest.TestCase):
|
|||
|
||||
entity = command_line.CommandBinarySensor(
|
||||
self.hass, data, 'test', None, '1.0', '0',
|
||||
'{{ value | multiply(0.1) }}')
|
||||
template.Template('{{ value | multiply(0.1) }}', self.hass))
|
||||
|
||||
self.assertEqual(STATE_ON, entity.state)
|
||||
|
||||
|
|
|
@ -4,8 +4,10 @@ from unittest import mock
|
|||
|
||||
from homeassistant.const import EVENT_STATE_CHANGED, MATCH_ALL
|
||||
import homeassistant.bootstrap as bootstrap
|
||||
from homeassistant.components.binary_sensor import PLATFORM_SCHEMA
|
||||
from homeassistant.components.binary_sensor import template
|
||||
from homeassistant.exceptions import TemplateError
|
||||
from homeassistant.helpers import template as template_hlpr
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
|
@ -13,31 +15,39 @@ from tests.common import get_test_home_assistant
|
|||
class TestBinarySensorTemplate(unittest.TestCase):
|
||||
"""Test for Binary sensor template platform."""
|
||||
|
||||
def setup_method(self, method):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
def teardown_method(self, method):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
@mock.patch.object(template, 'BinarySensorTemplate')
|
||||
def test_setup(self, mock_template):
|
||||
""""Test the setup."""
|
||||
config = {
|
||||
tpl = template_hlpr.Template('{{ foo }}', self.hass)
|
||||
config = PLATFORM_SCHEMA({
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test': {
|
||||
'friendly_name': 'virtual thingy',
|
||||
'value_template': '{{ foo }}',
|
||||
'value_template': tpl,
|
||||
'sensor_class': 'motion',
|
||||
'entity_id': 'test'
|
||||
},
|
||||
}
|
||||
}
|
||||
hass = mock.MagicMock()
|
||||
})
|
||||
add_devices = mock.MagicMock()
|
||||
result = template.setup_platform(hass, config, add_devices)
|
||||
result = template.setup_platform(self.hass, config, add_devices)
|
||||
self.assertTrue(result)
|
||||
mock_template.assert_called_once_with(hass, 'test', 'virtual thingy',
|
||||
'motion', '{{ foo }}', 'test')
|
||||
mock_template.assert_called_once_with(
|
||||
self.hass, 'test', 'virtual thingy', 'motion', tpl, 'test')
|
||||
add_devices.assert_called_once_with([mock_template.return_value])
|
||||
|
||||
def test_setup_no_sensors(self):
|
||||
""""Test setup with no sensors."""
|
||||
hass = mock.MagicMock()
|
||||
result = bootstrap.setup_component(hass, 'sensor', {
|
||||
result = bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template'
|
||||
}
|
||||
|
@ -46,8 +56,7 @@ class TestBinarySensorTemplate(unittest.TestCase):
|
|||
|
||||
def test_setup_invalid_device(self):
|
||||
""""Test the setup with invalid devices."""
|
||||
hass = mock.MagicMock()
|
||||
result = bootstrap.setup_component(hass, 'sensor', {
|
||||
result = bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
|
@ -59,8 +68,7 @@ class TestBinarySensorTemplate(unittest.TestCase):
|
|||
|
||||
def test_setup_invalid_sensor_class(self):
|
||||
""""Test setup with invalid sensor class."""
|
||||
hass = mock.MagicMock()
|
||||
result = bootstrap.setup_component(hass, 'sensor', {
|
||||
result = bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
|
@ -75,8 +83,7 @@ class TestBinarySensorTemplate(unittest.TestCase):
|
|||
|
||||
def test_setup_invalid_missing_template(self):
|
||||
""""Test setup with invalid and missing template."""
|
||||
hass = mock.MagicMock()
|
||||
result = bootstrap.setup_component(hass, 'sensor', {
|
||||
result = bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
|
@ -90,9 +97,9 @@ class TestBinarySensorTemplate(unittest.TestCase):
|
|||
|
||||
def test_attributes(self):
|
||||
""""Test the attributes."""
|
||||
hass = mock.MagicMock()
|
||||
vs = template.BinarySensorTemplate(hass, 'parent', 'Parent',
|
||||
'motion', '{{ 1 > 1 }}', MATCH_ALL)
|
||||
vs = template.BinarySensorTemplate(
|
||||
self.hass, 'parent', 'Parent', 'motion',
|
||||
template_hlpr.Template('{{ 1 > 1 }}', self.hass), MATCH_ALL)
|
||||
self.assertFalse(vs.should_poll)
|
||||
self.assertEqual('motion', vs.sensor_class)
|
||||
self.assertEqual('Parent', vs.name)
|
||||
|
@ -100,32 +107,29 @@ class TestBinarySensorTemplate(unittest.TestCase):
|
|||
vs.update()
|
||||
self.assertFalse(vs.is_on)
|
||||
|
||||
vs._template = "{{ 2 > 1 }}"
|
||||
vs._template = template_hlpr.Template("{{ 2 > 1 }}", self.hass)
|
||||
vs.update()
|
||||
self.assertTrue(vs.is_on)
|
||||
|
||||
def test_event(self):
|
||||
""""Test the event."""
|
||||
hass = get_test_home_assistant()
|
||||
vs = template.BinarySensorTemplate(hass, 'parent', 'Parent',
|
||||
'motion', '{{ 1 > 1 }}', MATCH_ALL)
|
||||
vs = template.BinarySensorTemplate(
|
||||
self.hass, 'parent', 'Parent', 'motion',
|
||||
template_hlpr.Template('{{ 1 > 1 }}', self.hass), MATCH_ALL)
|
||||
vs.update_ha_state()
|
||||
hass.block_till_done()
|
||||
self.hass.block_till_done()
|
||||
|
||||
with mock.patch.object(vs, 'update') as mock_update:
|
||||
hass.bus.fire(EVENT_STATE_CHANGED)
|
||||
hass.block_till_done()
|
||||
try:
|
||||
assert mock_update.call_count == 1
|
||||
finally:
|
||||
hass.stop()
|
||||
self.hass.bus.fire(EVENT_STATE_CHANGED)
|
||||
self.hass.block_till_done()
|
||||
assert mock_update.call_count == 1
|
||||
|
||||
@mock.patch('homeassistant.helpers.template.render')
|
||||
@mock.patch('homeassistant.helpers.template.Template.render')
|
||||
def test_update_template_error(self, mock_render):
|
||||
""""Test the template update error."""
|
||||
hass = mock.MagicMock()
|
||||
vs = template.BinarySensorTemplate(hass, 'parent', 'Parent',
|
||||
'motion', '{{ 1 > 1 }}', MATCH_ALL)
|
||||
vs = template.BinarySensorTemplate(
|
||||
self.hass, 'parent', 'Parent', 'motion',
|
||||
template_hlpr.Template('{{ 1 > 1 }}', self.hass), MATCH_ALL)
|
||||
mock_render.side_effect = TemplateError('foo')
|
||||
vs.update()
|
||||
mock_render.side_effect = TemplateError(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue