Allow reloading automation without restarting HA (#3002)
This commit is contained in:
1 parent
641d531be3
commit
e9813b219e
7 files changed
+365
-107
No files matched your search
@@ -5,6 +5,7 @@ from unittest.mock import patch
|
||||
from homeassistant.bootstrap import _setup_component
|
||||
import homeassistant.components.automation as automation
|
||||
from homeassistant.const import ATTR_ENTITY_ID
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
@@ -414,3 +415,132 @@ class TestAutomation(unittest.TestCase):
|
||||
automation.turn_on(self.hass, entity_id)
|
||||
self.hass.pool.block_till_done()
|
||||
assert automation.is_on(self.hass, entity_id)
|
||||
|
||||
@patch('homeassistant.config.load_yaml_config_file', return_value={
|
||||
automation.DOMAIN: {
|
||||
'alias': 'bye',
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event2',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
'data_template': {
|
||||
'event': '{{ trigger.event.event_type }}'
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
def test_reload_config_service(self, mock_load_yaml):
|
||||
"""Test the reload config service."""
|
||||
assert _setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'alias': 'hello',
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
'data_template': {
|
||||
'event': '{{ trigger.event.event_type }}'
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
assert self.hass.states.get('automation.hello') is not None
|
||||
assert self.hass.states.get('automation.bye') is None
|
||||
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
assert len(self.calls) == 1
|
||||
assert self.calls[0].data.get('event') == 'test_event'
|
||||
|
||||
automation.reload(self.hass)
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
assert self.hass.states.get('automation.hello') is None
|
||||
assert self.hass.states.get('automation.bye') is not None
|
||||
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.pool.block_till_done()
|
||||
assert len(self.calls) == 1
|
||||
|
||||
self.hass.bus.fire('test_event2')
|
||||
self.hass.pool.block_till_done()
|
||||
assert len(self.calls) == 2
|
||||
assert self.calls[1].data.get('event') == 'test_event2'
|
||||
|
||||
@patch('homeassistant.config.load_yaml_config_file', return_value={
|
||||
automation.DOMAIN: 'not valid',
|
||||
})
|
||||
def test_reload_config_when_invalid_config(self, mock_load_yaml):
|
||||
"""Test the reload config service handling invalid config."""
|
||||
assert _setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'alias': 'hello',
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
'data_template': {
|
||||
'event': '{{ trigger.event.event_type }}'
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
assert self.hass.states.get('automation.hello') is not None
|
||||
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
assert len(self.calls) == 1
|
||||
assert self.calls[0].data.get('event') == 'test_event'
|
||||
|
||||
automation.reload(self.hass)
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
assert self.hass.states.get('automation.hello') is not None
|
||||
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.pool.block_till_done()
|
||||
assert len(self.calls) == 2
|
||||
|
||||
def test_reload_config_handles_load_fails(self):
|
||||
"""Test the reload config service."""
|
||||
assert _setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'alias': 'hello',
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
'data_template': {
|
||||
'event': '{{ trigger.event.event_type }}'
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
assert self.hass.states.get('automation.hello') is not None
|
||||
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
assert len(self.calls) == 1
|
||||
assert self.calls[0].data.get('event') == 'test_event'
|
||||
|
||||
with patch('homeassistant.config.load_yaml_config_file',
|
||||
side_effect=HomeAssistantError('bla')):
|
||||
automation.reload(self.hass)
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
assert self.hass.states.get('automation.hello') is not None
|
||||
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.pool.block_till_done()
|
||||
assert len(self.calls) == 2
|
||||
@@ -211,19 +211,19 @@ class TestBootstrap:
|
||||
deps = ['non_existing']
|
||||
loader.set_component('comp', MockModule('comp', dependencies=deps))
|
||||
|
||||
assert not bootstrap._setup_component(self.hass, 'comp', None)
|
||||
assert not bootstrap._setup_component(self.hass, 'comp', {})
|
||||
assert 'comp' not in self.hass.config.components
|
||||
|
||||
self.hass.config.components.append('non_existing')
|
||||
|
||||
assert bootstrap._setup_component(self.hass, 'comp', None)
|
||||
assert bootstrap._setup_component(self.hass, 'comp', {})
|
||||
|
||||
def test_component_failing_setup(self):
|
||||
"""Test component that fails setup."""
|
||||
loader.set_component(
|
||||
'comp', MockModule('comp', setup=lambda hass, config: False))
|
||||
|
||||
assert not bootstrap._setup_component(self.hass, 'comp', None)
|
||||
assert not bootstrap._setup_component(self.hass, 'comp', {})
|
||||
assert 'comp' not in self.hass.config.components
|
||||
|
||||
def test_component_exception_setup(self):
|
||||
@@ -234,7 +234,7 @@ class TestBootstrap:
|
||||
|
||||
loader.set_component('comp', MockModule('comp', setup=exception_setup))
|
||||
|
||||
assert not bootstrap._setup_component(self.hass, 'comp', None)
|
||||
assert not bootstrap._setup_component(self.hass, 'comp', {})
|
||||
assert 'comp' not in self.hass.config.components
|
||||
|
||||
def test_home_assistant_core_config_validation(self):
|
||||
|
||||
Reference in new issue
Block a user