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

Set the translations cache under the lock (#42470)

This commit is contained in:
J. Nick Koston
2020-10-27 16:02:38 -05:00
committed by GitHub
parent 30a3fe69bc
commit 5ca4b4cd0f
2 changed files with 25 additions and 17 deletions

View File

@@ -294,7 +294,8 @@ async def async_get_translations(
}
async with lock:
if integration is None and not config_flow:
use_cache = integration is None and not config_flow
if use_cache:
cache = hass.data.get(TRANSLATION_FLATTEN_CACHE)
if cache is None:
cache = hass.data[TRANSLATION_FLATTEN_CACHE] = FlatCache(hass)
@@ -317,24 +318,24 @@ async def async_get_translations(
results = await asyncio.gather(*tasks)
if category == "state":
resource_func = merge_resources
else:
resource_func = build_resources
if category == "state":
resource_func = merge_resources
else:
resource_func = build_resources
resources = flatten(resource_func(results[0], components, category))
resources = flatten(resource_func(results[0], components, category))
if language != "en":
base_resources = flatten(resource_func(results[1], components, category))
resources = {**base_resources, **resources}
if language != "en":
base_resources = flatten(resource_func(results[1], components, category))
resources = {**base_resources, **resources}
if integration is not None:
pass
elif config_flow:
# The cache must be set while holding the lock
if use_cache:
assert cache is not None
cache.async_set_cache(language, category, resources)
if config_flow:
loaded_comp_resources = await async_get_translations(hass, language, category)
resources.update(loaded_comp_resources)
else:
assert cache is not None
cache.async_set_cache(language, category, resources)
return resources