Skip to content

Commit

Permalink
Add support for new translation mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrMachowski committed Feb 2, 2023
1 parent 2113950 commit b7cdb19
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
28 changes: 24 additions & 4 deletions custom_components/custom_templates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import logging

from homeassistant.exceptions import TemplateError
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, STATE_UNKNOWN
from homeassistant.const import EVENT_COMPONENT_LOADED, STATE_UNKNOWN
from homeassistant.core import Event, HomeAssistant, valid_entity_id
from homeassistant.helpers.entity_registry import async_get
from homeassistant.helpers.template import _get_state_if_valid, _RESERVED_NAMES, Template, TemplateEnvironment
from homeassistant.helpers.translation import _TranslationCache, TRANSLATION_FLATTEN_CACHE, TRANSLATION_LOAD_LOCK
from homeassistant.loader import bind_hass
Expand Down Expand Up @@ -36,14 +37,27 @@ def __call__(self, entity_id: str, language: str):

if state is None:
return STATE_UNKNOWN
entry = async_get(self._hass).async_get(entity_id)
translations = []
key = ""
if (entry is not None and
entry.unique_id is not None and
hasattr(entry, "translation_key") and
entry.translation_key is not None):
key = f"component.{entry.platform}.entity.{state.domain}.{entry.translation_key}.state.{state.state}"
translations = get_cached_translations(self._hass, language, "entity")
if len(translations) > 0 and key in translations:
return str(translations[key])

domain = state.domain
device_class = "_"
if "device_class" in state.attributes:
device_class = state.attributes["device_class"]
translations = get_cached_translations(self._hass, language, "state", domain)
key = f"component.{domain}.state.{device_class}.{state.state}"
translations = get_cached_translations(self._hass, language, "state", state.domain)
if len(translations) > 0 and key in translations:
return str(translations[key])
_LOGGER.warning(f"No translation found for entity: f{entity_id}")
return state.state

def __repr__(self):
Expand Down Expand Up @@ -80,9 +94,15 @@ async def load_translations_to_cache(
):
lock = hass.data.setdefault(TRANSLATION_LOAD_LOCK, asyncio.Lock())

components_entities = {
component for component in hass.config.components if "." not in component
}
components_state = set(hass.config.components)

async with lock:
cache = hass.data.setdefault(TRANSLATION_FLATTEN_CACHE, _TranslationCache(hass))
await cache.async_fetch(language, "states", set(hass.config.components))
await cache.async_fetch(language, "entity", components_entities)
await cache.async_fetch(language, "states", components_state)


@bind_hass
Expand Down Expand Up @@ -117,7 +137,7 @@ async def load_translations(_event: Event):
for language in languages:
await load_translations_to_cache(hass, language)

hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, load_translations)
hass.bus.async_listen(EVENT_COMPONENT_LOADED, load_translations)

_TranslationCache.get_cached = get_cached

Expand Down
2 changes: 1 addition & 1 deletion custom_components/custom_templates/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Custom Templates",
"documentation": "https://github.com/PiotrMachowski/Home-Assistant-custom-components-Custom-Templates",
"issue_tracker": "https://github.com/PiotrMachowski/Home-Assistant-custom-components-Custom-Templates/issues",
"version": "1.0.0",
"version": "1.0.1",
"requirements": [],
"codeowners": [
"@PiotrMachowski"
Expand Down

0 comments on commit b7cdb19

Please sign in to comment.