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
|
@ -5,7 +5,7 @@ import unittest
|
|||
from unittest.mock import patch
|
||||
from subprocess import SubprocessError
|
||||
|
||||
from homeassistant.bootstrap import _setup_component
|
||||
from homeassistant.bootstrap import setup_component
|
||||
from homeassistant.components import shell_command
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
|
@ -26,7 +26,7 @@ class TestShellCommand(unittest.TestCase):
|
|||
"""Test if able to call a configured service."""
|
||||
with tempfile.TemporaryDirectory() as tempdirname:
|
||||
path = os.path.join(tempdirname, 'called.txt')
|
||||
assert _setup_component(self.hass, shell_command.DOMAIN, {
|
||||
assert setup_component(self.hass, shell_command.DOMAIN, {
|
||||
shell_command.DOMAIN: {
|
||||
'test_service': "date > {}".format(path)
|
||||
}
|
||||
|
@ -40,41 +40,54 @@ class TestShellCommand(unittest.TestCase):
|
|||
|
||||
def test_config_not_dict(self):
|
||||
"""Test if config is not a dict."""
|
||||
assert not _setup_component(self.hass, shell_command.DOMAIN, {
|
||||
assert not setup_component(self.hass, shell_command.DOMAIN, {
|
||||
shell_command.DOMAIN: ['some', 'weird', 'list']
|
||||
})
|
||||
|
||||
def test_config_not_valid_service_names(self):
|
||||
"""Test if config contains invalid service names."""
|
||||
assert not _setup_component(self.hass, shell_command.DOMAIN, {
|
||||
assert not setup_component(self.hass, shell_command.DOMAIN, {
|
||||
shell_command.DOMAIN: {
|
||||
'this is invalid because space': 'touch bla.txt'
|
||||
}
|
||||
})
|
||||
|
||||
def test_template_render_no_template(self):
|
||||
@patch('homeassistant.components.shell_command.subprocess.call')
|
||||
def test_template_render_no_template(self, mock_call):
|
||||
"""Ensure shell_commands without templates get rendered properly."""
|
||||
cmd, shell = shell_command._parse_command(self.hass, 'ls /bin', {})
|
||||
self.assertTrue(shell)
|
||||
self.assertEqual(cmd, 'ls /bin')
|
||||
assert setup_component(self.hass, shell_command.DOMAIN, {
|
||||
shell_command.DOMAIN: {
|
||||
'test_service': "ls /bin"
|
||||
}
|
||||
})
|
||||
|
||||
def test_template_render(self):
|
||||
"""Ensure shell_commands with templates get rendered properly."""
|
||||
self.hass.services.call('shell_command', 'test_service',
|
||||
blocking=True)
|
||||
|
||||
cmd = mock_call.mock_calls[0][1][0]
|
||||
shell = mock_call.mock_calls[0][2]['shell']
|
||||
|
||||
assert 'ls /bin' == cmd
|
||||
assert shell
|
||||
|
||||
@patch('homeassistant.components.shell_command.subprocess.call')
|
||||
def test_template_render(self, mock_call):
|
||||
"""Ensure shell_commands without templates get rendered properly."""
|
||||
self.hass.states.set('sensor.test_state', 'Works')
|
||||
cmd, shell = shell_command._parse_command(
|
||||
self.hass,
|
||||
'ls /bin {{ states.sensor.test_state.state }}', {}
|
||||
)
|
||||
self.assertFalse(shell, False)
|
||||
self.assertEqual(cmd[-1], 'Works')
|
||||
assert setup_component(self.hass, shell_command.DOMAIN, {
|
||||
shell_command.DOMAIN: {
|
||||
'test_service': "ls /bin {{ states.sensor.test_state.state }}"
|
||||
}
|
||||
})
|
||||
|
||||
def test_invalid_template_fails(self):
|
||||
"""Test that shell_commands with invalid templates fail."""
|
||||
cmd, _shell = shell_command._parse_command(
|
||||
self.hass,
|
||||
'ls /bin {{ states. .test_state.state }}', {}
|
||||
)
|
||||
self.assertEqual(cmd, None)
|
||||
self.hass.services.call('shell_command', 'test_service',
|
||||
blocking=True)
|
||||
|
||||
cmd = mock_call.mock_calls[0][1][0]
|
||||
shell = mock_call.mock_calls[0][2]['shell']
|
||||
|
||||
assert ['ls', '/bin', 'Works'] == cmd
|
||||
assert not shell
|
||||
|
||||
@patch('homeassistant.components.shell_command.subprocess.call',
|
||||
side_effect=SubprocessError)
|
||||
|
@ -83,7 +96,7 @@ class TestShellCommand(unittest.TestCase):
|
|||
"""Test subprocess."""
|
||||
with tempfile.TemporaryDirectory() as tempdirname:
|
||||
path = os.path.join(tempdirname, 'called.txt')
|
||||
assert _setup_component(self.hass, shell_command.DOMAIN, {
|
||||
assert setup_component(self.hass, shell_command.DOMAIN, {
|
||||
shell_command.DOMAIN: {
|
||||
'test_service': "touch {}".format(path)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue