mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 06:37:52 +00:00
Small cleanup to entity platform translation fetching (#108890)
* Small cleanup to entity platform translation fetching While I could not realize the performance improvemnet I had hoped in #108800, I pulled this out since its a nice cleanup to avoid constructing the inner function over and over. * stale docstring
This commit is contained in:
parent
3f31a76692
commit
617e8dd8a5
@ -390,6 +390,22 @@ class EntityPlatform:
|
|||||||
finally:
|
finally:
|
||||||
warn_task.cancel()
|
warn_task.cancel()
|
||||||
|
|
||||||
|
async def _async_get_translations(
|
||||||
|
self, language: str, category: str, integration: str
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
"""Get translations for a language, category, and integration."""
|
||||||
|
try:
|
||||||
|
return await translation.async_get_translations(
|
||||||
|
self.hass, language, category, {integration}
|
||||||
|
)
|
||||||
|
except Exception as err: # pylint: disable=broad-exception-caught
|
||||||
|
_LOGGER.debug(
|
||||||
|
"Could not load translations for %s",
|
||||||
|
integration,
|
||||||
|
exc_info=err,
|
||||||
|
)
|
||||||
|
return {}
|
||||||
|
|
||||||
async def async_load_translations(self) -> None:
|
async def async_load_translations(self) -> None:
|
||||||
"""Load translations."""
|
"""Load translations."""
|
||||||
hass = self.hass
|
hass = self.hass
|
||||||
@ -398,37 +414,21 @@ class EntityPlatform:
|
|||||||
if hass.config.language in languages.NATIVE_ENTITY_IDS
|
if hass.config.language in languages.NATIVE_ENTITY_IDS
|
||||||
else languages.DEFAULT_LANGUAGE
|
else languages.DEFAULT_LANGUAGE
|
||||||
)
|
)
|
||||||
|
config_language = hass.config.language
|
||||||
async def get_translations(
|
self.component_translations = await self._async_get_translations(
|
||||||
language: str, category: str, integration: str
|
config_language, "entity_component", self.domain
|
||||||
) -> dict[str, Any]:
|
|
||||||
"""Get entity translations."""
|
|
||||||
try:
|
|
||||||
return await translation.async_get_translations(
|
|
||||||
hass, language, category, {integration}
|
|
||||||
)
|
|
||||||
except Exception as err: # pylint: disable=broad-exception-caught
|
|
||||||
_LOGGER.debug(
|
|
||||||
"Could not load translations for %s",
|
|
||||||
integration,
|
|
||||||
exc_info=err,
|
|
||||||
)
|
|
||||||
return {}
|
|
||||||
|
|
||||||
self.component_translations = await get_translations(
|
|
||||||
hass.config.language, "entity_component", self.domain
|
|
||||||
)
|
)
|
||||||
self.platform_translations = await get_translations(
|
self.platform_translations = await self._async_get_translations(
|
||||||
hass.config.language, "entity", self.platform_name
|
config_language, "entity", self.platform_name
|
||||||
)
|
)
|
||||||
if object_id_language == hass.config.language:
|
if object_id_language == config_language:
|
||||||
self.object_id_component_translations = self.component_translations
|
self.object_id_component_translations = self.component_translations
|
||||||
self.object_id_platform_translations = self.platform_translations
|
self.object_id_platform_translations = self.platform_translations
|
||||||
else:
|
else:
|
||||||
self.object_id_component_translations = await get_translations(
|
self.object_id_component_translations = await self._async_get_translations(
|
||||||
object_id_language, "entity_component", self.domain
|
object_id_language, "entity_component", self.domain
|
||||||
)
|
)
|
||||||
self.object_id_platform_translations = await get_translations(
|
self.object_id_platform_translations = await self._async_get_translations(
|
||||||
object_id_language, "entity", self.platform_name
|
object_id_language, "entity", self.platform_name
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user