Make setup tests async (#64456)
Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
parent
7592347715
commit
7d85c00b91
1 changed files with 371 additions and 388 deletions
|
@ -2,7 +2,6 @@
|
||||||
# pylint: disable=protected-access
|
# pylint: disable=protected-access
|
||||||
import asyncio
|
import asyncio
|
||||||
import datetime
|
import datetime
|
||||||
import os
|
|
||||||
import threading
|
import threading
|
||||||
from unittest.mock import AsyncMock, Mock, patch
|
from unittest.mock import AsyncMock, Mock, patch
|
||||||
|
|
||||||
|
@ -10,7 +9,6 @@ import pytest
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import config_entries, setup
|
from homeassistant import config_entries, setup
|
||||||
import homeassistant.config as config_util
|
|
||||||
from homeassistant.const import EVENT_COMPONENT_LOADED, EVENT_HOMEASSISTANT_START
|
from homeassistant.const import EVENT_COMPONENT_LOADED, EVENT_HOMEASSISTANT_START
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.helpers import discovery
|
from homeassistant.helpers import discovery
|
||||||
|
@ -18,24 +16,18 @@ from homeassistant.helpers.config_validation import (
|
||||||
PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA,
|
||||||
PLATFORM_SCHEMA_BASE,
|
PLATFORM_SCHEMA_BASE,
|
||||||
)
|
)
|
||||||
import homeassistant.util.dt as dt_util
|
|
||||||
|
|
||||||
from tests.common import (
|
from tests.common import (
|
||||||
MockConfigEntry,
|
MockConfigEntry,
|
||||||
MockModule,
|
MockModule,
|
||||||
MockPlatform,
|
MockPlatform,
|
||||||
assert_setup_component,
|
assert_setup_component,
|
||||||
get_test_config_dir,
|
|
||||||
get_test_home_assistant,
|
|
||||||
mock_entity_platform,
|
mock_entity_platform,
|
||||||
mock_integration,
|
mock_integration,
|
||||||
)
|
)
|
||||||
|
|
||||||
ORIG_TIMEZONE = dt_util.DEFAULT_TIME_ZONE
|
|
||||||
VERSION_PATH = os.path.join(get_test_config_dir(), config_util.VERSION_FILE)
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
@pytest.fixture(autouse=True)
|
|
||||||
def mock_handlers():
|
def mock_handlers():
|
||||||
"""Mock config flows."""
|
"""Mock config flows."""
|
||||||
|
|
||||||
|
@ -48,438 +40,429 @@ def mock_handlers():
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
||||||
class TestSetup:
|
async def test_validate_component_config(hass):
|
||||||
"""Test the bootstrap utils."""
|
"""Test validating component configuration."""
|
||||||
|
config_schema = vol.Schema({"comp_conf": {"hello": str}}, required=True)
|
||||||
|
mock_integration(hass, MockModule("comp_conf", config_schema=config_schema))
|
||||||
|
|
||||||
hass = None
|
with assert_setup_component(0):
|
||||||
backup_cache = None
|
assert not await setup.async_setup_component(hass, "comp_conf", {})
|
||||||
|
|
||||||
# pylint: disable=invalid-name, no-self-use
|
hass.data.pop(setup.DATA_SETUP)
|
||||||
def setup_method(self, method):
|
|
||||||
"""Set up the test."""
|
|
||||||
self.hass = get_test_home_assistant()
|
|
||||||
|
|
||||||
def teardown_method(self, method):
|
with assert_setup_component(0):
|
||||||
"""Clean up."""
|
assert not await setup.async_setup_component(
|
||||||
self.hass.stop()
|
hass, "comp_conf", {"comp_conf": None}
|
||||||
|
|
||||||
def test_validate_component_config(self):
|
|
||||||
"""Test validating component configuration."""
|
|
||||||
config_schema = vol.Schema({"comp_conf": {"hello": str}}, required=True)
|
|
||||||
mock_integration(
|
|
||||||
self.hass, MockModule("comp_conf", config_schema=config_schema)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
with assert_setup_component(0):
|
hass.data.pop(setup.DATA_SETUP)
|
||||||
assert not setup.setup_component(self.hass, "comp_conf", {})
|
|
||||||
|
|
||||||
self.hass.data.pop(setup.DATA_SETUP)
|
with assert_setup_component(0):
|
||||||
|
assert not await setup.async_setup_component(
|
||||||
with assert_setup_component(0):
|
hass, "comp_conf", {"comp_conf": {}}
|
||||||
assert not setup.setup_component(
|
|
||||||
self.hass, "comp_conf", {"comp_conf": None}
|
|
||||||
)
|
|
||||||
|
|
||||||
self.hass.data.pop(setup.DATA_SETUP)
|
|
||||||
|
|
||||||
with assert_setup_component(0):
|
|
||||||
assert not setup.setup_component(self.hass, "comp_conf", {"comp_conf": {}})
|
|
||||||
|
|
||||||
self.hass.data.pop(setup.DATA_SETUP)
|
|
||||||
|
|
||||||
with assert_setup_component(0):
|
|
||||||
assert not setup.setup_component(
|
|
||||||
self.hass,
|
|
||||||
"comp_conf",
|
|
||||||
{"comp_conf": {"hello": "world", "invalid": "extra"}},
|
|
||||||
)
|
|
||||||
|
|
||||||
self.hass.data.pop(setup.DATA_SETUP)
|
|
||||||
|
|
||||||
with assert_setup_component(1):
|
|
||||||
assert setup.setup_component(
|
|
||||||
self.hass, "comp_conf", {"comp_conf": {"hello": "world"}}
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_validate_platform_config(self, caplog):
|
|
||||||
"""Test validating platform configuration."""
|
|
||||||
platform_schema = PLATFORM_SCHEMA.extend({"hello": str})
|
|
||||||
platform_schema_base = PLATFORM_SCHEMA_BASE.extend({})
|
|
||||||
mock_integration(
|
|
||||||
self.hass,
|
|
||||||
MockModule("platform_conf", platform_schema_base=platform_schema_base),
|
|
||||||
)
|
|
||||||
mock_entity_platform(
|
|
||||||
self.hass,
|
|
||||||
"platform_conf.whatever",
|
|
||||||
MockPlatform(platform_schema=platform_schema),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
with assert_setup_component(0):
|
hass.data.pop(setup.DATA_SETUP)
|
||||||
assert setup.setup_component(
|
|
||||||
self.hass,
|
|
||||||
"platform_conf",
|
|
||||||
{"platform_conf": {"platform": "not_existing", "hello": "world"}},
|
|
||||||
)
|
|
||||||
|
|
||||||
self.hass.data.pop(setup.DATA_SETUP)
|
with assert_setup_component(0):
|
||||||
self.hass.config.components.remove("platform_conf")
|
assert not await setup.async_setup_component(
|
||||||
|
hass,
|
||||||
with assert_setup_component(1):
|
"comp_conf",
|
||||||
assert setup.setup_component(
|
{"comp_conf": {"hello": "world", "invalid": "extra"}},
|
||||||
self.hass,
|
|
||||||
"platform_conf",
|
|
||||||
{"platform_conf": {"platform": "whatever", "hello": "world"}},
|
|
||||||
)
|
|
||||||
|
|
||||||
self.hass.data.pop(setup.DATA_SETUP)
|
|
||||||
self.hass.config.components.remove("platform_conf")
|
|
||||||
|
|
||||||
with assert_setup_component(1):
|
|
||||||
assert setup.setup_component(
|
|
||||||
self.hass,
|
|
||||||
"platform_conf",
|
|
||||||
{"platform_conf": [{"platform": "whatever", "hello": "world"}]},
|
|
||||||
)
|
|
||||||
|
|
||||||
self.hass.data.pop(setup.DATA_SETUP)
|
|
||||||
self.hass.config.components.remove("platform_conf")
|
|
||||||
|
|
||||||
# Any falsey platform config will be ignored (None, {}, etc)
|
|
||||||
with assert_setup_component(0) as config:
|
|
||||||
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
|
|
||||||
|
|
||||||
def test_validate_platform_config_2(self, caplog):
|
|
||||||
"""Test component PLATFORM_SCHEMA_BASE prio over PLATFORM_SCHEMA."""
|
|
||||||
platform_schema = PLATFORM_SCHEMA.extend({"hello": str})
|
|
||||||
platform_schema_base = PLATFORM_SCHEMA_BASE.extend({"hello": "world"})
|
|
||||||
mock_integration(
|
|
||||||
self.hass,
|
|
||||||
MockModule(
|
|
||||||
"platform_conf",
|
|
||||||
platform_schema=platform_schema,
|
|
||||||
platform_schema_base=platform_schema_base,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
mock_entity_platform(
|
hass.data.pop(setup.DATA_SETUP)
|
||||||
self.hass,
|
|
||||||
"platform_conf.whatever",
|
with assert_setup_component(1):
|
||||||
MockPlatform("whatever", platform_schema=platform_schema),
|
assert await setup.async_setup_component(
|
||||||
|
hass, "comp_conf", {"comp_conf": {"hello": "world"}}
|
||||||
)
|
)
|
||||||
|
|
||||||
with assert_setup_component(1):
|
|
||||||
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"},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_validate_platform_config_3(self, caplog):
|
async def test_validate_platform_config(hass, caplog):
|
||||||
"""Test fallback to component PLATFORM_SCHEMA."""
|
"""Test validating platform configuration."""
|
||||||
component_schema = PLATFORM_SCHEMA_BASE.extend({"hello": str})
|
platform_schema = PLATFORM_SCHEMA.extend({"hello": str})
|
||||||
platform_schema = PLATFORM_SCHEMA.extend({"cheers": str, "hello": "world"})
|
platform_schema_base = PLATFORM_SCHEMA_BASE.extend({})
|
||||||
mock_integration(
|
mock_integration(
|
||||||
self.hass, MockModule("platform_conf", platform_schema=component_schema)
|
hass,
|
||||||
|
MockModule("platform_conf", platform_schema_base=platform_schema_base),
|
||||||
|
)
|
||||||
|
mock_entity_platform(
|
||||||
|
hass,
|
||||||
|
"platform_conf.whatever",
|
||||||
|
MockPlatform(platform_schema=platform_schema),
|
||||||
|
)
|
||||||
|
|
||||||
|
with assert_setup_component(0):
|
||||||
|
assert await setup.async_setup_component(
|
||||||
|
hass,
|
||||||
|
"platform_conf",
|
||||||
|
{"platform_conf": {"platform": "not_existing", "hello": "world"}},
|
||||||
)
|
)
|
||||||
|
|
||||||
mock_entity_platform(
|
hass.data.pop(setup.DATA_SETUP)
|
||||||
self.hass,
|
hass.config.components.remove("platform_conf")
|
||||||
"platform_conf.whatever",
|
|
||||||
MockPlatform("whatever", platform_schema=platform_schema),
|
with assert_setup_component(1):
|
||||||
|
assert await setup.async_setup_component(
|
||||||
|
hass,
|
||||||
|
"platform_conf",
|
||||||
|
{"platform_conf": {"platform": "whatever", "hello": "world"}},
|
||||||
)
|
)
|
||||||
|
|
||||||
with assert_setup_component(1):
|
hass.data.pop(setup.DATA_SETUP)
|
||||||
assert setup.setup_component(
|
hass.config.components.remove("platform_conf")
|
||||||
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"},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_validate_platform_config_4(self):
|
with assert_setup_component(1):
|
||||||
"""Test entity_namespace in PLATFORM_SCHEMA."""
|
assert await setup.async_setup_component(
|
||||||
component_schema = PLATFORM_SCHEMA_BASE
|
hass,
|
||||||
platform_schema = PLATFORM_SCHEMA
|
"platform_conf",
|
||||||
mock_integration(
|
{"platform_conf": [{"platform": "whatever", "hello": "world"}]},
|
||||||
self.hass,
|
|
||||||
MockModule("platform_conf", platform_schema_base=component_schema),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
mock_entity_platform(
|
hass.data.pop(setup.DATA_SETUP)
|
||||||
self.hass,
|
hass.config.components.remove("platform_conf")
|
||||||
"platform_conf.whatever",
|
|
||||||
MockPlatform(platform_schema=platform_schema),
|
# Any falsey platform config will be ignored (None, {}, etc)
|
||||||
|
with assert_setup_component(0) as config:
|
||||||
|
assert await setup.async_setup_component(
|
||||||
|
hass, "platform_conf", {"platform_conf": None}
|
||||||
|
)
|
||||||
|
assert "platform_conf" in hass.config.components
|
||||||
|
assert not config["platform_conf"] # empty
|
||||||
|
|
||||||
|
assert await setup.async_setup_component(
|
||||||
|
hass, "platform_conf", {"platform_conf": {}}
|
||||||
|
)
|
||||||
|
assert "platform_conf" in hass.config.components
|
||||||
|
assert not config["platform_conf"] # empty
|
||||||
|
|
||||||
|
|
||||||
|
async def test_validate_platform_config_2(hass, caplog):
|
||||||
|
"""Test component PLATFORM_SCHEMA_BASE prio over PLATFORM_SCHEMA."""
|
||||||
|
platform_schema = PLATFORM_SCHEMA.extend({"hello": str})
|
||||||
|
platform_schema_base = PLATFORM_SCHEMA_BASE.extend({"hello": "world"})
|
||||||
|
mock_integration(
|
||||||
|
hass,
|
||||||
|
MockModule(
|
||||||
|
"platform_conf",
|
||||||
|
platform_schema=platform_schema,
|
||||||
|
platform_schema_base=platform_schema_base,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
mock_entity_platform(
|
||||||
|
hass,
|
||||||
|
"platform_conf.whatever",
|
||||||
|
MockPlatform("whatever", platform_schema=platform_schema),
|
||||||
|
)
|
||||||
|
|
||||||
|
with assert_setup_component(1):
|
||||||
|
assert await setup.async_setup_component(
|
||||||
|
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"},
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
with assert_setup_component(1):
|
|
||||||
assert setup.setup_component(
|
|
||||||
self.hass,
|
|
||||||
"platform_conf",
|
|
||||||
{
|
|
||||||
"platform_conf": {
|
|
||||||
# pass: entity_namespace accepted by PLATFORM_SCHEMA
|
|
||||||
"platform": "whatever",
|
|
||||||
"entity_namespace": "yummy",
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
self.hass.data.pop(setup.DATA_SETUP)
|
async def test_validate_platform_config_3(hass, caplog):
|
||||||
self.hass.config.components.remove("platform_conf")
|
"""Test fallback to component PLATFORM_SCHEMA."""
|
||||||
|
component_schema = PLATFORM_SCHEMA_BASE.extend({"hello": str})
|
||||||
|
platform_schema = PLATFORM_SCHEMA.extend({"cheers": str, "hello": "world"})
|
||||||
|
mock_integration(
|
||||||
|
hass, MockModule("platform_conf", platform_schema=component_schema)
|
||||||
|
)
|
||||||
|
|
||||||
def test_component_not_found(self):
|
mock_entity_platform(
|
||||||
"""setup_component should not crash if component doesn't exist."""
|
hass,
|
||||||
assert setup.setup_component(self.hass, "non_existing", {}) is False
|
"platform_conf.whatever",
|
||||||
|
MockPlatform("whatever", platform_schema=platform_schema),
|
||||||
|
)
|
||||||
|
|
||||||
def test_component_not_double_initialized(self):
|
with assert_setup_component(1):
|
||||||
"""Test we do not set up a component twice."""
|
assert await setup.async_setup_component(
|
||||||
mock_setup = Mock(return_value=True)
|
hass,
|
||||||
|
"platform_conf",
|
||||||
mock_integration(self.hass, MockModule("comp", setup=mock_setup))
|
{
|
||||||
|
# pass
|
||||||
assert setup.setup_component(self.hass, "comp", {})
|
"platform_conf": {"platform": "whatever", "hello": "world"},
|
||||||
assert mock_setup.called
|
# fail: key hello violates component platform_schema
|
||||||
|
"platform_conf 2": {"platform": "whatever", "hello": "there"},
|
||||||
mock_setup.reset_mock()
|
},
|
||||||
|
|
||||||
assert setup.setup_component(self.hass, "comp", {})
|
|
||||||
assert not mock_setup.called
|
|
||||||
|
|
||||||
@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."""
|
|
||||||
self.hass.config.skip_pip = False
|
|
||||||
mock_integration(self.hass, MockModule("comp", requirements=["package==0.0.1"]))
|
|
||||||
|
|
||||||
assert not setup.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):
|
|
||||||
"""Test component setup while waiting for lock is not set up twice."""
|
|
||||||
result = []
|
|
||||||
|
|
||||||
async def async_setup(hass, config):
|
|
||||||
"""Tracking Setup."""
|
|
||||||
result.append(1)
|
|
||||||
|
|
||||||
mock_integration(self.hass, MockModule("comp", async_setup=async_setup))
|
|
||||||
|
|
||||||
def setup_component():
|
|
||||||
"""Set up the component."""
|
|
||||||
setup.setup_component(self.hass, "comp", {})
|
|
||||||
|
|
||||||
thread = threading.Thread(target=setup_component)
|
|
||||||
thread.start()
|
|
||||||
setup.setup_component(self.hass, "comp", {})
|
|
||||||
|
|
||||||
thread.join()
|
|
||||||
|
|
||||||
assert len(result) == 1
|
|
||||||
|
|
||||||
def test_component_not_setup_missing_dependencies(self):
|
|
||||||
"""Test we do not set up a component if not all dependencies loaded."""
|
|
||||||
deps = ["maybe_existing"]
|
|
||||||
mock_integration(self.hass, MockModule("comp", dependencies=deps))
|
|
||||||
|
|
||||||
assert not setup.setup_component(self.hass, "comp", {})
|
|
||||||
assert "comp" not in self.hass.config.components
|
|
||||||
|
|
||||||
self.hass.data.pop(setup.DATA_SETUP)
|
|
||||||
|
|
||||||
mock_integration(self.hass, MockModule("comp2", dependencies=deps))
|
|
||||||
mock_integration(self.hass, MockModule("maybe_existing"))
|
|
||||||
|
|
||||||
assert setup.setup_component(self.hass, "comp2", {})
|
|
||||||
|
|
||||||
def test_component_failing_setup(self):
|
|
||||||
"""Test component that fails setup."""
|
|
||||||
mock_integration(
|
|
||||||
self.hass, MockModule("comp", setup=lambda hass, config: False)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
assert not setup.setup_component(self.hass, "comp", {})
|
|
||||||
assert "comp" not in self.hass.config.components
|
|
||||||
|
|
||||||
def test_component_exception_setup(self):
|
async def test_validate_platform_config_4(hass):
|
||||||
"""Test component that raises exception during setup."""
|
"""Test entity_namespace in PLATFORM_SCHEMA."""
|
||||||
|
component_schema = PLATFORM_SCHEMA_BASE
|
||||||
|
platform_schema = PLATFORM_SCHEMA
|
||||||
|
mock_integration(
|
||||||
|
hass,
|
||||||
|
MockModule("platform_conf", platform_schema_base=component_schema),
|
||||||
|
)
|
||||||
|
|
||||||
def exception_setup(hass, config):
|
mock_entity_platform(
|
||||||
"""Raise exception."""
|
hass,
|
||||||
raise Exception("fail!")
|
"platform_conf.whatever",
|
||||||
|
MockPlatform(platform_schema=platform_schema),
|
||||||
|
)
|
||||||
|
|
||||||
mock_integration(self.hass, MockModule("comp", setup=exception_setup))
|
with assert_setup_component(1):
|
||||||
|
assert await setup.async_setup_component(
|
||||||
assert not setup.setup_component(self.hass, "comp", {})
|
hass,
|
||||||
assert "comp" not in self.hass.config.components
|
"platform_conf",
|
||||||
|
{
|
||||||
def test_component_setup_with_validation_and_dependency(self):
|
"platform_conf": {
|
||||||
"""Test all config is passed to dependencies."""
|
# pass: entity_namespace accepted by PLATFORM_SCHEMA
|
||||||
|
"platform": "whatever",
|
||||||
def config_check_setup(hass, config):
|
"entity_namespace": "yummy",
|
||||||
"""Test that config is passed in."""
|
}
|
||||||
if config.get("comp_a", {}).get("valid", False):
|
},
|
||||||
return True
|
|
||||||
raise Exception(f"Config not passed in: {config}")
|
|
||||||
|
|
||||||
platform = MockPlatform()
|
|
||||||
|
|
||||||
mock_integration(self.hass, MockModule("comp_a", setup=config_check_setup))
|
|
||||||
mock_integration(
|
|
||||||
self.hass,
|
|
||||||
MockModule("platform_a", setup=config_check_setup, dependencies=["comp_a"]),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
mock_entity_platform(self.hass, "switch.platform_a", platform)
|
hass.data.pop(setup.DATA_SETUP)
|
||||||
|
hass.config.components.remove("platform_conf")
|
||||||
|
|
||||||
setup.setup_component(
|
|
||||||
self.hass,
|
async def test_component_not_found(hass):
|
||||||
|
"""setup_component should not crash if component doesn't exist."""
|
||||||
|
assert await setup.async_setup_component(hass, "non_existing", {}) is False
|
||||||
|
|
||||||
|
|
||||||
|
async def test_component_not_double_initialized(hass):
|
||||||
|
"""Test we do not set up a component twice."""
|
||||||
|
mock_setup = Mock(return_value=True)
|
||||||
|
|
||||||
|
mock_integration(hass, MockModule("comp", setup=mock_setup))
|
||||||
|
|
||||||
|
assert await setup.async_setup_component(hass, "comp", {})
|
||||||
|
assert mock_setup.called
|
||||||
|
|
||||||
|
mock_setup.reset_mock()
|
||||||
|
|
||||||
|
assert await setup.async_setup_component(hass, "comp", {})
|
||||||
|
assert not mock_setup.called
|
||||||
|
|
||||||
|
|
||||||
|
async def test_component_not_installed_if_requirement_fails(hass):
|
||||||
|
"""Component setup should fail if requirement can't install."""
|
||||||
|
hass.config.skip_pip = False
|
||||||
|
mock_integration(hass, MockModule("comp", requirements=["package==0.0.1"]))
|
||||||
|
|
||||||
|
with patch("homeassistant.util.package.install_package", return_value=False):
|
||||||
|
assert not await setup.async_setup_component(hass, "comp", {})
|
||||||
|
|
||||||
|
assert "comp" not in hass.config.components
|
||||||
|
|
||||||
|
|
||||||
|
async def test_component_not_setup_twice_if_loaded_during_other_setup(hass):
|
||||||
|
"""Test component setup while waiting for lock is not set up twice."""
|
||||||
|
result = []
|
||||||
|
|
||||||
|
async def async_setup(hass, config):
|
||||||
|
"""Tracking Setup."""
|
||||||
|
result.append(1)
|
||||||
|
|
||||||
|
mock_integration(hass, MockModule("comp", async_setup=async_setup))
|
||||||
|
|
||||||
|
def setup_component():
|
||||||
|
"""Set up the component."""
|
||||||
|
setup.setup_component(hass, "comp", {})
|
||||||
|
|
||||||
|
thread = threading.Thread(target=setup_component)
|
||||||
|
thread.start()
|
||||||
|
await setup.async_setup_component(hass, "comp", {})
|
||||||
|
|
||||||
|
await hass.async_add_executor_job(thread.join)
|
||||||
|
|
||||||
|
assert len(result) == 1
|
||||||
|
|
||||||
|
|
||||||
|
async def test_component_not_setup_missing_dependencies(hass):
|
||||||
|
"""Test we do not set up a component if not all dependencies loaded."""
|
||||||
|
deps = ["maybe_existing"]
|
||||||
|
mock_integration(hass, MockModule("comp", dependencies=deps))
|
||||||
|
|
||||||
|
assert not await setup.async_setup_component(hass, "comp", {})
|
||||||
|
assert "comp" not in hass.config.components
|
||||||
|
|
||||||
|
hass.data.pop(setup.DATA_SETUP)
|
||||||
|
|
||||||
|
mock_integration(hass, MockModule("comp2", dependencies=deps))
|
||||||
|
mock_integration(hass, MockModule("maybe_existing"))
|
||||||
|
|
||||||
|
assert await setup.async_setup_component(hass, "comp2", {})
|
||||||
|
|
||||||
|
|
||||||
|
async def test_component_failing_setup(hass):
|
||||||
|
"""Test component that fails setup."""
|
||||||
|
mock_integration(hass, MockModule("comp", setup=lambda hass, config: False))
|
||||||
|
|
||||||
|
assert not await setup.async_setup_component(hass, "comp", {})
|
||||||
|
assert "comp" not in hass.config.components
|
||||||
|
|
||||||
|
|
||||||
|
async def test_component_exception_setup(hass):
|
||||||
|
"""Test component that raises exception during setup."""
|
||||||
|
|
||||||
|
def exception_setup(hass, config):
|
||||||
|
"""Raise exception."""
|
||||||
|
raise Exception("fail!")
|
||||||
|
|
||||||
|
mock_integration(hass, MockModule("comp", setup=exception_setup))
|
||||||
|
|
||||||
|
assert not await setup.async_setup_component(hass, "comp", {})
|
||||||
|
assert "comp" not in hass.config.components
|
||||||
|
|
||||||
|
|
||||||
|
async def test_component_setup_with_validation_and_dependency(hass):
|
||||||
|
"""Test all config is passed to dependencies."""
|
||||||
|
|
||||||
|
def config_check_setup(hass, config):
|
||||||
|
"""Test that config is passed in."""
|
||||||
|
if config.get("comp_a", {}).get("valid", False):
|
||||||
|
return True
|
||||||
|
raise Exception(f"Config not passed in: {config}")
|
||||||
|
|
||||||
|
platform = MockPlatform()
|
||||||
|
|
||||||
|
mock_integration(hass, MockModule("comp_a", setup=config_check_setup))
|
||||||
|
mock_integration(
|
||||||
|
hass,
|
||||||
|
MockModule("platform_a", setup=config_check_setup, dependencies=["comp_a"]),
|
||||||
|
)
|
||||||
|
|
||||||
|
mock_entity_platform(hass, "switch.platform_a", platform)
|
||||||
|
|
||||||
|
await setup.async_setup_component(
|
||||||
|
hass,
|
||||||
|
"switch",
|
||||||
|
{"comp_a": {"valid": True}, "switch": {"platform": "platform_a"}},
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert "comp_a" in hass.config.components
|
||||||
|
|
||||||
|
|
||||||
|
async def test_platform_specific_config_validation(hass):
|
||||||
|
"""Test platform that specifies config."""
|
||||||
|
platform_schema = PLATFORM_SCHEMA.extend({"valid": True}, extra=vol.PREVENT_EXTRA)
|
||||||
|
|
||||||
|
mock_setup = Mock(spec_set=True)
|
||||||
|
|
||||||
|
mock_entity_platform(
|
||||||
|
hass,
|
||||||
|
"switch.platform_a",
|
||||||
|
MockPlatform(platform_schema=platform_schema, setup_platform=mock_setup),
|
||||||
|
)
|
||||||
|
|
||||||
|
with assert_setup_component(0, "switch"):
|
||||||
|
assert await setup.async_setup_component(
|
||||||
|
hass,
|
||||||
"switch",
|
"switch",
|
||||||
{"comp_a": {"valid": True}, "switch": {"platform": "platform_a"}},
|
{"switch": {"platform": "platform_a", "invalid": True}},
|
||||||
)
|
)
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert "comp_a" in self.hass.config.components
|
assert mock_setup.call_count == 0
|
||||||
|
|
||||||
def test_platform_specific_config_validation(self):
|
hass.data.pop(setup.DATA_SETUP)
|
||||||
"""Test platform that specifies config."""
|
hass.config.components.remove("switch")
|
||||||
platform_schema = PLATFORM_SCHEMA.extend(
|
|
||||||
{"valid": True}, extra=vol.PREVENT_EXTRA
|
with assert_setup_component(0):
|
||||||
|
assert await setup.async_setup_component(
|
||||||
|
hass,
|
||||||
|
"switch",
|
||||||
|
{
|
||||||
|
"switch": {
|
||||||
|
"platform": "platform_a",
|
||||||
|
"valid": True,
|
||||||
|
"invalid_extra": True,
|
||||||
|
}
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert mock_setup.call_count == 0
|
||||||
|
|
||||||
mock_setup = Mock(spec_set=True)
|
hass.data.pop(setup.DATA_SETUP)
|
||||||
|
hass.config.components.remove("switch")
|
||||||
|
|
||||||
mock_entity_platform(
|
with assert_setup_component(1, "switch"):
|
||||||
self.hass,
|
assert await setup.async_setup_component(
|
||||||
"switch.platform_a",
|
hass,
|
||||||
MockPlatform(platform_schema=platform_schema, setup_platform=mock_setup),
|
"switch",
|
||||||
|
{"switch": {"platform": "platform_a", "valid": True}},
|
||||||
)
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert mock_setup.call_count == 1
|
||||||
|
|
||||||
with assert_setup_component(0, "switch"):
|
|
||||||
assert setup.setup_component(
|
|
||||||
self.hass,
|
|
||||||
"switch",
|
|
||||||
{"switch": {"platform": "platform_a", "invalid": True}},
|
|
||||||
)
|
|
||||||
self.hass.block_till_done()
|
|
||||||
assert mock_setup.call_count == 0
|
|
||||||
|
|
||||||
self.hass.data.pop(setup.DATA_SETUP)
|
async def test_disable_component_if_invalid_return(hass):
|
||||||
self.hass.config.components.remove("switch")
|
"""Test disabling component if invalid return."""
|
||||||
|
mock_integration(
|
||||||
|
hass, MockModule("disabled_component", setup=lambda hass, config: None)
|
||||||
|
)
|
||||||
|
|
||||||
with assert_setup_component(0):
|
assert not await setup.async_setup_component(hass, "disabled_component", {})
|
||||||
assert setup.setup_component(
|
assert "disabled_component" not in hass.config.components
|
||||||
self.hass,
|
|
||||||
"switch",
|
|
||||||
{
|
|
||||||
"switch": {
|
|
||||||
"platform": "platform_a",
|
|
||||||
"valid": True,
|
|
||||||
"invalid_extra": True,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
self.hass.block_till_done()
|
|
||||||
assert mock_setup.call_count == 0
|
|
||||||
|
|
||||||
self.hass.data.pop(setup.DATA_SETUP)
|
hass.data.pop(setup.DATA_SETUP)
|
||||||
self.hass.config.components.remove("switch")
|
mock_integration(
|
||||||
|
hass,
|
||||||
|
MockModule("disabled_component", setup=lambda hass, config: False),
|
||||||
|
)
|
||||||
|
|
||||||
with assert_setup_component(1, "switch"):
|
assert not await setup.async_setup_component(hass, "disabled_component", {})
|
||||||
assert setup.setup_component(
|
assert "disabled_component" not in hass.config.components
|
||||||
self.hass,
|
|
||||||
"switch",
|
|
||||||
{"switch": {"platform": "platform_a", "valid": True}},
|
|
||||||
)
|
|
||||||
self.hass.block_till_done()
|
|
||||||
assert mock_setup.call_count == 1
|
|
||||||
|
|
||||||
def test_disable_component_if_invalid_return(self):
|
hass.data.pop(setup.DATA_SETUP)
|
||||||
"""Test disabling component if invalid return."""
|
mock_integration(
|
||||||
mock_integration(
|
hass, MockModule("disabled_component", setup=lambda hass, config: True)
|
||||||
self.hass, MockModule("disabled_component", setup=lambda hass, config: None)
|
)
|
||||||
|
|
||||||
|
assert await setup.async_setup_component(hass, "disabled_component", {})
|
||||||
|
assert "disabled_component" in hass.config.components
|
||||||
|
|
||||||
|
|
||||||
|
async def test_all_work_done_before_start(hass):
|
||||||
|
"""Test all init work done till start."""
|
||||||
|
call_order = []
|
||||||
|
|
||||||
|
async def component1_setup(hass, config):
|
||||||
|
"""Set up mock component."""
|
||||||
|
await discovery.async_discover(
|
||||||
|
hass, "test_component2", {}, "test_component2", {}
|
||||||
)
|
)
|
||||||
|
await discovery.async_discover(
|
||||||
assert not setup.setup_component(self.hass, "disabled_component", {})
|
hass, "test_component3", {}, "test_component3", {}
|
||||||
assert "disabled_component" not in self.hass.config.components
|
|
||||||
|
|
||||||
self.hass.data.pop(setup.DATA_SETUP)
|
|
||||||
mock_integration(
|
|
||||||
self.hass,
|
|
||||||
MockModule("disabled_component", setup=lambda hass, config: False),
|
|
||||||
)
|
)
|
||||||
|
return True
|
||||||
|
|
||||||
assert not setup.setup_component(self.hass, "disabled_component", {})
|
def component_track_setup(hass, config):
|
||||||
assert "disabled_component" not in self.hass.config.components
|
"""Set up mock component."""
|
||||||
|
call_order.append(1)
|
||||||
|
return True
|
||||||
|
|
||||||
self.hass.data.pop(setup.DATA_SETUP)
|
mock_integration(hass, MockModule("test_component1", async_setup=component1_setup))
|
||||||
mock_integration(
|
|
||||||
self.hass, MockModule("disabled_component", setup=lambda hass, config: True)
|
|
||||||
)
|
|
||||||
|
|
||||||
assert setup.setup_component(self.hass, "disabled_component", {})
|
mock_integration(hass, MockModule("test_component2", setup=component_track_setup))
|
||||||
assert "disabled_component" in self.hass.config.components
|
|
||||||
|
|
||||||
def test_all_work_done_before_start(self):
|
mock_integration(hass, MockModule("test_component3", setup=component_track_setup))
|
||||||
"""Test all init work done till start."""
|
|
||||||
call_order = []
|
|
||||||
|
|
||||||
async def component1_setup(hass, config):
|
@callback
|
||||||
"""Set up mock component."""
|
def track_start(event):
|
||||||
await discovery.async_discover(
|
"""Track start event."""
|
||||||
hass, "test_component2", {}, "test_component2", {}
|
call_order.append(2)
|
||||||
)
|
|
||||||
await discovery.async_discover(
|
|
||||||
hass, "test_component3", {}, "test_component3", {}
|
|
||||||
)
|
|
||||||
return True
|
|
||||||
|
|
||||||
def component_track_setup(hass, config):
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, track_start)
|
||||||
"""Set up mock component."""
|
|
||||||
call_order.append(1)
|
|
||||||
return True
|
|
||||||
|
|
||||||
mock_integration(
|
hass.add_job(setup.async_setup_component(hass, "test_component1", {}))
|
||||||
self.hass, MockModule("test_component1", async_setup=component1_setup)
|
await hass.async_block_till_done()
|
||||||
)
|
await hass.async_start()
|
||||||
|
assert call_order == [1, 1, 2]
|
||||||
mock_integration(
|
|
||||||
self.hass, MockModule("test_component2", setup=component_track_setup)
|
|
||||||
)
|
|
||||||
|
|
||||||
mock_integration(
|
|
||||||
self.hass, MockModule("test_component3", setup=component_track_setup)
|
|
||||||
)
|
|
||||||
|
|
||||||
@callback
|
|
||||||
def track_start(event):
|
|
||||||
"""Track start event."""
|
|
||||||
call_order.append(2)
|
|
||||||
|
|
||||||
self.hass.bus.listen_once(EVENT_HOMEASSISTANT_START, track_start)
|
|
||||||
|
|
||||||
self.hass.add_job(setup.async_setup_component(self.hass, "test_component1", {}))
|
|
||||||
self.hass.block_till_done()
|
|
||||||
self.hass.start()
|
|
||||||
assert call_order == [1, 1, 2]
|
|
||||||
|
|
||||||
|
|
||||||
async def test_component_warn_slow_setup(hass):
|
async def test_component_warn_slow_setup(hass):
|
||||||
|
@ -513,7 +496,7 @@ async def test_platform_no_warn_slow(hass):
|
||||||
async def test_platform_error_slow_setup(hass, caplog):
|
async def test_platform_error_slow_setup(hass, caplog):
|
||||||
"""Don't block startup more than SLOW_SETUP_MAX_WAIT."""
|
"""Don't block startup more than SLOW_SETUP_MAX_WAIT."""
|
||||||
|
|
||||||
with patch.object(setup, "SLOW_SETUP_MAX_WAIT", 1):
|
with patch.object(setup, "SLOW_SETUP_MAX_WAIT", 0.1):
|
||||||
called = []
|
called = []
|
||||||
|
|
||||||
async def async_setup(*args):
|
async def async_setup(*args):
|
||||||
|
@ -525,7 +508,7 @@ async def test_platform_error_slow_setup(hass, caplog):
|
||||||
result = await setup.async_setup_component(hass, "test_component1", {})
|
result = await setup.async_setup_component(hass, "test_component1", {})
|
||||||
assert len(called) == 1
|
assert len(called) == 1
|
||||||
assert not result
|
assert not result
|
||||||
assert "test_component1 is taking longer than 1 seconds" in caplog.text
|
assert "test_component1 is taking longer than 0.1 seconds" in caplog.text
|
||||||
|
|
||||||
|
|
||||||
async def test_when_setup_already_loaded(hass):
|
async def test_when_setup_already_loaded(hass):
|
||||||
|
@ -599,7 +582,7 @@ async def test_setup_import_blows_up(hass):
|
||||||
assert not await setup.async_setup_component(hass, "sun", {})
|
assert not await setup.async_setup_component(hass, "sun", {})
|
||||||
|
|
||||||
|
|
||||||
async def test_parallel_entry_setup(hass):
|
async def test_parallel_entry_setup(hass, mock_handlers):
|
||||||
"""Test config entries are set up in parallel."""
|
"""Test config entries are set up in parallel."""
|
||||||
MockConfigEntry(domain="comp", data={"value": 1}).add_to_hass(hass)
|
MockConfigEntry(domain="comp", data={"value": 1}).add_to_hass(hass)
|
||||||
MockConfigEntry(domain="comp", data={"value": 2}).add_to_hass(hass)
|
MockConfigEntry(domain="comp", data={"value": 2}).add_to_hass(hass)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue