mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 16:27:08 +00:00
Use HassKey for importlib helper (#117116)
This commit is contained in:
parent
a77add1b77
commit
04c0b7d3df
@ -10,12 +10,15 @@ import sys
|
|||||||
from types import ModuleType
|
from types import ModuleType
|
||||||
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.util.hass_dict import HassKey
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DATA_IMPORT_CACHE = "import_cache"
|
DATA_IMPORT_CACHE: HassKey[dict[str, ModuleType]] = HassKey("import_cache")
|
||||||
DATA_IMPORT_FUTURES = "import_futures"
|
DATA_IMPORT_FUTURES: HassKey[dict[str, asyncio.Future[ModuleType]]] = HassKey(
|
||||||
DATA_IMPORT_FAILURES = "import_failures"
|
"import_futures"
|
||||||
|
)
|
||||||
|
DATA_IMPORT_FAILURES: HassKey[dict[str, bool]] = HassKey("import_failures")
|
||||||
|
|
||||||
|
|
||||||
def _get_module(cache: dict[str, ModuleType], name: str) -> ModuleType:
|
def _get_module(cache: dict[str, ModuleType], name: str) -> ModuleType:
|
||||||
@ -26,17 +29,15 @@ def _get_module(cache: dict[str, ModuleType], name: str) -> ModuleType:
|
|||||||
|
|
||||||
async def async_import_module(hass: HomeAssistant, name: str) -> ModuleType:
|
async def async_import_module(hass: HomeAssistant, name: str) -> ModuleType:
|
||||||
"""Import a module or return it from the cache."""
|
"""Import a module or return it from the cache."""
|
||||||
cache: dict[str, ModuleType] = hass.data.setdefault(DATA_IMPORT_CACHE, {})
|
cache = hass.data.setdefault(DATA_IMPORT_CACHE, {})
|
||||||
if module := cache.get(name):
|
if module := cache.get(name):
|
||||||
return module
|
return module
|
||||||
|
|
||||||
failure_cache: dict[str, bool] = hass.data.setdefault(DATA_IMPORT_FAILURES, {})
|
failure_cache = hass.data.setdefault(DATA_IMPORT_FAILURES, {})
|
||||||
if name in failure_cache:
|
if name in failure_cache:
|
||||||
raise ModuleNotFoundError(f"{name} not found", name=name)
|
raise ModuleNotFoundError(f"{name} not found", name=name)
|
||||||
|
|
||||||
import_futures: dict[str, asyncio.Future[ModuleType]]
|
|
||||||
import_futures = hass.data.setdefault(DATA_IMPORT_FUTURES, {})
|
import_futures = hass.data.setdefault(DATA_IMPORT_FUTURES, {})
|
||||||
|
|
||||||
if future := import_futures.get(name):
|
if future := import_futures.get(name):
|
||||||
return await future
|
return await future
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user