mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Migrate auth to use async_import_module to avoid blocking I/O in the event loop (#113387)
This commit is contained in:
parent
4aec48d358
commit
4341b21a61
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import importlib
|
|
||||||
import logging
|
import logging
|
||||||
import types
|
import types
|
||||||
from typing import Any
|
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.core import HomeAssistant
|
||||||
from homeassistant.data_entry_flow import FlowResult
|
from homeassistant.data_entry_flow import FlowResult
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
|
from homeassistant.helpers.importlib import async_import_module
|
||||||
from homeassistant.util.decorator import Registry
|
from homeassistant.util.decorator import Registry
|
||||||
|
|
||||||
MULTI_FACTOR_AUTH_MODULES: Registry[str, type[MultiFactorAuthModule]] = 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}"
|
module_path = f"homeassistant.auth.mfa_modules.{module_name}"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
module = importlib.import_module(module_path)
|
module = await async_import_module(hass, module_path)
|
||||||
except ImportError as err:
|
except ImportError as err:
|
||||||
_LOGGER.error("Unable to load mfa module %s: %s", module_name, err)
|
_LOGGER.error("Unable to load mfa module %s: %s", module_name, err)
|
||||||
raise HomeAssistantError(
|
raise HomeAssistantError(
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Mapping
|
from collections.abc import Mapping
|
||||||
import importlib
|
|
||||||
import logging
|
import logging
|
||||||
import types
|
import types
|
||||||
from typing import Any
|
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.const import CONF_ID, CONF_NAME, CONF_TYPE
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
|
from homeassistant.helpers.importlib import async_import_module
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
from homeassistant.util.decorator import Registry
|
from homeassistant.util.decorator import Registry
|
||||||
|
|
||||||
@ -157,7 +157,9 @@ async def load_auth_provider_module(
|
|||||||
) -> types.ModuleType:
|
) -> types.ModuleType:
|
||||||
"""Load an auth provider."""
|
"""Load an auth provider."""
|
||||||
try:
|
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:
|
except ImportError as err:
|
||||||
_LOGGER.error("Unable to load auth provider %s: %s", provider, err)
|
_LOGGER.error("Unable to load auth provider %s: %s", provider, err)
|
||||||
raise HomeAssistantError(
|
raise HomeAssistantError(
|
||||||
|
@ -41,6 +41,7 @@ async def test_validating_password_invalid_user(data, hass: HomeAssistant) -> No
|
|||||||
async def test_not_allow_set_id() -> None:
|
async def test_not_allow_set_id() -> None:
|
||||||
"""Test we are not allowed to set an ID in config."""
|
"""Test we are not allowed to set an ID in config."""
|
||||||
hass = Mock()
|
hass = Mock()
|
||||||
|
hass.data = {}
|
||||||
with pytest.raises(vol.Invalid):
|
with pytest.raises(vol.Invalid):
|
||||||
await auth_provider_from_config(
|
await auth_provider_from_config(
|
||||||
hass, None, {"type": "homeassistant", "id": "invalid"}
|
hass, None, {"type": "homeassistant", "id": "invalid"}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user