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:
J. Nick Koston 2024-01-25 19:14:44 -10:00 committed by GitHub
parent 3f31a76692
commit 617e8dd8a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -390,22 +390,13 @@ class EntityPlatform:
finally: finally:
warn_task.cancel() warn_task.cancel()
async def async_load_translations(self) -> None: async def _async_get_translations(
"""Load translations.""" self, language: str, category: str, integration: str
hass = self.hass
object_id_language = (
hass.config.language
if hass.config.language in languages.NATIVE_ENTITY_IDS
else languages.DEFAULT_LANGUAGE
)
async def get_translations(
language: str, category: str, integration: str
) -> dict[str, Any]: ) -> dict[str, Any]:
"""Get entity translations.""" """Get translations for a language, category, and integration."""
try: try:
return await translation.async_get_translations( return await translation.async_get_translations(
hass, language, category, {integration} self.hass, language, category, {integration}
) )
except Exception as err: # pylint: disable=broad-exception-caught except Exception as err: # pylint: disable=broad-exception-caught
_LOGGER.debug( _LOGGER.debug(
@ -415,20 +406,29 @@ class EntityPlatform:
) )
return {} return {}
self.component_translations = await get_translations( async def async_load_translations(self) -> None:
hass.config.language, "entity_component", self.domain """Load translations."""
hass = self.hass
object_id_language = (
hass.config.language
if hass.config.language in languages.NATIVE_ENTITY_IDS
else languages.DEFAULT_LANGUAGE
) )
self.platform_translations = await get_translations( config_language = hass.config.language
hass.config.language, "entity", self.platform_name self.component_translations = await self._async_get_translations(
config_language, "entity_component", self.domain
) )
if object_id_language == hass.config.language: self.platform_translations = await self._async_get_translations(
config_language, "entity", self.platform_name
)
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
) )