2016-03-09 10:25:50 +01:00
|
|
|
"""Test to verify that we can load components."""
|
2021-05-17 15:48:41 +02:00
|
|
|
from unittest.mock import patch
|
2021-01-01 22:31:56 +01:00
|
|
|
|
2017-07-16 18:23:06 +02:00
|
|
|
import pytest
|
|
|
|
|
2020-11-27 17:48:43 +01:00
|
|
|
from homeassistant import core, loader
|
2019-04-09 18:30:32 +02:00
|
|
|
from homeassistant.components import http, hue
|
|
|
|
from homeassistant.components.hue import light as hue_light
|
2014-11-23 18:51:16 +01:00
|
|
|
|
2021-10-07 12:58:00 +02:00
|
|
|
from tests.common import MockModule, mock_integration
|
2014-11-25 09:20:36 +01:00
|
|
|
|
2014-11-23 18:51:16 +01:00
|
|
|
|
2019-04-09 18:30:32 +02:00
|
|
|
async def test_component_dependencies(hass):
|
2019-02-07 22:56:40 +01:00
|
|
|
"""Test if we can get the proper load order of components."""
|
2019-07-31 21:25:30 +02:00
|
|
|
mock_integration(hass, MockModule("mod1"))
|
|
|
|
mock_integration(hass, MockModule("mod2", ["mod1"]))
|
2020-06-20 02:24:33 +02:00
|
|
|
mod_3 = mock_integration(hass, MockModule("mod3", ["mod2"]))
|
2014-11-29 00:34:42 +01:00
|
|
|
|
2020-06-20 02:24:33 +02:00
|
|
|
assert {"mod1", "mod2", "mod3"} == await loader._async_component_dependencies(
|
|
|
|
hass, "mod_3", mod_3, set(), set()
|
2019-07-31 21:25:30 +02:00
|
|
|
)
|
2014-11-29 00:34:42 +01:00
|
|
|
|
2019-02-07 22:56:40 +01:00
|
|
|
# Create circular dependency
|
2019-07-31 21:25:30 +02:00
|
|
|
mock_integration(hass, MockModule("mod1", ["mod3"]))
|
2014-11-29 00:34:42 +01:00
|
|
|
|
2019-02-07 22:56:40 +01:00
|
|
|
with pytest.raises(loader.CircularDependency):
|
2020-06-20 02:24:33 +02:00
|
|
|
print(
|
|
|
|
await loader._async_component_dependencies(
|
|
|
|
hass, "mod_3", mod_3, set(), set()
|
|
|
|
)
|
|
|
|
)
|
2014-11-29 00:34:42 +01:00
|
|
|
|
2019-02-07 22:56:40 +01:00
|
|
|
# Depend on non-existing component
|
2020-06-20 02:24:33 +02:00
|
|
|
mod_1 = mock_integration(hass, MockModule("mod1", ["nonexisting"]))
|
2014-11-29 00:34:42 +01:00
|
|
|
|
2019-04-09 18:30:32 +02:00
|
|
|
with pytest.raises(loader.IntegrationNotFound):
|
2020-06-20 02:24:33 +02: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-29 00:34:42 +01:00
|
|
|
|
2020-06-20 02:24:33 +02:00
|
|
|
with pytest.raises(loader.CircularDependency):
|
|
|
|
print(
|
|
|
|
await loader._async_component_dependencies(
|
|
|
|
hass, "mod_3", mod_3, set(), set()
|
|
|
|
)
|
|
|
|
)
|
2017-07-16 18:23:06 +02: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 10:26:36 +02:00
|
|
|
async def test_component_wrapper(hass):
|
2017-07-16 18:23:06 +02:00
|
|
|
"""Test component wrapper."""
|
|
|
|
components = loader.Components(hass)
|
2019-07-31 21:25:30 +02:00
|
|
|
components.persistent_notification.async_create("message")
|
2017-07-16 18:23:06 +02:00
|
|
|
|
2021-10-07 12:58:00 +02:00
|
|
|
assert len(hass.states.async_entity_ids("persistent_notification")) == 1
|
2017-10-08 17:17:54 +02:00
|
|
|
|
|
|
|
|
2019-04-11 10:26:36 +02:00
|
|
|
async def test_helpers_wrapper(hass):
|
2017-10-08 17:17:54 +02:00
|
|
|
"""Test helpers wrapper."""
|
|
|
|
helpers = loader.Helpers(hass)
|
|
|
|
|
|
|
|
result = []
|
|
|
|
|
2020-11-27 17:48:43 +01:00
|
|
|
@core.callback
|
2017-10-08 17:17:54 +02:00
|
|
|
def discovery_callback(service, discovered):
|
|
|
|
"""Handle discovery callback."""
|
|
|
|
result.append(discovered)
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
helpers.discovery.async_listen("service_name", discovery_callback)
|
2017-10-08 17:17:54 +02:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
await helpers.discovery.async_discover("service_name", "hello", None, {})
|
2019-04-11 10:26:36 +02:00
|
|
|
await hass.async_block_till_done()
|
2017-10-08 17:17:54 +02:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
assert result == ["hello"]
|
2018-05-07 02:54:56 +02:00
|
|
|
|
|
|
|
|
2021-05-17 15:48:41 +02:00
|
|
|
async def test_custom_component_name(hass, enable_custom_integrations):
|
2020-12-31 19:14:07 +01:00
|
|
|
"""Test the name attribute of custom components."""
|
2021-05-17 15:48:41 +02:00
|
|
|
with pytest.raises(loader.IntegrationNotFound):
|
|
|
|
await loader.async_get_integration(hass, "test_standalone")
|
2018-05-07 02:54:56 +02:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
integration = await loader.async_get_integration(hass, "test_package")
|
2021-05-17 15:48:41 +02:00
|
|
|
|
2019-04-15 07:31:01 +02:00
|
|
|
int_comp = integration.get_component()
|
2019-07-31 21:25:30 +02:00
|
|
|
assert int_comp.__name__ == "custom_components.test_package"
|
|
|
|
assert int_comp.__package__ == "custom_components.test_package"
|
2019-04-15 07:31:01 +02:00
|
|
|
|
|
|
|
comp = hass.components.test_package
|
2019-07-31 21:25:30 +02:00
|
|
|
assert comp.__name__ == "custom_components.test_package"
|
|
|
|
assert comp.__package__ == "custom_components.test_package"
|
2018-05-07 02:54:56 +02:00
|
|
|
|
2019-07-31 21:25:30 +02: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 11:25:48 +02:00
|
|
|
|
|
|
|
# Test custom components is mounted
|
|
|
|
from custom_components.test_package import TEST
|
2019-07-31 21:25:30 +02:00
|
|
|
|
2018-05-07 11:25:48 +02:00
|
|
|
assert TEST == 5
|
2018-06-27 21:21:32 +02:00
|
|
|
|
|
|
|
|
2021-05-17 15:48:41 +02:00
|
|
|
async def test_log_warning_custom_component(hass, caplog, enable_custom_integrations):
|
2018-06-27 21:21:32 +02:00
|
|
|
"""Test that we log a warning when loading a custom component."""
|
2021-05-17 15:48:41 +02:00
|
|
|
|
|
|
|
await loader.async_get_integration(hass, "test_package")
|
2021-06-01 00:32:03 +02:00
|
|
|
assert "We found a custom integration test_package" in caplog.text
|
2018-06-27 21:21:32 +02:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
await loader.async_get_integration(hass, "test")
|
2021-06-01 00:32:03 +02:00
|
|
|
assert "We found a custom integration test " in caplog.text
|
2019-02-07 22:33:12 +01:00
|
|
|
|
|
|
|
|
2021-06-01 00:32:03 +02:00
|
|
|
async def test_custom_integration_version_not_valid(
|
|
|
|
hass, caplog, enable_custom_integrations
|
|
|
|
):
|
2021-05-17 15:48:41 +02:00
|
|
|
"""Test that we log a warning when custom integrations have a invalid version."""
|
2021-06-01 00:32:03 +02:00
|
|
|
with pytest.raises(loader.IntegrationNotFound):
|
|
|
|
await loader.async_get_integration(hass, "test_no_version")
|
2021-02-04 09:59:41 +01:00
|
|
|
|
2021-06-01 00:32:03 +02:00
|
|
|
assert (
|
2021-09-11 20:40:46 +02:00
|
|
|
"The custom integration 'test_no_version' does not have a version key in the manifest file and was blocked from loading."
|
2021-06-01 00:32:03 +02:00
|
|
|
in caplog.text
|
|
|
|
)
|
2021-02-04 09:59:41 +01:00
|
|
|
|
2021-06-01 00:32:03 +02:00
|
|
|
with pytest.raises(loader.IntegrationNotFound):
|
|
|
|
await loader.async_get_integration(hass, "test2")
|
|
|
|
assert (
|
|
|
|
"The custom integration 'test_bad_version' does not have a valid version key (bad) in the manifest file and was blocked from loading."
|
|
|
|
in caplog.text
|
|
|
|
)
|
2021-02-04 09:59:41 +01:00
|
|
|
|
|
|
|
|
2019-04-09 18:30:32 +02:00
|
|
|
async def test_get_integration(hass):
|
|
|
|
"""Test resolving integration."""
|
2019-07-31 21:25:30 +02:00
|
|
|
integration = await loader.async_get_integration(hass, "hue")
|
2019-04-09 18:30:32 +02:00
|
|
|
assert hue == integration.get_component()
|
2019-07-31 21:25:30 +02:00
|
|
|
assert hue_light == integration.get_platform("light")
|
2019-04-09 18:30:32 +02:00
|
|
|
|
|
|
|
|
2022-01-26 05:39:32 +01:00
|
|
|
async def test_get_integration_exceptions(hass):
|
|
|
|
"""Test resolving integration."""
|
|
|
|
integration = await loader.async_get_integration(hass, "hue")
|
|
|
|
|
|
|
|
with pytest.raises(ImportError), patch(
|
|
|
|
"homeassistant.loader.importlib.import_module", side_effect=ValueError("Boom")
|
|
|
|
):
|
|
|
|
assert hue == integration.get_component()
|
|
|
|
|
|
|
|
with pytest.raises(ImportError), patch(
|
|
|
|
"homeassistant.loader.importlib.import_module", side_effect=ValueError("Boom")
|
|
|
|
):
|
|
|
|
assert hue_light == integration.get_platform("light")
|
|
|
|
|
|
|
|
|
2021-05-17 15:48:41 +02:00
|
|
|
async def test_get_integration_legacy(hass, enable_custom_integrations):
|
2019-04-09 18:30:32 +02:00
|
|
|
"""Test resolving integration."""
|
2019-07-31 21:25:30 +02: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 18:30:32 +02:00
|
|
|
|
|
|
|
|
2020-11-27 12:53:16 +01:00
|
|
|
async def test_get_integration_custom_component(hass, enable_custom_integrations):
|
2019-04-09 18:30:32 +02:00
|
|
|
"""Test resolving integration."""
|
2019-07-31 21:25:30 +02:00
|
|
|
integration = await loader.async_get_integration(hass, "test_package")
|
|
|
|
assert integration.get_component().DOMAIN == "test_package"
|
|
|
|
assert integration.name == "Test Package"
|
2019-04-09 18:30:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_integration_properties(hass):
|
|
|
|
"""Test integration properties."""
|
2019-04-12 19:09:17 +02:00
|
|
|
integration = loader.Integration(
|
2019-07-31 21:25:30 +02:00
|
|
|
hass,
|
|
|
|
"homeassistant.components.hue",
|
|
|
|
None,
|
|
|
|
{
|
|
|
|
"name": "Philips Hue",
|
|
|
|
"domain": "hue",
|
|
|
|
"dependencies": ["test-dep"],
|
|
|
|
"requirements": ["test-req==1.0.0"],
|
2020-08-05 15:50:56 +02:00
|
|
|
"zeroconf": ["_hue._tcp.local."],
|
|
|
|
"homekit": {"models": ["BSB002"]},
|
2021-01-14 09:09:08 +01:00
|
|
|
"dhcp": [
|
|
|
|
{"hostname": "tesla_*", "macaddress": "4CFCAA*"},
|
|
|
|
{"hostname": "tesla_*", "macaddress": "044EAF*"},
|
|
|
|
{"hostname": "tesla_*", "macaddress": "98ED5C*"},
|
2022-02-15 18:02:52 +01:00
|
|
|
{"registered_devices": True},
|
2021-01-14 09:09:08 +01:00
|
|
|
],
|
2022-07-18 00:25:45 +02:00
|
|
|
"bluetooth": [{"manufacturer_id": 76, "manufacturer_data_start": [0x06]}],
|
2021-08-20 21:04:18 +02:00
|
|
|
"usb": [
|
|
|
|
{"vid": "10C4", "pid": "EA60"},
|
|
|
|
{"vid": "1CF1", "pid": "0030"},
|
|
|
|
{"vid": "1A86", "pid": "7523"},
|
|
|
|
{"vid": "10C4", "pid": "8A2A"},
|
|
|
|
],
|
2020-08-05 15:50:56 +02:00
|
|
|
"ssdp": [
|
|
|
|
{
|
|
|
|
"manufacturer": "Royal Philips Electronics",
|
|
|
|
"modelName": "Philips hue bridge 2012",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"manufacturer": "Royal Philips Electronics",
|
|
|
|
"modelName": "Philips hue bridge 2015",
|
|
|
|
},
|
|
|
|
{"manufacturer": "Signify", "modelName": "Philips hue bridge 2015"},
|
|
|
|
],
|
2020-10-07 18:30:51 +02:00
|
|
|
"mqtt": ["hue/discovery"],
|
2021-02-04 09:59:41 +01:00
|
|
|
"version": "1.0.0",
|
2019-07-31 21:25:30 +02:00
|
|
|
},
|
|
|
|
)
|
2019-04-09 18:30:32 +02:00
|
|
|
assert integration.name == "Philips Hue"
|
2019-07-31 21:25:30 +02:00
|
|
|
assert integration.domain == "hue"
|
2020-08-05 15:50:56 +02:00
|
|
|
assert integration.homekit == {"models": ["BSB002"]}
|
|
|
|
assert integration.zeroconf == ["_hue._tcp.local."]
|
2021-01-14 09:09:08 +01:00
|
|
|
assert integration.dhcp == [
|
|
|
|
{"hostname": "tesla_*", "macaddress": "4CFCAA*"},
|
|
|
|
{"hostname": "tesla_*", "macaddress": "044EAF*"},
|
|
|
|
{"hostname": "tesla_*", "macaddress": "98ED5C*"},
|
2022-02-15 18:02:52 +01:00
|
|
|
{"registered_devices": True},
|
2021-01-14 09:09:08 +01:00
|
|
|
]
|
2021-08-20 21:04:18 +02:00
|
|
|
assert integration.usb == [
|
|
|
|
{"vid": "10C4", "pid": "EA60"},
|
|
|
|
{"vid": "1CF1", "pid": "0030"},
|
|
|
|
{"vid": "1A86", "pid": "7523"},
|
|
|
|
{"vid": "10C4", "pid": "8A2A"},
|
|
|
|
]
|
2022-07-09 01:55:31 +02:00
|
|
|
assert integration.bluetooth == [
|
2022-07-18 00:25:45 +02:00
|
|
|
{"manufacturer_id": 76, "manufacturer_data_start": [0x06]}
|
2022-07-09 01:55:31 +02:00
|
|
|
]
|
2020-08-05 15:50:56 +02:00
|
|
|
assert integration.ssdp == [
|
|
|
|
{
|
|
|
|
"manufacturer": "Royal Philips Electronics",
|
|
|
|
"modelName": "Philips hue bridge 2012",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"manufacturer": "Royal Philips Electronics",
|
|
|
|
"modelName": "Philips hue bridge 2015",
|
|
|
|
},
|
|
|
|
{"manufacturer": "Signify", "modelName": "Philips hue bridge 2015"},
|
|
|
|
]
|
2020-10-07 18:30:51 +02:00
|
|
|
assert integration.mqtt == ["hue/discovery"]
|
2019-07-31 21:25:30 +02:00
|
|
|
assert integration.dependencies == ["test-dep"]
|
|
|
|
assert integration.requirements == ["test-req==1.0.0"]
|
2019-06-20 22:22:12 +02:00
|
|
|
assert integration.is_built_in is True
|
2021-02-04 09:59:41 +01:00
|
|
|
assert integration.version == "1.0.0"
|
2019-06-20 22:22:12 +02:00
|
|
|
|
|
|
|
integration = loader.Integration(
|
2019-07-31 21:25:30 +02:00
|
|
|
hass,
|
|
|
|
"custom_components.hue",
|
|
|
|
None,
|
|
|
|
{
|
|
|
|
"name": "Philips Hue",
|
|
|
|
"domain": "hue",
|
|
|
|
"dependencies": ["test-dep"],
|
|
|
|
"requirements": ["test-req==1.0.0"],
|
|
|
|
},
|
|
|
|
)
|
2019-06-20 22:22:12 +02:00
|
|
|
assert integration.is_built_in is False
|
2020-08-05 15:50:56 +02:00
|
|
|
assert integration.homekit is None
|
|
|
|
assert integration.zeroconf is None
|
2021-01-14 09:09:08 +01:00
|
|
|
assert integration.dhcp is None
|
2022-07-09 01:55:31 +02:00
|
|
|
assert integration.bluetooth is None
|
2021-08-20 21:04:18 +02:00
|
|
|
assert integration.usb is None
|
2020-08-05 15:50:56 +02:00
|
|
|
assert integration.ssdp is None
|
2020-10-07 18:30:51 +02:00
|
|
|
assert integration.mqtt is None
|
2021-02-04 09:59:41 +01:00
|
|
|
assert integration.version is None
|
2019-04-16 05:38:24 +02:00
|
|
|
|
2020-09-11 12:19:21 +02:00
|
|
|
integration = loader.Integration(
|
|
|
|
hass,
|
|
|
|
"custom_components.hue",
|
|
|
|
None,
|
|
|
|
{
|
|
|
|
"name": "Philips Hue",
|
|
|
|
"domain": "hue",
|
|
|
|
"dependencies": ["test-dep"],
|
|
|
|
"zeroconf": [{"type": "_hue._tcp.local.", "name": "hue*"}],
|
|
|
|
"requirements": ["test-req==1.0.0"],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert integration.is_built_in is False
|
|
|
|
assert integration.homekit is None
|
|
|
|
assert integration.zeroconf == [{"type": "_hue._tcp.local.", "name": "hue*"}]
|
2021-01-14 09:09:08 +01:00
|
|
|
assert integration.dhcp is None
|
2021-08-20 21:04:18 +02:00
|
|
|
assert integration.usb is None
|
2022-07-09 01:55:31 +02:00
|
|
|
assert integration.bluetooth is None
|
2020-09-11 12:19:21 +02:00
|
|
|
assert integration.ssdp is None
|
|
|
|
|
2019-04-16 05:38:24 +02:00
|
|
|
|
|
|
|
async def test_integrations_only_once(hass):
|
|
|
|
"""Test that we load integrations only once."""
|
2019-07-31 21:25:30 +02: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 05:38:24 +02:00
|
|
|
|
|
|
|
assert await int_1 is await int_2
|
2019-07-09 01:19:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
def _get_test_integration(hass, name, config_flow):
|
|
|
|
"""Return a generated test integration."""
|
|
|
|
return loader.Integration(
|
2019-07-31 21:25:30 +02:00
|
|
|
hass,
|
2020-01-03 14:47:06 +01:00
|
|
|
f"homeassistant.components.{name}",
|
2019-07-31 21:25:30 +02:00
|
|
|
None,
|
|
|
|
{
|
|
|
|
"name": name,
|
|
|
|
"domain": name,
|
|
|
|
"config_flow": config_flow,
|
|
|
|
"dependencies": [],
|
|
|
|
"requirements": [],
|
2020-08-05 15:50:56 +02:00
|
|
|
"zeroconf": [f"_{name}._tcp.local."],
|
|
|
|
"homekit": {"models": [name]},
|
|
|
|
"ssdp": [{"manufacturer": name, "modelName": name}],
|
2020-10-07 18:30:51 +02:00
|
|
|
"mqtt": [f"{name}/discovery"],
|
2019-07-31 21:25:30 +02:00
|
|
|
},
|
|
|
|
)
|
2019-07-09 01:19:37 +02:00
|
|
|
|
|
|
|
|
2022-05-02 01:26:22 +02:00
|
|
|
def _get_test_integration_with_application_credentials(hass, name):
|
|
|
|
"""Return a generated test integration with application_credentials support."""
|
|
|
|
return loader.Integration(
|
|
|
|
hass,
|
|
|
|
f"homeassistant.components.{name}",
|
|
|
|
None,
|
|
|
|
{
|
|
|
|
"name": name,
|
|
|
|
"domain": name,
|
|
|
|
"config_flow": True,
|
|
|
|
"dependencies": ["application_credentials"],
|
|
|
|
"requirements": [],
|
|
|
|
"zeroconf": [f"_{name}._tcp.local."],
|
|
|
|
"homekit": {"models": [name]},
|
|
|
|
"ssdp": [{"manufacturer": name, "modelName": name}],
|
|
|
|
"mqtt": [f"{name}/discovery"],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-09-11 12:19:21 +02:00
|
|
|
def _get_test_integration_with_zeroconf_matcher(hass, name, config_flow):
|
|
|
|
"""Return a generated test integration with a zeroconf matcher."""
|
|
|
|
return loader.Integration(
|
|
|
|
hass,
|
|
|
|
f"homeassistant.components.{name}",
|
|
|
|
None,
|
|
|
|
{
|
|
|
|
"name": name,
|
|
|
|
"domain": name,
|
|
|
|
"config_flow": config_flow,
|
|
|
|
"dependencies": [],
|
|
|
|
"requirements": [],
|
|
|
|
"zeroconf": [{"type": f"_{name}._tcp.local.", "name": f"{name}*"}],
|
|
|
|
"homekit": {"models": [name]},
|
|
|
|
"ssdp": [{"manufacturer": name, "modelName": name}],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-12-19 09:09:21 +01:00
|
|
|
def _get_test_integration_with_legacy_zeroconf_matcher(hass, name, config_flow):
|
|
|
|
"""Return a generated test integration with a legacy zeroconf matcher."""
|
|
|
|
return loader.Integration(
|
|
|
|
hass,
|
|
|
|
f"homeassistant.components.{name}",
|
|
|
|
None,
|
|
|
|
{
|
|
|
|
"name": name,
|
|
|
|
"domain": name,
|
|
|
|
"config_flow": config_flow,
|
|
|
|
"dependencies": [],
|
|
|
|
"requirements": [],
|
|
|
|
"zeroconf": [
|
|
|
|
{
|
|
|
|
"type": f"_{name}._tcp.local.",
|
|
|
|
"macaddress": "AABBCC*",
|
|
|
|
"manufacturer": "legacy*",
|
|
|
|
"model": "legacy*",
|
|
|
|
"name": f"{name}*",
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"homekit": {"models": [name]},
|
|
|
|
"ssdp": [{"manufacturer": name, "modelName": name}],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-01-14 09:09:08 +01:00
|
|
|
def _get_test_integration_with_dhcp_matcher(hass, name, config_flow):
|
|
|
|
"""Return a generated test integration with a dhcp matcher."""
|
|
|
|
return loader.Integration(
|
|
|
|
hass,
|
|
|
|
f"homeassistant.components.{name}",
|
|
|
|
None,
|
|
|
|
{
|
|
|
|
"name": name,
|
|
|
|
"domain": name,
|
|
|
|
"config_flow": config_flow,
|
|
|
|
"dependencies": [],
|
|
|
|
"requirements": [],
|
|
|
|
"zeroconf": [],
|
|
|
|
"dhcp": [
|
|
|
|
{"hostname": "tesla_*", "macaddress": "4CFCAA*"},
|
|
|
|
{"hostname": "tesla_*", "macaddress": "044EAF*"},
|
|
|
|
{"hostname": "tesla_*", "macaddress": "98ED5C*"},
|
|
|
|
],
|
|
|
|
"homekit": {"models": [name]},
|
|
|
|
"ssdp": [{"manufacturer": name, "modelName": name}],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-07-09 01:55:31 +02:00
|
|
|
def _get_test_integration_with_bluetooth_matcher(hass, name, config_flow):
|
|
|
|
"""Return a generated test integration with a bluetooth matcher."""
|
|
|
|
return loader.Integration(
|
|
|
|
hass,
|
|
|
|
f"homeassistant.components.{name}",
|
|
|
|
None,
|
|
|
|
{
|
|
|
|
"name": name,
|
|
|
|
"domain": name,
|
|
|
|
"config_flow": config_flow,
|
|
|
|
"bluetooth": [
|
|
|
|
{
|
|
|
|
"local_name": "Prodigio_*",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-20 21:04:18 +02:00
|
|
|
def _get_test_integration_with_usb_matcher(hass, name, config_flow):
|
|
|
|
"""Return a generated test integration with a usb matcher."""
|
|
|
|
return loader.Integration(
|
|
|
|
hass,
|
|
|
|
f"homeassistant.components.{name}",
|
|
|
|
None,
|
|
|
|
{
|
|
|
|
"name": name,
|
|
|
|
"domain": name,
|
|
|
|
"config_flow": config_flow,
|
|
|
|
"dependencies": [],
|
|
|
|
"requirements": [],
|
|
|
|
"usb": [
|
2021-08-21 21:56:49 +02:00
|
|
|
{
|
|
|
|
"vid": "10C4",
|
|
|
|
"pid": "EA60",
|
|
|
|
"known_devices": ["slae.sh cc2652rb stick"],
|
|
|
|
},
|
|
|
|
{"vid": "1CF1", "pid": "0030", "known_devices": ["Conbee II"]},
|
|
|
|
{
|
|
|
|
"vid": "1A86",
|
|
|
|
"pid": "7523",
|
|
|
|
"known_devices": ["Electrolama zig-a-zig-ah"],
|
|
|
|
},
|
|
|
|
{"vid": "10C4", "pid": "8A2A", "known_devices": ["Nortek HUSBZB-1"]},
|
2021-08-20 21:04:18 +02:00
|
|
|
],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-11-27 12:53:16 +01:00
|
|
|
async def test_get_custom_components(hass, enable_custom_integrations):
|
2019-07-09 01:19:37 +02:00
|
|
|
"""Verify that custom components are cached."""
|
2019-07-31 21:25:30 +02:00
|
|
|
test_1_integration = _get_test_integration(hass, "test_1", False)
|
|
|
|
test_2_integration = _get_test_integration(hass, "test_2", True)
|
2019-07-09 01:19:37 +02:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
name = "homeassistant.loader._async_get_custom_components"
|
2019-07-09 01:19:37 +02:00
|
|
|
with patch(name) as mock_get:
|
|
|
|
mock_get.return_value = {
|
2019-07-31 21:25:30 +02:00
|
|
|
"test_1": test_1_integration,
|
|
|
|
"test_2": test_2_integration,
|
2019-07-09 01:19:37 +02: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 21:25:30 +02:00
|
|
|
test_1_integration = _get_test_integration(hass, "test_1", False)
|
|
|
|
test_2_integration = _get_test_integration(hass, "test_2", True)
|
2019-07-09 01:19:37 +02:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
with patch("homeassistant.loader.async_get_custom_components") as mock_get:
|
2019-07-09 01:19:37 +02:00
|
|
|
mock_get.return_value = {
|
2019-07-31 21:25:30 +02:00
|
|
|
"test_1": test_1_integration,
|
|
|
|
"test_2": test_2_integration,
|
2019-07-09 01:19:37 +02:00
|
|
|
}
|
|
|
|
flows = await loader.async_get_config_flows(hass)
|
2019-07-31 21:25:30 +02:00
|
|
|
assert "test_2" in flows
|
|
|
|
assert "test_1" not in flows
|
2020-02-18 20:52:38 +01:00
|
|
|
|
|
|
|
|
2020-08-05 15:50:56 +02:00
|
|
|
async def test_get_zeroconf(hass):
|
|
|
|
"""Verify that custom components with zeroconf are found."""
|
|
|
|
test_1_integration = _get_test_integration(hass, "test_1", True)
|
2020-09-11 12:19:21 +02:00
|
|
|
test_2_integration = _get_test_integration_with_zeroconf_matcher(
|
|
|
|
hass, "test_2", True
|
|
|
|
)
|
2020-08-05 15:50:56 +02:00
|
|
|
|
|
|
|
with patch("homeassistant.loader.async_get_custom_components") as mock_get:
|
|
|
|
mock_get.return_value = {
|
|
|
|
"test_1": test_1_integration,
|
|
|
|
"test_2": test_2_integration,
|
|
|
|
}
|
|
|
|
zeroconf = await loader.async_get_zeroconf(hass)
|
2020-09-11 12:19:21 +02:00
|
|
|
assert zeroconf["_test_1._tcp.local."] == [{"domain": "test_1"}]
|
|
|
|
assert zeroconf["_test_2._tcp.local."] == [
|
|
|
|
{"domain": "test_2", "name": "test_2*"}
|
|
|
|
]
|
2020-08-05 15:50:56 +02:00
|
|
|
|
|
|
|
|
2022-05-02 01:26:22 +02:00
|
|
|
async def test_get_application_credentials(hass):
|
|
|
|
"""Verify that custom components with application_credentials are found."""
|
|
|
|
test_1_integration = _get_test_integration(hass, "test_1", True)
|
|
|
|
test_2_integration = _get_test_integration_with_application_credentials(
|
|
|
|
hass, "test_2"
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch("homeassistant.loader.async_get_custom_components") as mock_get:
|
|
|
|
mock_get.return_value = {
|
|
|
|
"test_1": test_1_integration,
|
|
|
|
"test_2": test_2_integration,
|
|
|
|
}
|
|
|
|
application_credentials = await loader.async_get_application_credentials(hass)
|
|
|
|
assert "test_2" in application_credentials
|
|
|
|
assert "test_1" not in application_credentials
|
|
|
|
|
|
|
|
|
2021-12-19 09:09:21 +01:00
|
|
|
async def test_get_zeroconf_back_compat(hass):
|
|
|
|
"""Verify that custom components with zeroconf are found and legacy matchers are converted."""
|
|
|
|
test_1_integration = _get_test_integration(hass, "test_1", True)
|
|
|
|
test_2_integration = _get_test_integration_with_legacy_zeroconf_matcher(
|
|
|
|
hass, "test_2", True
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch("homeassistant.loader.async_get_custom_components") as mock_get:
|
|
|
|
mock_get.return_value = {
|
|
|
|
"test_1": test_1_integration,
|
|
|
|
"test_2": test_2_integration,
|
|
|
|
}
|
|
|
|
zeroconf = await loader.async_get_zeroconf(hass)
|
|
|
|
assert zeroconf["_test_1._tcp.local."] == [{"domain": "test_1"}]
|
|
|
|
assert zeroconf["_test_2._tcp.local."] == [
|
|
|
|
{
|
|
|
|
"domain": "test_2",
|
|
|
|
"name": "test_2*",
|
|
|
|
"properties": {
|
|
|
|
"macaddress": "aabbcc*",
|
|
|
|
"model": "legacy*",
|
|
|
|
"manufacturer": "legacy*",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-07-09 01:55:31 +02:00
|
|
|
async def test_get_bluetooth(hass):
|
|
|
|
"""Verify that custom components with bluetooth are found."""
|
|
|
|
test_1_integration = _get_test_integration_with_bluetooth_matcher(
|
|
|
|
hass, "test_1", True
|
|
|
|
)
|
|
|
|
test_2_integration = _get_test_integration_with_dhcp_matcher(hass, "test_2", True)
|
|
|
|
with patch("homeassistant.loader.async_get_custom_components") as mock_get:
|
|
|
|
mock_get.return_value = {
|
|
|
|
"test_1": test_1_integration,
|
|
|
|
"test_2": test_2_integration,
|
|
|
|
}
|
|
|
|
bluetooth = await loader.async_get_bluetooth(hass)
|
|
|
|
bluetooth_for_domain = [
|
|
|
|
entry for entry in bluetooth if entry["domain"] == "test_1"
|
|
|
|
]
|
|
|
|
assert bluetooth_for_domain == [
|
|
|
|
{"domain": "test_1", "local_name": "Prodigio_*"},
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-01-14 09:09:08 +01:00
|
|
|
async def test_get_dhcp(hass):
|
|
|
|
"""Verify that custom components with dhcp are found."""
|
|
|
|
test_1_integration = _get_test_integration_with_dhcp_matcher(hass, "test_1", True)
|
|
|
|
|
|
|
|
with patch("homeassistant.loader.async_get_custom_components") as mock_get:
|
|
|
|
mock_get.return_value = {
|
|
|
|
"test_1": test_1_integration,
|
|
|
|
}
|
|
|
|
dhcp = await loader.async_get_dhcp(hass)
|
|
|
|
dhcp_for_domain = [entry for entry in dhcp if entry["domain"] == "test_1"]
|
|
|
|
assert dhcp_for_domain == [
|
|
|
|
{"domain": "test_1", "hostname": "tesla_*", "macaddress": "4CFCAA*"},
|
|
|
|
{"domain": "test_1", "hostname": "tesla_*", "macaddress": "044EAF*"},
|
|
|
|
{"domain": "test_1", "hostname": "tesla_*", "macaddress": "98ED5C*"},
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-08-20 21:04:18 +02:00
|
|
|
async def test_get_usb(hass):
|
|
|
|
"""Verify that custom components with usb matchers are found."""
|
|
|
|
test_1_integration = _get_test_integration_with_usb_matcher(hass, "test_1", True)
|
|
|
|
|
|
|
|
with patch("homeassistant.loader.async_get_custom_components") as mock_get:
|
|
|
|
mock_get.return_value = {
|
|
|
|
"test_1": test_1_integration,
|
|
|
|
}
|
|
|
|
usb = await loader.async_get_usb(hass)
|
|
|
|
usb_for_domain = [entry for entry in usb if entry["domain"] == "test_1"]
|
|
|
|
assert usb_for_domain == [
|
|
|
|
{"domain": "test_1", "vid": "10C4", "pid": "EA60"},
|
|
|
|
{"domain": "test_1", "vid": "1CF1", "pid": "0030"},
|
|
|
|
{"domain": "test_1", "vid": "1A86", "pid": "7523"},
|
|
|
|
{"domain": "test_1", "vid": "10C4", "pid": "8A2A"},
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-08-05 15:50:56 +02:00
|
|
|
async def test_get_homekit(hass):
|
|
|
|
"""Verify that custom components with homekit are found."""
|
|
|
|
test_1_integration = _get_test_integration(hass, "test_1", True)
|
|
|
|
test_2_integration = _get_test_integration(hass, "test_2", True)
|
|
|
|
|
|
|
|
with patch("homeassistant.loader.async_get_custom_components") as mock_get:
|
|
|
|
mock_get.return_value = {
|
|
|
|
"test_1": test_1_integration,
|
|
|
|
"test_2": test_2_integration,
|
|
|
|
}
|
|
|
|
homekit = await loader.async_get_homekit(hass)
|
|
|
|
assert homekit["test_1"] == "test_1"
|
|
|
|
assert homekit["test_2"] == "test_2"
|
|
|
|
|
|
|
|
|
|
|
|
async def test_get_ssdp(hass):
|
|
|
|
"""Verify that custom components with ssdp are found."""
|
|
|
|
test_1_integration = _get_test_integration(hass, "test_1", True)
|
|
|
|
test_2_integration = _get_test_integration(hass, "test_2", True)
|
|
|
|
|
|
|
|
with patch("homeassistant.loader.async_get_custom_components") as mock_get:
|
|
|
|
mock_get.return_value = {
|
|
|
|
"test_1": test_1_integration,
|
|
|
|
"test_2": test_2_integration,
|
|
|
|
}
|
|
|
|
ssdp = await loader.async_get_ssdp(hass)
|
|
|
|
assert ssdp["test_1"] == [{"manufacturer": "test_1", "modelName": "test_1"}]
|
|
|
|
assert ssdp["test_2"] == [{"manufacturer": "test_2", "modelName": "test_2"}]
|
|
|
|
|
|
|
|
|
2020-10-07 18:30:51 +02:00
|
|
|
async def test_get_mqtt(hass):
|
|
|
|
"""Verify that custom components with MQTT are found."""
|
|
|
|
test_1_integration = _get_test_integration(hass, "test_1", True)
|
|
|
|
test_2_integration = _get_test_integration(hass, "test_2", True)
|
|
|
|
|
|
|
|
with patch("homeassistant.loader.async_get_custom_components") as mock_get:
|
|
|
|
mock_get.return_value = {
|
|
|
|
"test_1": test_1_integration,
|
|
|
|
"test_2": test_2_integration,
|
|
|
|
}
|
|
|
|
mqtt = await loader.async_get_mqtt(hass)
|
|
|
|
assert mqtt["test_1"] == ["test_1/discovery"]
|
|
|
|
assert mqtt["test_2"] == ["test_2/discovery"]
|
|
|
|
|
|
|
|
|
2020-02-18 20:52:38 +01: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) == {}
|
2021-05-18 00:32:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def test_custom_integration_missing_version(hass, caplog):
|
|
|
|
"""Test trying to load a custom integration without a version twice does not deadlock."""
|
2021-06-01 00:32:03 +02:00
|
|
|
with pytest.raises(loader.IntegrationNotFound):
|
|
|
|
await loader.async_get_integration(hass, "test_no_version")
|
2021-05-18 00:32:05 +02:00
|
|
|
|
2021-06-01 00:32:03 +02:00
|
|
|
with pytest.raises(loader.IntegrationNotFound):
|
|
|
|
await loader.async_get_integration(hass, "test_no_version")
|
2021-05-18 00:32:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def test_custom_integration_missing(hass, caplog):
|
|
|
|
"""Test trying to load a custom integration that is missing twice not deadlock."""
|
|
|
|
with patch("homeassistant.loader.async_get_custom_components") as mock_get:
|
|
|
|
mock_get.return_value = {}
|
|
|
|
|
|
|
|
with pytest.raises(loader.IntegrationNotFound):
|
|
|
|
await loader.async_get_integration(hass, "test1")
|
|
|
|
|
|
|
|
with pytest.raises(loader.IntegrationNotFound):
|
|
|
|
await loader.async_get_integration(hass, "test1")
|
2021-10-26 00:56:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def test_validation(hass):
|
|
|
|
"""Test we raise if invalid domain passed in."""
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
await loader.async_get_integration(hass, "some.thing")
|
2022-01-28 22:37:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def test_loggers(hass):
|
|
|
|
"""Test we can fetch the loggers from the integration."""
|
|
|
|
name = "dummy"
|
|
|
|
integration = loader.Integration(
|
|
|
|
hass,
|
|
|
|
f"homeassistant.components.{name}",
|
|
|
|
None,
|
|
|
|
{
|
|
|
|
"name": name,
|
|
|
|
"domain": name,
|
|
|
|
"config_flow": True,
|
|
|
|
"dependencies": [],
|
|
|
|
"requirements": [],
|
|
|
|
"loggers": ["name1", "name2"],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert integration.loggers == ["name1", "name2"]
|