2016-03-09 09:25:50 +00:00
|
|
|
"""Test to verify that we can load components."""
|
2017-07-16 16:23:06 +00:00
|
|
|
import pytest
|
|
|
|
|
2019-04-09 16:30:32 +00:00
|
|
|
from homeassistant.components import http, hue
|
|
|
|
from homeassistant.components.hue import light as hue_light
|
2019-12-09 15:52:24 +00:00
|
|
|
import homeassistant.loader as loader
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
from tests.async_mock import ANY, patch
|
2019-04-09 16:30:32 +00:00
|
|
|
from tests.common import MockModule, async_mock_service, mock_integration
|
2014-11-25 08:20:36 +00:00
|
|
|
|
2014-11-23 17:51:16 +00:00
|
|
|
|
2019-04-09 16:30:32 +00:00
|
|
|
async def test_component_dependencies(hass):
|
2019-02-07 21:56:40 +00:00
|
|
|
"""Test if we can get the proper load order of components."""
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(hass, MockModule("mod1"))
|
|
|
|
mock_integration(hass, MockModule("mod2", ["mod1"]))
|
2020-06-20 00:24:33 +00:00
|
|
|
mod_3 = mock_integration(hass, MockModule("mod3", ["mod2"]))
|
2014-11-28 23:34:42 +00:00
|
|
|
|
2020-06-20 00:24:33 +00:00
|
|
|
assert {"mod1", "mod2", "mod3"} == await loader._async_component_dependencies(
|
|
|
|
hass, "mod_3", mod_3, set(), set()
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2014-11-28 23:34:42 +00:00
|
|
|
|
2019-02-07 21:56:40 +00:00
|
|
|
# Create circular dependency
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(hass, MockModule("mod1", ["mod3"]))
|
2014-11-28 23:34:42 +00:00
|
|
|
|
2019-02-07 21:56:40 +00:00
|
|
|
with pytest.raises(loader.CircularDependency):
|
2020-06-20 00:24:33 +00:00
|
|
|
print(
|
|
|
|
await loader._async_component_dependencies(
|
|
|
|
hass, "mod_3", mod_3, set(), set()
|
|
|
|
)
|
|
|
|
)
|
2014-11-28 23:34:42 +00:00
|
|
|
|
2019-02-07 21:56:40 +00:00
|
|
|
# Depend on non-existing component
|
2020-06-20 00:24:33 +00:00
|
|
|
mod_1 = mock_integration(hass, MockModule("mod1", ["nonexisting"]))
|
2014-11-28 23:34:42 +00:00
|
|
|
|
2019-04-09 16:30:32 +00:00
|
|
|
with pytest.raises(loader.IntegrationNotFound):
|
2020-06-20 00:24:33 +00:00
|
|
|
print(
|
|
|
|
await loader._async_component_dependencies(
|
|
|
|
hass, "mod_1", mod_1, set(), set()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Having an after dependency 2 deps down that is circular
|
|
|
|
mod_1 = mock_integration(
|
|
|
|
hass, MockModule("mod1", partial_manifest={"after_dependencies": ["mod_3"]})
|
|
|
|
)
|
2014-11-28 23:34:42 +00:00
|
|
|
|
2020-06-20 00:24:33 +00:00
|
|
|
with pytest.raises(loader.CircularDependency):
|
|
|
|
print(
|
|
|
|
await loader._async_component_dependencies(
|
|
|
|
hass, "mod_3", mod_3, set(), set()
|
|
|
|
)
|
|
|
|
)
|
2017-07-16 16:23:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_component_loader(hass):
|
|
|
|
"""Test loading components."""
|
|
|
|
components = loader.Components(hass)
|
|
|
|
assert components.http.CONFIG_SCHEMA is http.CONFIG_SCHEMA
|
|
|
|
assert hass.components.http.CONFIG_SCHEMA is http.CONFIG_SCHEMA
|
|
|
|
|
|
|
|
|
|
|
|
def test_component_loader_non_existing(hass):
|
|
|
|
"""Test loading components."""
|
|
|
|
components = loader.Components(hass)
|
|
|
|
with pytest.raises(ImportError):
|
|
|
|
components.non_existing
|
|
|
|
|
|
|
|
|
2019-04-11 08:26:36 +00:00
|
|
|
async def test_component_wrapper(hass):
|
2017-07-16 16:23:06 +00:00
|
|
|
"""Test component wrapper."""
|
2019-07-31 19:25:30 +00:00
|
|
|
calls = async_mock_service(hass, "persistent_notification", "create")
|
2017-07-16 16:23:06 +00:00
|
|
|
|
|
|
|
components = loader.Components(hass)
|
2019-07-31 19:25:30 +00:00
|
|
|
components.persistent_notification.async_create("message")
|
2019-04-11 08:26:36 +00:00
|
|
|
await hass.async_block_till_done()
|
2017-07-16 16:23:06 +00:00
|
|
|
|
|
|
|
assert len(calls) == 1
|
2017-10-08 15:17:54 +00:00
|
|
|
|
|
|
|
|
2019-04-11 08:26:36 +00:00
|
|
|
async def test_helpers_wrapper(hass):
|
2017-10-08 15:17:54 +00:00
|
|
|
"""Test helpers wrapper."""
|
|
|
|
helpers = loader.Helpers(hass)
|
|
|
|
|
|
|
|
result = []
|
|
|
|
|
|
|
|
def discovery_callback(service, discovered):
|
|
|
|
"""Handle discovery callback."""
|
|
|
|
result.append(discovered)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
helpers.discovery.async_listen("service_name", discovery_callback)
|
2017-10-08 15:17:54 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await helpers.discovery.async_discover("service_name", "hello", None, {})
|
2019-04-11 08:26:36 +00:00
|
|
|
await hass.async_block_till_done()
|
2017-10-08 15:17:54 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result == ["hello"]
|
2018-05-07 00:54:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_custom_component_name(hass):
|
|
|
|
"""Test the name attribte of custom components."""
|
2019-07-31 19:25:30 +00:00
|
|
|
integration = await loader.async_get_integration(hass, "test_standalone")
|
2019-04-15 05:31:01 +00:00
|
|
|
int_comp = integration.get_component()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert int_comp.__name__ == "custom_components.test_standalone"
|
|
|
|
assert int_comp.__package__ == "custom_components"
|
2019-04-15 05:31:01 +00:00
|
|
|
|
|
|
|
comp = hass.components.test_standalone
|
2019-07-31 19:25:30 +00:00
|
|
|
assert comp.__name__ == "custom_components.test_standalone"
|
|
|
|
assert comp.__package__ == "custom_components"
|
2018-05-07 00:54:56 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
integration = await loader.async_get_integration(hass, "test_package")
|
2019-04-15 05:31:01 +00:00
|
|
|
int_comp = integration.get_component()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert int_comp.__name__ == "custom_components.test_package"
|
|
|
|
assert int_comp.__package__ == "custom_components.test_package"
|
2019-04-15 05:31:01 +00:00
|
|
|
|
|
|
|
comp = hass.components.test_package
|
2019-07-31 19:25:30 +00:00
|
|
|
assert comp.__name__ == "custom_components.test_package"
|
|
|
|
assert comp.__package__ == "custom_components.test_package"
|
2018-05-07 00:54:56 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
integration = await loader.async_get_integration(hass, "test")
|
|
|
|
platform = integration.get_platform("light")
|
|
|
|
assert platform.__name__ == "custom_components.test.light"
|
|
|
|
assert platform.__package__ == "custom_components.test"
|
2018-05-07 09:25:48 +00:00
|
|
|
|
|
|
|
# Test custom components is mounted
|
|
|
|
from custom_components.test_package import TEST
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-05-07 09:25:48 +00:00
|
|
|
assert TEST == 5
|
2018-06-27 19:21:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_log_warning_custom_component(hass, caplog):
|
|
|
|
"""Test that we log a warning when loading a custom component."""
|
2019-04-15 05:31:01 +00:00
|
|
|
hass.components.test_standalone
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "You are using a custom integration for test_standalone" in caplog.text
|
2018-06-27 19:21:32 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await loader.async_get_integration(hass, "test")
|
|
|
|
assert "You are using a custom integration for test " in caplog.text
|
2019-02-07 21:33:12 +00:00
|
|
|
|
|
|
|
|
2019-04-09 16:30:32 +00:00
|
|
|
async def test_get_integration(hass):
|
|
|
|
"""Test resolving integration."""
|
2019-07-31 19:25:30 +00:00
|
|
|
integration = await loader.async_get_integration(hass, "hue")
|
2019-04-09 16:30:32 +00:00
|
|
|
assert hue == integration.get_component()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert hue_light == integration.get_platform("light")
|
2019-04-09 16:30:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_get_integration_legacy(hass):
|
|
|
|
"""Test resolving integration."""
|
2019-07-31 19:25:30 +00:00
|
|
|
integration = await loader.async_get_integration(hass, "test_embedded")
|
|
|
|
assert integration.get_component().DOMAIN == "test_embedded"
|
|
|
|
assert integration.get_platform("switch") is not None
|
2019-04-09 16:30:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_get_integration_custom_component(hass):
|
|
|
|
"""Test resolving integration."""
|
2019-07-31 19:25:30 +00:00
|
|
|
integration = await loader.async_get_integration(hass, "test_package")
|
2019-04-09 16:30:32 +00:00
|
|
|
print(integration)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert integration.get_component().DOMAIN == "test_package"
|
|
|
|
assert integration.name == "Test Package"
|
2019-04-09 16:30:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_integration_properties(hass):
|
|
|
|
"""Test integration properties."""
|
2019-04-12 17:09:17 +00:00
|
|
|
integration = loader.Integration(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass,
|
|
|
|
"homeassistant.components.hue",
|
|
|
|
None,
|
|
|
|
{
|
|
|
|
"name": "Philips Hue",
|
|
|
|
"domain": "hue",
|
|
|
|
"dependencies": ["test-dep"],
|
|
|
|
"requirements": ["test-req==1.0.0"],
|
|
|
|
},
|
|
|
|
)
|
2019-04-09 16:30:32 +00:00
|
|
|
assert integration.name == "Philips Hue"
|
2019-07-31 19:25:30 +00:00
|
|
|
assert integration.domain == "hue"
|
|
|
|
assert integration.dependencies == ["test-dep"]
|
|
|
|
assert integration.requirements == ["test-req==1.0.0"]
|
2019-06-20 20:22:12 +00:00
|
|
|
assert integration.is_built_in is True
|
|
|
|
|
|
|
|
integration = loader.Integration(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass,
|
|
|
|
"custom_components.hue",
|
|
|
|
None,
|
|
|
|
{
|
|
|
|
"name": "Philips Hue",
|
|
|
|
"domain": "hue",
|
|
|
|
"dependencies": ["test-dep"],
|
|
|
|
"requirements": ["test-req==1.0.0"],
|
|
|
|
},
|
|
|
|
)
|
2019-06-20 20:22:12 +00:00
|
|
|
assert integration.is_built_in is False
|
2019-04-16 03:38:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_integrations_only_once(hass):
|
|
|
|
"""Test that we load integrations only once."""
|
2019-07-31 19:25:30 +00:00
|
|
|
int_1 = hass.async_create_task(loader.async_get_integration(hass, "hue"))
|
|
|
|
int_2 = hass.async_create_task(loader.async_get_integration(hass, "hue"))
|
2019-04-16 03:38:24 +00:00
|
|
|
|
|
|
|
assert await int_1 is await int_2
|
2019-07-08 23:19:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_get_custom_components_internal(hass):
|
|
|
|
"""Test that we can a list of custom components."""
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
integrations = await loader._async_get_custom_components(hass)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert integrations == {"test": ANY, "test_package": ANY}
|
2019-07-08 23:19:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _get_test_integration(hass, name, config_flow):
|
|
|
|
"""Return a generated test integration."""
|
|
|
|
return loader.Integration(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass,
|
2020-01-03 13:47:06 +00:00
|
|
|
f"homeassistant.components.{name}",
|
2019-07-31 19:25:30 +00:00
|
|
|
None,
|
|
|
|
{
|
|
|
|
"name": name,
|
|
|
|
"domain": name,
|
|
|
|
"config_flow": config_flow,
|
|
|
|
"dependencies": [],
|
|
|
|
"requirements": [],
|
|
|
|
},
|
|
|
|
)
|
2019-07-08 23:19:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_get_custom_components(hass):
|
|
|
|
"""Verify that custom components are cached."""
|
2019-07-31 19:25:30 +00:00
|
|
|
test_1_integration = _get_test_integration(hass, "test_1", False)
|
|
|
|
test_2_integration = _get_test_integration(hass, "test_2", True)
|
2019-07-08 23:19:37 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
name = "homeassistant.loader._async_get_custom_components"
|
2019-07-08 23:19:37 +00:00
|
|
|
with patch(name) as mock_get:
|
|
|
|
mock_get.return_value = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"test_1": test_1_integration,
|
|
|
|
"test_2": test_2_integration,
|
2019-07-08 23:19:37 +00:00
|
|
|
}
|
|
|
|
integrations = await loader.async_get_custom_components(hass)
|
|
|
|
assert integrations == mock_get.return_value
|
|
|
|
integrations = await loader.async_get_custom_components(hass)
|
|
|
|
assert integrations == mock_get.return_value
|
|
|
|
mock_get.assert_called_once_with(hass)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_get_config_flows(hass):
|
|
|
|
"""Verify that custom components with config_flow are available."""
|
2019-07-31 19:25:30 +00:00
|
|
|
test_1_integration = _get_test_integration(hass, "test_1", False)
|
|
|
|
test_2_integration = _get_test_integration(hass, "test_2", True)
|
2019-07-08 23:19:37 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.loader.async_get_custom_components") as mock_get:
|
2019-07-08 23:19:37 +00:00
|
|
|
mock_get.return_value = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"test_1": test_1_integration,
|
|
|
|
"test_2": test_2_integration,
|
2019-07-08 23:19:37 +00:00
|
|
|
}
|
|
|
|
flows = await loader.async_get_config_flows(hass)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "test_2" in flows
|
|
|
|
assert "test_1" not in flows
|
2020-02-18 19:52:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_get_custom_components_safe_mode(hass):
|
|
|
|
"""Test that we get empty custom components in safe mode."""
|
|
|
|
hass.config.safe_mode = True
|
|
|
|
assert await loader.async_get_custom_components(hass) == {}
|