2016-03-09 09:25:50 +00:00
|
|
|
"""Test the bootstrapping."""
|
2016-10-30 21:18:53 +00:00
|
|
|
# pylint: disable=protected-access
|
2016-03-30 05:50:38 +00:00
|
|
|
from unittest import mock
|
|
|
|
import threading
|
2016-10-08 18:27:35 +00:00
|
|
|
import logging
|
2015-08-11 15:20:13 +00:00
|
|
|
|
2016-03-29 07:17:53 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-02-20 07:20:14 +00:00
|
|
|
from homeassistant import bootstrap, loader
|
2015-08-11 15:20:13 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2016-03-29 07:17:53 +00:00
|
|
|
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA
|
2015-08-11 15:20:13 +00:00
|
|
|
|
2016-10-08 18:27:35 +00:00
|
|
|
from tests.common import \
|
2016-10-18 03:16:36 +00:00
|
|
|
get_test_home_assistant, MockModule, MockPlatform, \
|
|
|
|
assert_setup_component, patch_yaml_files
|
2016-02-14 23:08:23 +00:00
|
|
|
|
2016-03-30 05:50:38 +00:00
|
|
|
ORIG_TIMEZONE = dt_util.DEFAULT_TIME_ZONE
|
2015-08-11 15:20:13 +00:00
|
|
|
|
2016-10-08 18:27:35 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-03-30 05:50:38 +00:00
|
|
|
|
|
|
|
class TestBootstrap:
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the bootstrap utils."""
|
2015-08-11 15:20:13 +00:00
|
|
|
|
2016-10-08 18:27:35 +00:00
|
|
|
hass = None
|
|
|
|
backup_cache = None
|
|
|
|
|
|
|
|
# pylint: disable=invalid-name, no-self-use
|
2016-03-30 05:50:38 +00:00
|
|
|
def setup_method(self, method):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup the test."""
|
2016-06-27 16:02:45 +00:00
|
|
|
self.backup_cache = loader._COMPONENT_CACHE
|
|
|
|
|
2016-03-30 05:50:38 +00:00
|
|
|
if method == self.test_from_config_file:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.hass = get_test_home_assistant()
|
2015-08-11 15:20:13 +00:00
|
|
|
|
2016-03-30 05:50:38 +00:00
|
|
|
def teardown_method(self, method):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Clean up."""
|
2016-09-19 03:35:58 +00:00
|
|
|
if method == self.test_from_config_file:
|
|
|
|
return
|
|
|
|
|
2016-03-30 05:50:38 +00:00
|
|
|
dt_util.DEFAULT_TIME_ZONE = ORIG_TIMEZONE
|
|
|
|
self.hass.stop()
|
|
|
|
loader._COMPONENT_CACHE = self.backup_cache
|
2015-08-11 15:20:13 +00:00
|
|
|
|
2016-10-18 03:16:36 +00:00
|
|
|
@mock.patch(
|
|
|
|
# prevent .HA_VERISON file from being written
|
|
|
|
'homeassistant.bootstrap.conf_util.process_ha_config_upgrade',
|
2016-10-27 07:16:23 +00:00
|
|
|
autospec=True)
|
2016-06-27 16:02:45 +00:00
|
|
|
@mock.patch('homeassistant.util.location.detect_location_info',
|
2016-10-27 07:16:23 +00:00
|
|
|
autospec=True, return_value=None)
|
|
|
|
def test_from_config_file(self, mock_upgrade, mock_detect):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test with configuration file."""
|
2015-08-11 15:20:13 +00:00
|
|
|
components = ['browser', 'conversation', 'script']
|
2016-10-18 03:16:36 +00:00
|
|
|
files = {
|
|
|
|
'config.yaml': ''.join(
|
|
|
|
'{}:\n'.format(comp)
|
|
|
|
for comp in components
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
with mock.patch('os.path.isfile', mock.Mock(return_value=True)), \
|
|
|
|
mock.patch('os.access', mock.Mock(return_value=True)), \
|
|
|
|
patch_yaml_files(files, True):
|
|
|
|
self.hass = bootstrap.from_config_file('config.yaml')
|
2016-01-31 02:27:00 +00:00
|
|
|
|
2016-06-27 16:02:45 +00:00
|
|
|
components.append('group')
|
|
|
|
assert sorted(components) == sorted(self.hass.config.components)
|
2016-02-20 07:20:14 +00:00
|
|
|
|
|
|
|
def test_handle_setup_circular_dependency(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the setup of circular dependencies."""
|
2016-02-20 07:20:14 +00:00
|
|
|
loader.set_component('comp_b', MockModule('comp_b', ['comp_a']))
|
|
|
|
|
|
|
|
def setup_a(hass, config):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup the another component."""
|
2016-02-20 07:20:14 +00:00
|
|
|
bootstrap.setup_component(hass, 'comp_b')
|
|
|
|
return True
|
|
|
|
|
|
|
|
loader.set_component('comp_a', MockModule('comp_a', setup=setup_a))
|
|
|
|
|
2016-03-30 05:50:38 +00:00
|
|
|
bootstrap.setup_component(self.hass, 'comp_a')
|
|
|
|
assert ['comp_a'] == self.hass.config.components
|
2016-03-29 07:17:53 +00:00
|
|
|
|
|
|
|
def test_validate_component_config(self):
|
|
|
|
"""Test validating component configuration."""
|
|
|
|
config_schema = vol.Schema({
|
|
|
|
'comp_conf': {
|
|
|
|
'hello': str
|
|
|
|
}
|
|
|
|
}, required=True)
|
|
|
|
loader.set_component(
|
|
|
|
'comp_conf', MockModule('comp_conf', config_schema=config_schema))
|
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
with assert_setup_component(0):
|
|
|
|
assert not bootstrap.setup_component(self.hass, 'comp_conf', {})
|
2016-03-29 07:17:53 +00:00
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
with assert_setup_component(0):
|
|
|
|
assert not bootstrap.setup_component(self.hass, 'comp_conf', {
|
|
|
|
'comp_conf': None
|
|
|
|
})
|
2016-03-29 07:17:53 +00:00
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
with assert_setup_component(0):
|
|
|
|
assert not bootstrap.setup_component(self.hass, 'comp_conf', {
|
|
|
|
'comp_conf': {}
|
|
|
|
})
|
2016-03-29 07:17:53 +00:00
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
with assert_setup_component(0):
|
|
|
|
assert not bootstrap.setup_component(self.hass, 'comp_conf', {
|
|
|
|
'comp_conf': {
|
|
|
|
'hello': 'world',
|
|
|
|
'invalid': 'extra',
|
|
|
|
}
|
|
|
|
})
|
2016-03-29 07:17:53 +00:00
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
with assert_setup_component(1):
|
|
|
|
assert bootstrap.setup_component(self.hass, 'comp_conf', {
|
|
|
|
'comp_conf': {
|
|
|
|
'hello': 'world',
|
|
|
|
}
|
|
|
|
})
|
2016-03-29 07:17:53 +00:00
|
|
|
|
|
|
|
def test_validate_platform_config(self):
|
|
|
|
"""Test validating platform configuration."""
|
|
|
|
platform_schema = PLATFORM_SCHEMA.extend({
|
|
|
|
'hello': str,
|
2016-04-03 03:10:57 +00:00
|
|
|
})
|
2016-03-29 07:17:53 +00:00
|
|
|
loader.set_component(
|
|
|
|
'platform_conf',
|
|
|
|
MockModule('platform_conf', platform_schema=platform_schema))
|
|
|
|
|
2016-04-03 03:10:57 +00:00
|
|
|
loader.set_component(
|
|
|
|
'platform_conf.whatever', MockPlatform('whatever'))
|
|
|
|
|
2016-10-08 18:27:35 +00:00
|
|
|
with assert_setup_component(0):
|
2016-10-27 07:16:23 +00:00
|
|
|
assert bootstrap.setup_component(self.hass, 'platform_conf', {
|
2016-10-08 18:27:35 +00:00
|
|
|
'platform_conf': {
|
|
|
|
'hello': 'world',
|
|
|
|
'invalid': 'extra',
|
|
|
|
}
|
|
|
|
})
|
2016-03-29 07:17:53 +00:00
|
|
|
|
2016-10-08 18:27:35 +00:00
|
|
|
self.hass.config.components.remove('platform_conf')
|
2016-03-29 07:17:53 +00:00
|
|
|
|
2016-10-08 18:27:35 +00:00
|
|
|
with assert_setup_component(1):
|
2016-10-27 07:16:23 +00:00
|
|
|
assert bootstrap.setup_component(self.hass, 'platform_conf', {
|
2016-10-08 18:27:35 +00:00
|
|
|
'platform_conf': {
|
|
|
|
'platform': 'whatever',
|
|
|
|
'hello': 'world',
|
|
|
|
},
|
|
|
|
'platform_conf 2': {
|
|
|
|
'invalid': True
|
|
|
|
}
|
|
|
|
})
|
2016-03-29 07:17:53 +00:00
|
|
|
|
2016-10-08 18:27:35 +00:00
|
|
|
self.hass.config.components.remove('platform_conf')
|
2016-04-03 03:10:57 +00:00
|
|
|
|
2016-10-08 18:27:35 +00:00
|
|
|
with assert_setup_component(0):
|
2016-10-27 07:16:23 +00:00
|
|
|
assert bootstrap.setup_component(self.hass, 'platform_conf', {
|
2016-10-08 18:27:35 +00:00
|
|
|
'platform_conf': {
|
|
|
|
'platform': 'not_existing',
|
|
|
|
'hello': 'world',
|
|
|
|
}
|
|
|
|
})
|
2016-03-29 07:17:53 +00:00
|
|
|
|
2016-09-24 07:03:44 +00:00
|
|
|
self.hass.config.components.remove('platform_conf')
|
|
|
|
|
2016-10-08 18:27:35 +00:00
|
|
|
with assert_setup_component(1):
|
2016-10-27 07:16:23 +00:00
|
|
|
assert bootstrap.setup_component(self.hass, 'platform_conf', {
|
2016-10-08 18:27:35 +00:00
|
|
|
'platform_conf': {
|
|
|
|
'platform': 'whatever',
|
|
|
|
'hello': 'world',
|
|
|
|
}
|
|
|
|
})
|
2016-03-29 07:17:53 +00:00
|
|
|
|
2016-09-24 07:03:44 +00:00
|
|
|
self.hass.config.components.remove('platform_conf')
|
|
|
|
|
2016-10-08 18:27:35 +00:00
|
|
|
with assert_setup_component(1):
|
2016-10-27 07:16:23 +00:00
|
|
|
assert bootstrap.setup_component(self.hass, 'platform_conf', {
|
2016-10-08 18:27:35 +00:00
|
|
|
'platform_conf': [{
|
|
|
|
'platform': 'whatever',
|
|
|
|
'hello': 'world',
|
|
|
|
}]
|
|
|
|
})
|
2016-09-24 07:03:44 +00:00
|
|
|
|
|
|
|
self.hass.config.components.remove('platform_conf')
|
|
|
|
|
2016-10-08 18:27:35 +00:00
|
|
|
# Any falsey platform config will be ignored (None, {}, etc)
|
|
|
|
with assert_setup_component(0) as config:
|
2016-10-27 07:16:23 +00:00
|
|
|
assert bootstrap.setup_component(self.hass, 'platform_conf', {
|
2016-10-08 18:27:35 +00:00
|
|
|
'platform_conf': None
|
|
|
|
})
|
|
|
|
assert 'platform_conf' in self.hass.config.components
|
|
|
|
assert not config['platform_conf'] # empty
|
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
assert bootstrap.setup_component(self.hass, 'platform_conf', {
|
2016-10-08 18:27:35 +00:00
|
|
|
'platform_conf': {}
|
|
|
|
})
|
|
|
|
assert 'platform_conf' in self.hass.config.components
|
|
|
|
assert not config['platform_conf'] # empty
|
2016-09-24 07:03:44 +00:00
|
|
|
|
2016-03-30 05:50:38 +00:00
|
|
|
def test_component_not_found(self):
|
|
|
|
"""setup_component should not crash if component doesn't exist."""
|
|
|
|
assert not bootstrap.setup_component(self.hass, 'non_existing')
|
|
|
|
|
|
|
|
def test_component_not_double_initialized(self):
|
|
|
|
"""Test we do not setup a component twice."""
|
2016-07-26 05:49:10 +00:00
|
|
|
mock_setup = mock.MagicMock(return_value=True)
|
2016-03-30 05:50:38 +00:00
|
|
|
|
|
|
|
loader.set_component('comp', MockModule('comp', setup=mock_setup))
|
|
|
|
|
|
|
|
assert bootstrap.setup_component(self.hass, 'comp')
|
|
|
|
assert mock_setup.called
|
|
|
|
|
|
|
|
mock_setup.reset_mock()
|
|
|
|
|
|
|
|
assert bootstrap.setup_component(self.hass, 'comp')
|
|
|
|
assert not mock_setup.called
|
|
|
|
|
|
|
|
@mock.patch('homeassistant.util.package.install_package',
|
|
|
|
return_value=False)
|
|
|
|
def test_component_not_installed_if_requirement_fails(self, mock_install):
|
|
|
|
"""Component setup should fail if requirement can't install."""
|
2016-08-26 06:23:14 +00:00
|
|
|
self.hass.config.skip_pip = False
|
2016-03-30 05:50:38 +00:00
|
|
|
loader.set_component(
|
|
|
|
'comp', MockModule('comp', requirements=['package==0.0.1']))
|
|
|
|
|
|
|
|
assert not bootstrap.setup_component(self.hass, 'comp')
|
|
|
|
assert 'comp' not in self.hass.config.components
|
|
|
|
|
|
|
|
def test_component_not_setup_twice_if_loaded_during_other_setup(self):
|
2016-10-08 18:27:35 +00:00
|
|
|
"""Test component setup while waiting for lock is not setup twice."""
|
2016-03-30 05:50:38 +00:00
|
|
|
loader.set_component('comp', MockModule('comp'))
|
|
|
|
|
|
|
|
result = []
|
|
|
|
|
|
|
|
def setup_component():
|
2016-10-08 18:27:35 +00:00
|
|
|
"""Setup the component."""
|
2016-03-30 05:50:38 +00:00
|
|
|
result.append(bootstrap.setup_component(self.hass, 'comp'))
|
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
thread = threading.Thread(target=setup_component)
|
|
|
|
thread.start()
|
|
|
|
self.hass.config.components.append('comp')
|
2016-03-30 05:50:38 +00:00
|
|
|
|
|
|
|
thread.join()
|
|
|
|
|
|
|
|
assert len(result) == 1
|
|
|
|
assert result[0]
|
|
|
|
|
|
|
|
def test_component_not_setup_missing_dependencies(self):
|
|
|
|
"""Test we do not setup a component if not all dependencies loaded."""
|
|
|
|
deps = ['non_existing']
|
|
|
|
loader.set_component('comp', MockModule('comp', dependencies=deps))
|
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
assert not bootstrap.setup_component(self.hass, 'comp', {})
|
2016-03-30 05:50:38 +00:00
|
|
|
assert 'comp' not in self.hass.config.components
|
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
loader.set_component('non_existing', MockModule('non_existing'))
|
2016-03-30 05:50:38 +00:00
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
assert bootstrap.setup_component(self.hass, 'comp', {})
|
2016-03-30 05:50:38 +00:00
|
|
|
|
|
|
|
def test_component_failing_setup(self):
|
|
|
|
"""Test component that fails setup."""
|
|
|
|
loader.set_component(
|
|
|
|
'comp', MockModule('comp', setup=lambda hass, config: False))
|
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
assert not bootstrap.setup_component(self.hass, 'comp', {})
|
2016-03-30 05:50:38 +00:00
|
|
|
assert 'comp' not in self.hass.config.components
|
|
|
|
|
|
|
|
def test_component_exception_setup(self):
|
|
|
|
"""Test component that raises exception during setup."""
|
|
|
|
def exception_setup(hass, config):
|
|
|
|
"""Setup that raises exception."""
|
|
|
|
raise Exception('fail!')
|
|
|
|
|
|
|
|
loader.set_component('comp', MockModule('comp', setup=exception_setup))
|
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
assert not bootstrap.setup_component(self.hass, 'comp', {})
|
2016-03-30 05:50:38 +00:00
|
|
|
assert 'comp' not in self.hass.config.components
|
|
|
|
|
2016-06-27 16:02:45 +00:00
|
|
|
def test_home_assistant_core_config_validation(self):
|
2016-03-30 05:50:38 +00:00
|
|
|
"""Test if we pass in wrong information for HA conf."""
|
|
|
|
# Extensive HA conf validation testing is done in test_config.py
|
|
|
|
assert None is bootstrap.from_config_dict({
|
|
|
|
'homeassistant': {
|
|
|
|
'latitude': 'some string'
|
|
|
|
}
|
2016-10-27 07:16:23 +00:00
|
|
|
})
|
2016-04-01 04:11:14 +00:00
|
|
|
|
|
|
|
def test_component_setup_with_validation_and_dependency(self):
|
|
|
|
"""Test all config is passed to dependencies."""
|
|
|
|
def config_check_setup(hass, config):
|
|
|
|
"""Setup method that tests config is passed in."""
|
|
|
|
if config.get('comp_a', {}).get('valid', False):
|
|
|
|
return True
|
|
|
|
raise Exception('Config not passed in: {}'.format(config))
|
|
|
|
|
|
|
|
loader.set_component('comp_a',
|
|
|
|
MockModule('comp_a', setup=config_check_setup))
|
|
|
|
|
|
|
|
loader.set_component('switch.platform_a', MockPlatform('comp_b',
|
|
|
|
['comp_a']))
|
|
|
|
|
2016-04-03 03:10:57 +00:00
|
|
|
bootstrap.setup_component(self.hass, 'switch', {
|
2016-04-01 04:11:14 +00:00
|
|
|
'comp_a': {
|
|
|
|
'valid': True
|
|
|
|
},
|
|
|
|
'switch': {
|
|
|
|
'platform': 'platform_a',
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assert 'comp_a' in self.hass.config.components
|
2016-04-03 03:10:57 +00:00
|
|
|
|
|
|
|
def test_platform_specific_config_validation(self):
|
|
|
|
"""Test platform that specifies config."""
|
|
|
|
platform_schema = PLATFORM_SCHEMA.extend({
|
|
|
|
'valid': True,
|
|
|
|
}, extra=vol.PREVENT_EXTRA)
|
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
mock_setup = mock.MagicMock(spec_set=True)
|
2016-10-08 18:27:35 +00:00
|
|
|
|
2016-04-03 03:10:57 +00:00
|
|
|
loader.set_component(
|
|
|
|
'switch.platform_a',
|
2016-10-08 18:27:35 +00:00
|
|
|
MockPlatform(platform_schema=platform_schema,
|
|
|
|
setup_platform=mock_setup))
|
|
|
|
|
|
|
|
with assert_setup_component(0):
|
|
|
|
assert bootstrap.setup_component(self.hass, 'switch', {
|
|
|
|
'switch': {
|
|
|
|
'platform': 'platform_a',
|
|
|
|
'invalid': True
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assert mock_setup.call_count == 0
|
|
|
|
|
|
|
|
self.hass.config.components.remove('switch')
|
|
|
|
|
|
|
|
with assert_setup_component(0):
|
|
|
|
assert bootstrap.setup_component(self.hass, 'switch', {
|
|
|
|
'switch': {
|
|
|
|
'platform': 'platform_a',
|
|
|
|
'valid': True,
|
|
|
|
'invalid_extra': True,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assert mock_setup.call_count == 0
|
|
|
|
|
|
|
|
self.hass.config.components.remove('switch')
|
|
|
|
|
|
|
|
with assert_setup_component(1):
|
|
|
|
assert bootstrap.setup_component(self.hass, 'switch', {
|
|
|
|
'switch': {
|
|
|
|
'platform': 'platform_a',
|
|
|
|
'valid': True
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assert mock_setup.call_count == 1
|
2016-07-26 05:49:10 +00:00
|
|
|
|
|
|
|
def test_disable_component_if_invalid_return(self):
|
|
|
|
"""Test disabling component if invalid return."""
|
|
|
|
loader.set_component(
|
|
|
|
'disabled_component',
|
|
|
|
MockModule('disabled_component', setup=lambda hass, config: None))
|
|
|
|
|
|
|
|
assert not bootstrap.setup_component(self.hass, 'disabled_component')
|
|
|
|
assert loader.get_component('disabled_component') is None
|
|
|
|
assert 'disabled_component' not in self.hass.config.components
|
|
|
|
|
|
|
|
loader.set_component(
|
|
|
|
'disabled_component',
|
|
|
|
MockModule('disabled_component', setup=lambda hass, config: False))
|
|
|
|
|
|
|
|
assert not bootstrap.setup_component(self.hass, 'disabled_component')
|
|
|
|
assert loader.get_component('disabled_component') is not None
|
|
|
|
assert 'disabled_component' not in self.hass.config.components
|
|
|
|
|
|
|
|
loader.set_component(
|
|
|
|
'disabled_component',
|
|
|
|
MockModule('disabled_component', setup=lambda hass, config: True))
|
|
|
|
|
|
|
|
assert bootstrap.setup_component(self.hass, 'disabled_component')
|
|
|
|
assert loader.get_component('disabled_component') is not None
|
|
|
|
assert 'disabled_component' in self.hass.config.components
|