1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Support requesting translations for multiple integrations in a single request (#71979)

This commit is contained in:
J. Nick Koston
2022-05-17 01:23:11 -05:00
committed by GitHub
parent 78f0716574
commit a614ddca28
5 changed files with 88 additions and 15 deletions

View File

@@ -3,7 +3,7 @@ from __future__ import annotations
import asyncio
from collections import ChainMap
from collections.abc import Mapping
from collections.abc import Iterable, Mapping
import logging
from typing import Any
@@ -286,7 +286,7 @@ async def async_get_translations(
hass: HomeAssistant,
language: str,
category: str,
integration: str | None = None,
integrations: Iterable[str] | None = None,
config_flow: bool | None = None,
) -> dict[str, Any]:
"""Return all backend translations.
@@ -297,8 +297,8 @@ async def async_get_translations(
"""
lock = hass.data.setdefault(TRANSLATION_LOAD_LOCK, asyncio.Lock())
if integration is not None:
components = {integration}
if integrations is not None:
components = set(integrations)
elif config_flow:
components = (await async_get_config_flows(hass)) - hass.config.components
elif category == "state":
@@ -310,7 +310,10 @@ async def async_get_translations(
}
async with lock:
cache = hass.data.setdefault(TRANSLATION_FLATTEN_CACHE, _TranslationCache(hass))
if TRANSLATION_FLATTEN_CACHE in hass.data:
cache = hass.data[TRANSLATION_FLATTEN_CACHE]
else:
cache = hass.data[TRANSLATION_FLATTEN_CACHE] = _TranslationCache(hass)
cached = await cache.async_fetch(language, category, components)
return dict(ChainMap(*cached))