2017-03-05 09:41:54 +00:00
|
|
|
"""Test component/platform setup."""
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
import asyncio
|
|
|
|
import os
|
|
|
|
from unittest import mock
|
|
|
|
import threading
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.core import callback
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_START, EVENT_COMPONENT_LOADED
|
2017-03-05 09:41:54 +00:00
|
|
|
import homeassistant.config as config_util
|
2019-04-15 05:31:01 +00:00
|
|
|
from homeassistant import setup
|
2017-03-05 09:41:54 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2019-01-29 00:14:55 +00:00
|
|
|
from homeassistant.helpers.config_validation import (
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
PLATFORM_SCHEMA_BASE,
|
|
|
|
)
|
2017-03-05 09:41:54 +00:00
|
|
|
from homeassistant.helpers import discovery
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
from tests.common import (
|
|
|
|
get_test_home_assistant,
|
|
|
|
MockModule,
|
|
|
|
MockPlatform,
|
|
|
|
assert_setup_component,
|
|
|
|
get_test_config_dir,
|
|
|
|
mock_integration,
|
|
|
|
mock_entity_platform,
|
|
|
|
)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
ORIG_TIMEZONE = dt_util.DEFAULT_TIME_ZONE
|
|
|
|
VERSION_PATH = os.path.join(get_test_config_dir(), config_util.VERSION_FILE)
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class TestSetup:
|
|
|
|
"""Test the bootstrap utils."""
|
|
|
|
|
|
|
|
hass = None
|
|
|
|
backup_cache = None
|
|
|
|
|
|
|
|
# pylint: disable=invalid-name, no-self-use
|
|
|
|
def setup_method(self, method):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up the test."""
|
2017-03-05 09:41:54 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
|
|
|
|
|
|
|
def teardown_method(self, method):
|
|
|
|
"""Clean up."""
|
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
def test_validate_component_config(self):
|
|
|
|
"""Test validating component configuration."""
|
2019-07-31 19:25:30 +00:00
|
|
|
config_schema = vol.Schema({"comp_conf": {"hello": str}}, required=True)
|
2019-04-11 08:26:36 +00:00
|
|
|
mock_integration(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, MockModule("comp_conf", config_schema=config_schema)
|
|
|
|
)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
with assert_setup_component(0):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert not setup.setup_component(self.hass, "comp_conf", {})
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
self.hass.data.pop(setup.DATA_SETUP)
|
|
|
|
|
|
|
|
with assert_setup_component(0):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert not setup.setup_component(
|
|
|
|
self.hass, "comp_conf", {"comp_conf": None}
|
|
|
|
)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
self.hass.data.pop(setup.DATA_SETUP)
|
|
|
|
|
|
|
|
with assert_setup_component(0):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert not setup.setup_component(self.hass, "comp_conf", {"comp_conf": {}})
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
self.hass.data.pop(setup.DATA_SETUP)
|
|
|
|
|
|
|
|
with assert_setup_component(0):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert not setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"comp_conf",
|
|
|
|
{"comp_conf": {"hello": "world", "invalid": "extra"}},
|
|
|
|
)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
self.hass.data.pop(setup.DATA_SETUP)
|
|
|
|
|
|
|
|
with assert_setup_component(1):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass, "comp_conf", {"comp_conf": {"hello": "world"}}
|
|
|
|
)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2019-02-15 09:51:52 +00:00
|
|
|
def test_validate_platform_config(self, caplog):
|
2017-03-05 09:41:54 +00:00
|
|
|
"""Test validating platform configuration."""
|
2019-07-31 19:25:30 +00:00
|
|
|
platform_schema = PLATFORM_SCHEMA.extend({"hello": str})
|
|
|
|
platform_schema_base = PLATFORM_SCHEMA_BASE.extend({})
|
2019-04-11 08:26:36 +00:00
|
|
|
mock_integration(
|
2018-05-01 18:57:30 +00:00
|
|
|
self.hass,
|
2019-07-31 19:25:30 +00:00
|
|
|
MockModule("platform_conf", platform_schema_base=platform_schema_base),
|
2019-04-11 08:26:36 +00:00
|
|
|
)
|
|
|
|
mock_entity_platform(
|
2018-05-01 18:57:30 +00:00
|
|
|
self.hass,
|
2019-07-31 19:25:30 +00:00
|
|
|
"platform_conf.whatever",
|
|
|
|
MockPlatform(platform_schema=platform_schema),
|
|
|
|
)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
with assert_setup_component(0):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"platform_conf",
|
|
|
|
{"platform_conf": {"platform": "not_existing", "hello": "world"}},
|
|
|
|
)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
self.hass.data.pop(setup.DATA_SETUP)
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.config.components.remove("platform_conf")
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
with assert_setup_component(1):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"platform_conf",
|
|
|
|
{"platform_conf": {"platform": "whatever", "hello": "world"}},
|
|
|
|
)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
self.hass.data.pop(setup.DATA_SETUP)
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.config.components.remove("platform_conf")
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
with assert_setup_component(1):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"platform_conf",
|
|
|
|
{"platform_conf": [{"platform": "whatever", "hello": "world"}]},
|
|
|
|
)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
self.hass.data.pop(setup.DATA_SETUP)
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.config.components.remove("platform_conf")
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
# Any falsey platform config will be ignored (None, {}, etc)
|
|
|
|
with assert_setup_component(0) as config:
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass, "platform_conf", {"platform_conf": None}
|
|
|
|
)
|
|
|
|
assert "platform_conf" in self.hass.config.components
|
|
|
|
assert not config["platform_conf"] # empty
|
|
|
|
|
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass, "platform_conf", {"platform_conf": {}}
|
|
|
|
)
|
|
|
|
assert "platform_conf" in self.hass.config.components
|
|
|
|
assert not config["platform_conf"] # empty
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2019-02-15 09:51:52 +00:00
|
|
|
def test_validate_platform_config_2(self, caplog):
|
2019-01-29 00:14:55 +00:00
|
|
|
"""Test component PLATFORM_SCHEMA_BASE prio over PLATFORM_SCHEMA."""
|
2019-07-31 19:25:30 +00:00
|
|
|
platform_schema = PLATFORM_SCHEMA.extend({"hello": str})
|
|
|
|
platform_schema_base = PLATFORM_SCHEMA_BASE.extend({"hello": "world"})
|
2019-04-11 08:26:36 +00:00
|
|
|
mock_integration(
|
2019-01-29 00:14:55 +00:00
|
|
|
self.hass,
|
2019-07-31 19:25:30 +00:00
|
|
|
MockModule(
|
|
|
|
"platform_conf",
|
|
|
|
platform_schema=platform_schema,
|
|
|
|
platform_schema_base=platform_schema_base,
|
|
|
|
),
|
|
|
|
)
|
2019-01-29 00:14:55 +00:00
|
|
|
|
2019-04-11 08:26:36 +00:00
|
|
|
mock_entity_platform(
|
2019-01-29 00:14:55 +00:00
|
|
|
self.hass,
|
2019-07-31 19:25:30 +00:00
|
|
|
"platform_conf.whatever",
|
|
|
|
MockPlatform("whatever", platform_schema=platform_schema),
|
|
|
|
)
|
2019-01-29 00:14:55 +00:00
|
|
|
|
|
|
|
with assert_setup_component(1):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"platform_conf",
|
|
|
|
{
|
|
|
|
# pass
|
|
|
|
"platform_conf": {"platform": "whatever", "hello": "world"},
|
|
|
|
# fail: key hello violates component platform_schema_base
|
|
|
|
"platform_conf 2": {"platform": "whatever", "hello": "there"},
|
2019-01-29 00:14:55 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-01-29 00:14:55 +00:00
|
|
|
|
2019-02-15 09:51:52 +00:00
|
|
|
def test_validate_platform_config_3(self, caplog):
|
2019-01-29 00:14:55 +00:00
|
|
|
"""Test fallback to component PLATFORM_SCHEMA."""
|
2019-07-31 19:25:30 +00:00
|
|
|
component_schema = PLATFORM_SCHEMA_BASE.extend({"hello": str})
|
|
|
|
platform_schema = PLATFORM_SCHEMA.extend({"cheers": str, "hello": "world"})
|
2019-04-11 08:26:36 +00:00
|
|
|
mock_integration(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, MockModule("platform_conf", platform_schema=component_schema)
|
|
|
|
)
|
2019-01-29 00:14:55 +00:00
|
|
|
|
2019-04-11 08:26:36 +00:00
|
|
|
mock_entity_platform(
|
2019-01-29 00:14:55 +00:00
|
|
|
self.hass,
|
2019-07-31 19:25:30 +00:00
|
|
|
"platform_conf.whatever",
|
|
|
|
MockPlatform("whatever", platform_schema=platform_schema),
|
|
|
|
)
|
2019-01-29 00:14:55 +00:00
|
|
|
|
|
|
|
with assert_setup_component(1):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"platform_conf",
|
|
|
|
{
|
|
|
|
# pass
|
|
|
|
"platform_conf": {"platform": "whatever", "hello": "world"},
|
|
|
|
# fail: key hello violates component platform_schema
|
|
|
|
"platform_conf 2": {"platform": "whatever", "hello": "there"},
|
2019-01-29 00:14:55 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-01-29 00:14:55 +00:00
|
|
|
|
2019-02-02 17:31:28 +00:00
|
|
|
def test_validate_platform_config_4(self):
|
|
|
|
"""Test entity_namespace in PLATFORM_SCHEMA."""
|
|
|
|
component_schema = PLATFORM_SCHEMA_BASE
|
|
|
|
platform_schema = PLATFORM_SCHEMA
|
2019-04-11 08:26:36 +00:00
|
|
|
mock_integration(
|
2019-02-02 17:31:28 +00:00
|
|
|
self.hass,
|
2019-07-31 19:25:30 +00:00
|
|
|
MockModule("platform_conf", platform_schema_base=component_schema),
|
|
|
|
)
|
2019-02-02 17:31:28 +00:00
|
|
|
|
2019-04-11 08:26:36 +00:00
|
|
|
mock_entity_platform(
|
2019-02-02 17:31:28 +00:00
|
|
|
self.hass,
|
2019-07-31 19:25:30 +00:00
|
|
|
"platform_conf.whatever",
|
|
|
|
MockPlatform(platform_schema=platform_schema),
|
|
|
|
)
|
2019-02-02 17:31:28 +00:00
|
|
|
|
|
|
|
with assert_setup_component(1):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"platform_conf",
|
|
|
|
{
|
|
|
|
"platform_conf": {
|
|
|
|
# pass: entity_namespace accepted by PLATFORM_SCHEMA
|
|
|
|
"platform": "whatever",
|
|
|
|
"entity_namespace": "yummy",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2019-02-02 17:31:28 +00:00
|
|
|
|
|
|
|
self.hass.data.pop(setup.DATA_SETUP)
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.config.components.remove("platform_conf")
|
2019-02-02 17:31:28 +00:00
|
|
|
|
2017-03-05 09:41:54 +00:00
|
|
|
def test_component_not_found(self):
|
|
|
|
"""setup_component should not crash if component doesn't exist."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup.setup_component(self.hass, "non_existing", {}) is False
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
def test_component_not_double_initialized(self):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Test we do not set up a component twice."""
|
2017-03-05 09:41:54 +00:00
|
|
|
mock_setup = mock.MagicMock(return_value=True)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(self.hass, MockModule("comp", setup=mock_setup))
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup.setup_component(self.hass, "comp", {})
|
2017-03-05 09:41:54 +00:00
|
|
|
assert mock_setup.called
|
|
|
|
|
|
|
|
mock_setup.reset_mock()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup.setup_component(self.hass, "comp", {})
|
2017-03-05 09:41:54 +00:00
|
|
|
assert not mock_setup.called
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@mock.patch("homeassistant.util.package.install_package", return_value=False)
|
2017-03-05 09:41:54 +00:00
|
|
|
def test_component_not_installed_if_requirement_fails(self, mock_install):
|
|
|
|
"""Component setup should fail if requirement can't install."""
|
|
|
|
self.hass.config.skip_pip = False
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(self.hass, MockModule("comp", requirements=["package==0.0.1"]))
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert not setup.setup_component(self.hass, "comp", {})
|
|
|
|
assert "comp" not in self.hass.config.components
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
def test_component_not_setup_twice_if_loaded_during_other_setup(self):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Test component setup while waiting for lock is not set up twice."""
|
2017-03-05 09:41:54 +00:00
|
|
|
result = []
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def async_setup(hass, config):
|
|
|
|
"""Tracking Setup."""
|
|
|
|
result.append(1)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(self.hass, MockModule("comp", async_setup=async_setup))
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
def setup_component():
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up the component."""
|
2019-07-31 19:25:30 +00:00
|
|
|
setup.setup_component(self.hass, "comp", {})
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
thread = threading.Thread(target=setup_component)
|
|
|
|
thread.start()
|
2019-07-31 19:25:30 +00:00
|
|
|
setup.setup_component(self.hass, "comp", {})
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
thread.join()
|
|
|
|
|
|
|
|
assert len(result) == 1
|
|
|
|
|
|
|
|
def test_component_not_setup_missing_dependencies(self):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Test we do not set up a component if not all dependencies loaded."""
|
2019-07-31 19:25:30 +00:00
|
|
|
deps = ["maybe_existing"]
|
|
|
|
mock_integration(self.hass, MockModule("comp", dependencies=deps))
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert not setup.setup_component(self.hass, "comp", {})
|
|
|
|
assert "comp" not in self.hass.config.components
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
self.hass.data.pop(setup.DATA_SETUP)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(self.hass, MockModule("comp2", dependencies=deps))
|
|
|
|
mock_integration(self.hass, MockModule("maybe_existing"))
|
2019-04-09 16:30:32 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup.setup_component(self.hass, "comp2", {})
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
def test_component_failing_setup(self):
|
|
|
|
"""Test component that fails setup."""
|
2019-04-11 08:26:36 +00:00
|
|
|
mock_integration(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, MockModule("comp", setup=lambda hass, config: False)
|
|
|
|
)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert not setup.setup_component(self.hass, "comp", {})
|
|
|
|
assert "comp" not in self.hass.config.components
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
def test_component_exception_setup(self):
|
|
|
|
"""Test component that raises exception during setup."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-03-05 09:41:54 +00:00
|
|
|
def exception_setup(hass, config):
|
2018-08-24 08:28:43 +00:00
|
|
|
"""Raise exception."""
|
2019-07-31 19:25:30 +00:00
|
|
|
raise Exception("fail!")
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(self.hass, MockModule("comp", setup=exception_setup))
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert not setup.setup_component(self.hass, "comp", {})
|
|
|
|
assert "comp" not in self.hass.config.components
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
def test_component_setup_with_validation_and_dependency(self):
|
|
|
|
"""Test all config is passed to dependencies."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-03-05 09:41:54 +00:00
|
|
|
def config_check_setup(hass, config):
|
2018-08-24 08:28:43 +00:00
|
|
|
"""Test that config is passed in."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if config.get("comp_a", {}).get("valid", False):
|
2017-03-05 09:41:54 +00:00
|
|
|
return True
|
2019-07-31 19:25:30 +00:00
|
|
|
raise Exception("Config not passed in: {}".format(config))
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2019-04-11 08:26:36 +00:00
|
|
|
platform = MockPlatform()
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(self.hass, MockModule("comp_a", setup=config_check_setup))
|
2019-04-11 08:26:36 +00:00
|
|
|
mock_integration(
|
|
|
|
self.hass,
|
2019-07-31 19:25:30 +00:00
|
|
|
MockModule("platform_a", setup=config_check_setup, dependencies=["comp_a"]),
|
2019-04-11 08:26:36 +00:00
|
|
|
)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_entity_platform(self.hass, "switch.platform_a", platform)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"switch",
|
|
|
|
{"comp_a": {"valid": True}, "switch": {"platform": "platform_a"}},
|
|
|
|
)
|
|
|
|
assert "comp_a" in self.hass.config.components
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
def test_platform_specific_config_validation(self):
|
|
|
|
"""Test platform that specifies config."""
|
2019-07-31 19:25:30 +00:00
|
|
|
platform_schema = PLATFORM_SCHEMA.extend(
|
|
|
|
{"valid": True}, extra=vol.PREVENT_EXTRA
|
|
|
|
)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
mock_setup = mock.MagicMock(spec_set=True)
|
|
|
|
|
2019-04-11 08:26:36 +00:00
|
|
|
mock_entity_platform(
|
2018-05-01 18:57:30 +00:00
|
|
|
self.hass,
|
2019-07-31 19:25:30 +00:00
|
|
|
"switch.platform_a",
|
|
|
|
MockPlatform(platform_schema=platform_schema, setup_platform=mock_setup),
|
|
|
|
)
|
|
|
|
|
|
|
|
with assert_setup_component(0, "switch"):
|
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"switch",
|
|
|
|
{"switch": {"platform": "platform_a", "invalid": True}},
|
|
|
|
)
|
2017-03-05 09:41:54 +00:00
|
|
|
assert mock_setup.call_count == 0
|
|
|
|
|
|
|
|
self.hass.data.pop(setup.DATA_SETUP)
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.config.components.remove("switch")
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
with assert_setup_component(0):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"switch",
|
|
|
|
{
|
|
|
|
"switch": {
|
|
|
|
"platform": "platform_a",
|
|
|
|
"valid": True,
|
|
|
|
"invalid_extra": True,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2017-03-05 09:41:54 +00:00
|
|
|
assert mock_setup.call_count == 0
|
|
|
|
|
|
|
|
self.hass.data.pop(setup.DATA_SETUP)
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.config.components.remove("switch")
|
|
|
|
|
|
|
|
with assert_setup_component(1, "switch"):
|
|
|
|
assert setup.setup_component(
|
|
|
|
self.hass,
|
|
|
|
"switch",
|
|
|
|
{"switch": {"platform": "platform_a", "valid": True}},
|
|
|
|
)
|
2017-03-05 09:41:54 +00:00
|
|
|
assert mock_setup.call_count == 1
|
|
|
|
|
|
|
|
def test_disable_component_if_invalid_return(self):
|
|
|
|
"""Test disabling component if invalid return."""
|
2019-04-11 08:26:36 +00:00
|
|
|
mock_integration(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, MockModule("disabled_component", setup=lambda hass, config: None)
|
|
|
|
)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert not setup.setup_component(self.hass, "disabled_component", {})
|
|
|
|
assert "disabled_component" not in self.hass.config.components
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
self.hass.data.pop(setup.DATA_SETUP)
|
2019-04-11 08:26:36 +00:00
|
|
|
mock_integration(
|
2018-05-01 18:57:30 +00:00
|
|
|
self.hass,
|
2019-07-31 19:25:30 +00:00
|
|
|
MockModule("disabled_component", setup=lambda hass, config: False),
|
|
|
|
)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert not setup.setup_component(self.hass, "disabled_component", {})
|
|
|
|
assert "disabled_component" not in self.hass.config.components
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
self.hass.data.pop(setup.DATA_SETUP)
|
2019-04-11 08:26:36 +00:00
|
|
|
mock_integration(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, MockModule("disabled_component", setup=lambda hass, config: True)
|
|
|
|
)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup.setup_component(self.hass, "disabled_component", {})
|
|
|
|
assert "disabled_component" in self.hass.config.components
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
def test_all_work_done_before_start(self):
|
|
|
|
"""Test all init work done till start."""
|
|
|
|
call_order = []
|
|
|
|
|
|
|
|
def component1_setup(hass, config):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up mock component."""
|
2019-07-31 19:25:30 +00:00
|
|
|
discovery.discover(hass, "test_component2", {}, "test_component2", {})
|
|
|
|
discovery.discover(hass, "test_component3", {}, "test_component3", {})
|
2017-03-05 09:41:54 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def component_track_setup(hass, config):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up mock component."""
|
2017-03-05 09:41:54 +00:00
|
|
|
call_order.append(1)
|
|
|
|
return True
|
|
|
|
|
2019-04-11 08:26:36 +00:00
|
|
|
mock_integration(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, MockModule("test_component1", setup=component1_setup)
|
|
|
|
)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2019-04-11 08:26:36 +00:00
|
|
|
mock_integration(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, MockModule("test_component2", setup=component_track_setup)
|
|
|
|
)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
2019-04-11 08:26:36 +00:00
|
|
|
mock_integration(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, MockModule("test_component3", setup=component_track_setup)
|
|
|
|
)
|
2017-03-05 09:41:54 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def track_start(event):
|
|
|
|
"""Track start event."""
|
|
|
|
call_order.append(2)
|
|
|
|
|
|
|
|
self.hass.bus.listen_once(EVENT_HOMEASSISTANT_START, track_start)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.add_job(setup.async_setup_component(self.hass, "test_component1", {}))
|
2017-03-05 09:41:54 +00:00
|
|
|
self.hass.block_till_done()
|
|
|
|
self.hass.start()
|
|
|
|
assert call_order == [1, 1, 2]
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_component_cannot_depend_config(hass):
|
|
|
|
"""Test config is not allowed to be a dependency."""
|
|
|
|
result = yield from setup._async_process_dependencies(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, None, "test", ["config"]
|
|
|
|
)
|
2017-03-05 09:41:54 +00:00
|
|
|
assert not result
|
2017-03-08 04:31:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_component_warn_slow_setup(hass):
|
|
|
|
"""Warn we log when a component setup takes a long time."""
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(hass, MockModule("test_component1"))
|
|
|
|
with mock.patch.object(hass.loop, "call_later", mock.MagicMock()) as mock_call:
|
|
|
|
result = yield from setup.async_setup_component(hass, "test_component1", {})
|
2017-03-08 04:31:57 +00:00
|
|
|
assert result
|
|
|
|
assert mock_call.called
|
2018-01-02 20:16:32 +00:00
|
|
|
assert len(mock_call.mock_calls) == 3
|
2017-03-08 04:31:57 +00:00
|
|
|
|
|
|
|
timeout, logger_method = mock_call.mock_calls[0][1][:2]
|
|
|
|
|
|
|
|
assert timeout == setup.SLOW_SETUP_WARNING
|
|
|
|
assert logger_method == setup._LOGGER.warning
|
|
|
|
|
|
|
|
assert mock_call().cancel.called
|
2018-01-02 20:16:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_platform_no_warn_slow(hass):
|
|
|
|
"""Do not warn for long entity setup time."""
|
2019-04-11 08:26:36 +00:00
|
|
|
mock_integration(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, MockModule("test_component1", platform_schema=PLATFORM_SCHEMA)
|
|
|
|
)
|
|
|
|
with mock.patch.object(hass.loop, "call_later", mock.MagicMock()) as mock_call:
|
|
|
|
result = yield from setup.async_setup_component(hass, "test_component1", {})
|
2018-01-02 20:16:32 +00:00
|
|
|
assert result
|
|
|
|
assert not mock_call.called
|
2018-11-28 21:20:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_when_setup_already_loaded(hass):
|
|
|
|
"""Test when setup."""
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
async def mock_callback(hass, component):
|
|
|
|
"""Mock callback."""
|
|
|
|
calls.append(component)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
setup.async_when_setup(hass, "test", mock_callback)
|
2018-11-28 21:20:13 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert calls == []
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.config.components.add("test")
|
|
|
|
hass.bus.async_fire(EVENT_COMPONENT_LOADED, {"component": "test"})
|
2018-11-28 21:20:13 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert calls == ["test"]
|
2018-11-28 21:20:13 +00:00
|
|
|
|
|
|
|
# Event listener should be gone
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.bus.async_fire(EVENT_COMPONENT_LOADED, {"component": "test"})
|
2018-11-28 21:20:13 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert calls == ["test"]
|
2018-11-28 21:20:13 +00:00
|
|
|
|
|
|
|
# Should be called right away
|
2019-07-31 19:25:30 +00:00
|
|
|
setup.async_when_setup(hass, "test", mock_callback)
|
2018-11-28 21:20:13 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert calls == ["test", "test"]
|
2019-10-31 18:38:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_setup_import_blows_up(hass):
|
|
|
|
"""Test that we handle it correctly when importing integration blows up."""
|
|
|
|
with mock.patch(
|
|
|
|
"homeassistant.loader.Integration.get_component", side_effect=ValueError
|
|
|
|
):
|
|
|
|
assert not await setup.async_setup_component(hass, "sun", {})
|