Skip to content

Commit

Permalink
Migrate auth to use async_import_module to avoid blocking I/O in the …
Browse files Browse the repository at this point in the history
…event loop (home-assistant#113387)
  • Loading branch information
bdraco authored Mar 14, 2024
1 parent 4aec48d commit 4341b21
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions homeassistant/auth/mfa_modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

import importlib
import logging
import types
from typing import Any
Expand All @@ -15,6 +14,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.importlib import async_import_module
from homeassistant.util.decorator import Registry

MULTI_FACTOR_AUTH_MODULES: Registry[str, type[MultiFactorAuthModule]] = Registry()
Expand Down Expand Up @@ -149,7 +149,7 @@ async def _load_mfa_module(hass: HomeAssistant, module_name: str) -> types.Modul
module_path = f"homeassistant.auth.mfa_modules.{module_name}"

try:
module = importlib.import_module(module_path)
module = await async_import_module(hass, module_path)
except ImportError as err:
_LOGGER.error("Unable to load mfa module %s: %s", module_name, err)
raise HomeAssistantError(
Expand Down
6 changes: 4 additions & 2 deletions homeassistant/auth/providers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import annotations

from collections.abc import Mapping
import importlib
import logging
import types
from typing import Any
Expand All @@ -15,6 +14,7 @@
from homeassistant.const import CONF_ID, CONF_NAME, CONF_TYPE
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.importlib import async_import_module
from homeassistant.util import dt as dt_util
from homeassistant.util.decorator import Registry

Expand Down Expand Up @@ -157,7 +157,9 @@ async def load_auth_provider_module(
) -> types.ModuleType:
"""Load an auth provider."""
try:
module = importlib.import_module(f"homeassistant.auth.providers.{provider}")
module = await async_import_module(
hass, f"homeassistant.auth.providers.{provider}"
)
except ImportError as err:
_LOGGER.error("Unable to load auth provider %s: %s", provider, err)
raise HomeAssistantError(
Expand Down
1 change: 1 addition & 0 deletions tests/auth/providers/test_homeassistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ async def test_validating_password_invalid_user(data, hass: HomeAssistant) -> No
async def test_not_allow_set_id() -> None:
"""Test we are not allowed to set an ID in config."""
hass = Mock()
hass.data = {}
with pytest.raises(vol.Invalid):
await auth_provider_from_config(
hass, None, {"type": "homeassistant", "id": "invalid"}
Expand Down

0 comments on commit 4341b21

Please sign in to comment.