Import custom components in the executor by default (#112177)

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
J. Nick Koston 2024-03-10 08:41:33 -10:00 committed by GitHub
parent a2318c26c9
commit afa69cca38
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 49 additions and 4 deletions

View File

@ -109,6 +109,12 @@ CUSTOM_WARNING = (
"cause stability problems, be sure to disable it if you "
"experience issues with Home Assistant"
)
IMPORT_EVENT_LOOP_WARNING = (
"We found an integration %s which is configured to "
"to import its code in the event loop. This component might "
"cause stability problems, be sure to disable it if you "
"experience issues with Home Assistant"
)
_UNDEF = object() # Internal; not helpers.typing.UNDEFINED due to circular dependency
@ -653,6 +659,9 @@ class Integration:
None if is_virtual else set(os.listdir(file_path)),
)
if not integration.import_executor:
_LOGGER.warning(IMPORT_EVENT_LOOP_WARNING, integration.domain)
if integration.is_built_in:
return integration
@ -821,9 +830,8 @@ class Integration:
def import_executor(self) -> bool:
"""Import integration in the executor."""
# If the integration does not explicitly set import_executor, we default to
# True if it's a built-in integration and False if it's a custom integration.
# In the future, we want to default to True for all integrations.
return self.manifest.get("import_executor", self.is_built_in)
# True.
return self.manifest.get("import_executor", True)
@cached_property
def has_translations(self) -> bool:

View File

@ -1098,7 +1098,7 @@ async def test_async_suggest_report_issue(
def test_import_executor_default(hass: HomeAssistant) -> None:
"""Test that import_executor defaults."""
custom_comp = mock_integration(hass, MockModule("any_random"), built_in=False)
assert custom_comp.import_executor is False
assert custom_comp.import_executor is True
built_in_comp = mock_integration(hass, MockModule("other_random"), built_in=True)
assert built_in_comp.import_executor is True
@ -1675,3 +1675,13 @@ async def test_async_get_platforms_concurrent_loads(
assert imports == [button_module_name]
assert integration.get_platform_cached("button") is button_module_mock
async def test_integration_warnings(
hass: HomeAssistant,
enable_custom_integrations: None,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test integration warnings."""
await loader.async_get_integration(hass, "test_package_loaded_loop")
assert "configured to to import its code in the event loop" in caplog.text

View File

@ -0,0 +1,7 @@
"""Provide a mock package component."""
from .const import TEST # noqa: F401
async def async_setup(hass, config):
"""Mock a successful setup."""
return True

View File

@ -0,0 +1,7 @@
"""Config flow."""
from homeassistant.core import HomeAssistant
async def _async_has_devices(hass: HomeAssistant) -> bool:
return True

View File

@ -0,0 +1,2 @@
"""Constants for test_package custom component."""
TEST = 5

View File

@ -0,0 +1,11 @@
{
"domain": "test_package_loaded_loop",
"name": "Test Package that loads in the loop",
"documentation": "http://test-package.io",
"requirements": [],
"dependencies": [],
"codeowners": [],
"config_flow": true,
"import_executor": false,
"version": "1.2.3"
}