Skip to content

Commit

Permalink
Fix scene integration doing blocking I/O in the event loop to import …
Browse files Browse the repository at this point in the history
…platforms (home-assistant#113391)
  • Loading branch information
bdraco authored Mar 15, 2024
1 parent 09934d4 commit 052d7d1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
5 changes: 5 additions & 0 deletions homeassistant/components/homeassistant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
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,
Expand Down
12 changes: 5 additions & 7 deletions homeassistant/components/scene/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@ 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."""
platform_name = config[CONF_PLATFORM]
try:
platform = importlib.import_module(f".{config[CONF_PLATFORM]}", __name__)
platform = importlib.import_module(
f"homeassistant.components.{platform_name}.scene"
)
except ImportError:
try:
platform = importlib.import_module(
f"homeassistant.components.{config[CONF_PLATFORM]}.scene"
)
except ImportError:
raise vol.Invalid("Invalid platform specified") from None
raise vol.Invalid("Invalid platform specified") from None

if not hasattr(platform, "PLATFORM_SCHEMA"):
return config
Expand Down
12 changes: 12 additions & 0 deletions tests/components/scene/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 052d7d1

Please sign in to comment.