From a611c5f903fb74b98a0c7c36fcbd31ead5a0c371 Mon Sep 17 00:00:00 2001 From: Piotr Machowski Date: Tue, 11 Jun 2024 01:58:55 +0200 Subject: [PATCH] Refactor, HA compatibility adjustments --- README.md | 5 + .../custom_templates/__init__.py | 323 ++---------------- custom_components/custom_templates/const.py | 2 +- .../custom_templates/templates/__init__.py | 1 + .../templates/all_translations.py | 22 ++ .../custom_templates/templates/dict_merge.py | 14 + .../templates/eval_template.py | 17 + .../templates/is_available.py | 32 ++ .../templates/state_attr_translated.py | 33 ++ .../templates/state_translated.py | 34 ++ .../templates/translatable_template.py | 16 + .../custom_templates/templates/translated.py | 26 ++ .../custom_templates/templates/utils.py | 73 ++++ 13 files changed, 298 insertions(+), 300 deletions(-) create mode 100644 custom_components/custom_templates/templates/__init__.py create mode 100644 custom_components/custom_templates/templates/all_translations.py create mode 100644 custom_components/custom_templates/templates/dict_merge.py create mode 100644 custom_components/custom_templates/templates/eval_template.py create mode 100644 custom_components/custom_templates/templates/is_available.py create mode 100644 custom_components/custom_templates/templates/state_attr_translated.py create mode 100644 custom_components/custom_templates/templates/state_translated.py create mode 100644 custom_components/custom_templates/templates/translatable_template.py create mode 100644 custom_components/custom_templates/templates/translated.py create mode 100644 custom_components/custom_templates/templates/utils.py diff --git a/README.md b/README.md index 5f80a23..b88708e 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,11 @@ # Custom Templates +> [!CAUTION] +> This custom integration tampers with internal code of Home Assistant which _might_ cause some unforeseen issues (especially after HA updates). +> If you encounter any problems related to templating engine or translations try uninstalling this integration before raising an issue for Home Assistant. + + This integration adds possibility to use new functions in Home Assistant Jinja2 templating engine: - `ct_state_translated` - returns translated state of an entity - `ct_state_attr_translated` - returns translated value of an attribute of an entity diff --git a/custom_components/custom_templates/__init__.py b/custom_components/custom_templates/__init__.py index 9b17bb0..ce06260 100644 --- a/custom_components/custom_templates/__init__.py +++ b/custom_components/custom_templates/__init__.py @@ -1,21 +1,23 @@ -import asyncio -from collections import ChainMap import logging from typing import Any, Callable -from homeassistant.exceptions import TemplateError -from homeassistant.const import EVENT_COMPONENT_LOADED, STATE_UNAVAILABLE, 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.const import EVENT_COMPONENT_LOADED +from homeassistant.core import Event, HomeAssistant +from homeassistant.helpers.template import Template, TemplateEnvironment from homeassistant.helpers.translation import _TranslationCache, TRANSLATION_FLATTEN_CACHE -from homeassistant.loader import bind_hass from .const import (DOMAIN, CUSTOM_TEMPLATES_SCHEMA, CONF_PRELOAD_TRANSLATIONS, CONST_EVAL_FUNCTION_NAME, CONST_STATE_TRANSLATED_FUNCTION_NAME, CONST_STATE_ATTR_TRANSLATED_FUNCTION_NAME, CONST_TRANSLATED_FUNCTION_NAME, CONST_ALL_TRANSLATIONS_FUNCTION_NAME, - DEFAULT_UNAVAILABLE_STATES, CONST_IS_AVAILABLE_FUNCTION_NAME, + CONST_IS_AVAILABLE_FUNCTION_NAME, CONST_DICT_MERGE_FUNCTION_NAME) +from .templates.all_translations import AllTranslations +from .templates.dict_merge import DictMerge +from .templates.eval_template import EvalTemplate +from .templates.is_available import IsAvailable +from .templates.state_attr_translated import StateAttrTranslated +from .templates.state_translated import StateTranslated +from .templates.translated import Translated _LOGGER = logging.getLogger(__name__) @@ -23,300 +25,25 @@ ConfigType = dict[str, Any] -class TranslatableTemplate: - - def __init__(self, hass: HomeAssistant, available_languages): - self._hass = hass - self._available_languages = available_languages - - def validate_language(self, language): - if language not in self._available_languages: - raise TemplateError(f"Language {language} is not loaded") # type: ignore[arg-type] - - -class DictMerge: - - def __init__(self, hass: HomeAssistant): - self._hass = hass - - def __call__(self, *args): - result = {k:v for d in args for k, v in d.items()} - return result - - def __repr__(self): - return f"