Fix scene integration doing blocking I/O in the event loop to import platforms (#113391)

This commit is contained in:
J. Nick Koston 2024-03-14 14:56:57 -10:00 committed by GitHub
parent 09934d44c4
commit 052d7d1e19
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 7 deletions

View File

@ -35,6 +35,11 @@ from homeassistant.helpers.service import (
from homeassistant.helpers.template import async_load_custom_templates
from homeassistant.helpers.typing import ConfigType
# The scene integration will do a late import of scene
# so we want to make sure its loaded with the component
# so its already in memory when its imported so the import
# does not do blocking I/O in the event loop.
from . import scene as scene_pre_import # noqa: F401
from .const import (
DATA_EXPOSED_ENTITIES,
DATA_STOP_HANDLER,

View File

@ -32,12 +32,10 @@ def _hass_domain_validator(config: dict[str, Any]) -> dict[str, Any]:
def _platform_validator(config: dict[str, Any]) -> dict[str, Any]:
"""Validate it is a valid platform."""
try:
platform = importlib.import_module(f".{config[CONF_PLATFORM]}", __name__)
except ImportError:
platform_name = config[CONF_PLATFORM]
try:
platform = importlib.import_module(
f"homeassistant.components.{config[CONF_PLATFORM]}.scene"
f"homeassistant.components.{platform_name}.scene"
)
except ImportError:
raise vol.Invalid("Invalid platform specified") from None

View File

@ -262,3 +262,15 @@ async def turn_off_lights(hass, entity_ids):
blocking=True,
)
await hass.async_block_till_done()
async def test_invalid_platform(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test invalid platform."""
await async_setup_component(
hass, scene.DOMAIN, {scene.DOMAIN: {"platform": "does_not_exist"}}
)
await hass.async_block_till_done()
assert "Invalid platform specified" in caplog.text
assert "does_not_exist" in caplog.text