From 4341b21a61f9994a426f0dcf82c50b941ae721fc Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 13 Mar 2024 23:44:36 -1000 Subject: [PATCH] Migrate auth to use async_import_module to avoid blocking I/O in the event loop (#113387) --- homeassistant/auth/mfa_modules/__init__.py | 4 ++-- homeassistant/auth/providers/__init__.py | 6 ++++-- tests/auth/providers/test_homeassistant.py | 1 + 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/homeassistant/auth/mfa_modules/__init__.py b/homeassistant/auth/mfa_modules/__init__.py index 0cfb7508dbf..fd4072ea88a 100644 --- a/homeassistant/auth/mfa_modules/__init__.py +++ b/homeassistant/auth/mfa_modules/__init__.py @@ -2,7 +2,6 @@ from __future__ import annotations -import importlib import logging import types from typing import Any @@ -15,6 +14,7 @@ from homeassistant.const import CONF_ID, CONF_NAME, CONF_TYPE 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() @@ -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( diff --git a/homeassistant/auth/providers/__init__.py b/homeassistant/auth/providers/__init__.py index 0a3d697eeea..63028f54d2e 100644 --- a/homeassistant/auth/providers/__init__.py +++ b/homeassistant/auth/providers/__init__.py @@ -3,7 +3,6 @@ from __future__ import annotations from collections.abc import Mapping -import importlib import logging import types from typing import Any @@ -15,6 +14,7 @@ from homeassistant import data_entry_flow, requirements 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 @@ -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( diff --git a/tests/auth/providers/test_homeassistant.py b/tests/auth/providers/test_homeassistant.py index 22d4ba26845..dc5c255579c 100644 --- a/tests/auth/providers/test_homeassistant.py +++ b/tests/auth/providers/test_homeassistant.py @@ -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"}