Skip to content

Commit

Permalink
Merge pull request #29 from PiotrMachowski/dev
Browse files Browse the repository at this point in the history
v1.4.1
  • Loading branch information
PiotrMachowski authored Sep 20, 2024
2 parents 63f27b9 + 69e4db6 commit 7067533
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 15 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ below_horizon
</tr>
</table>

Optional parameters:
* `variables` (`dict[string, Any]`) - allows adding additional variables to evaluation context
* `parse_result` (`bool`, default: `True`) - allows to disable result parsing for internal template evaluation
* `pass_context` (`bool`, default: `True`) - allows to disable passing external context to evaluation of internal template

### `ct_is_available`

This function checks if given entity has an available state.
Expand Down
3 changes: 2 additions & 1 deletion custom_components/custom_templates/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from typing import Any, Callable
from jinja2 import pass_context

from homeassistant.const import EVENT_COMPONENT_LOADED
from homeassistant.core import Event, HomeAssistant
Expand Down Expand Up @@ -51,7 +52,7 @@ async def _async_load_translations(_: Event) -> None:
state_attr_translated_template = StateAttrTranslated(hass, languages)
translated_template = Translated(hass, languages)
all_translations_template = AllTranslations(hass, languages)
eval_template = EvalTemplate(hass)
eval_template = pass_context(EvalTemplate(hass))
is_available_template = IsAvailable(hass)
dict_merge_template = DictMerge(hass)

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 @@ -6,5 +6,5 @@
"iot_class": "calculated",
"issue_tracker": "https://github.com/PiotrMachowski/Home-Assistant-custom-components-Custom-Templates/issues",
"requirements": [],
"version": "v1.4.0"
"version": "v1.4.1"
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, hass: HomeAssistant, available_languages: list[str]) -> None:
super().__init__(hass, available_languages)

def __call__(self, language: str | None = None) -> dict[str, str]:
language = self.validate_language(language)
language = self._validate_language(language)
translations = {}
translations.update(async_get_cached_translations(self._hass, language, "state"))
translations.update(async_get_cached_translations(self._hass, language, "entity"))
Expand Down
19 changes: 16 additions & 3 deletions custom_components/custom_templates/templates/eval_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,29 @@

from homeassistant.core import HomeAssistant
from homeassistant.helpers.template import Template
from jinja2.runtime import Context


class EvalTemplate:

def __init__(self, hass: HomeAssistant) -> None:
self._hass = hass

def __call__(self, content: str) -> Any:
def __call__(
self,
passed_context: Context,
content: str,
variables: dict | None = None,
parse_result: bool = True,
pass_context: bool = True
) -> Any:
context = {}
if pass_context:
context = context | passed_context.get_all()
if variables is not None:
context = context | variables
tpl = Template(content, self._hass)
return tpl.async_render()
return tpl.async_render(variables=context, parse_result=parse_result)

def __repr__(self) -> str:
return "<template CT_EvalTemplate>"
return "<template CT_EvalTemplate>"
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from homeassistant.const import STATE_UNKNOWN
from homeassistant.core import HomeAssistant, valid_entity_id
from homeassistant.exceptions import TemplateError
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_registry import async_get
from homeassistant.helpers.template import _get_state_if_valid, _RESERVED_NAMES
from homeassistant.helpers.template import _get_state_if_valid

from .translatable_template import TranslatableTemplate
from .utils import async_translate_state_attribute
Expand All @@ -14,7 +13,7 @@ def __init__(self, hass: HomeAssistant, available_languages: list[str]) -> None:
super().__init__(hass, available_languages)

def __call__(self, entity_id: str, attribute: str, language: str | None = None) -> str:
language = self.validate_language(language)
language = self._validate_language(language)
state = _get_state_if_valid(self._hass, entity_id)
if state is None:
return STATE_UNKNOWN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(self, hass: HomeAssistant, available_languages: list[str]) -> None:
super().__init__(hass, available_languages)

def __call__(self, entity_id: str, language: str | None = None) -> str:
language = self.validate_language(language)
language = self._validate_language(language)
state = _get_state_if_valid(self._hass, entity_id)
if state is None:
return STATE_UNKNOWN
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from abc import ABC

from homeassistant.core import HomeAssistant
from homeassistant.exceptions import TemplateError


class TranslatableTemplate:
class TranslatableTemplate(ABC):

def __init__(self, hass: HomeAssistant, available_languages: list[str]) -> None:
self._hass = hass
self._available_languages = available_languages

def validate_language(self, language: str | None = None) -> str:
def _validate_language(self, language: str | None = None) -> str:
if language is None:
return self._hass.config.language
if language not in self._available_languages:
raise TemplateError(f"Language {language} is not loaded") # type: ignore[arg-type]
return language
return language
2 changes: 1 addition & 1 deletion custom_components/custom_templates/templates/translated.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def __init__(self, hass: HomeAssistant, available_languages: list[str]) -> None:
super().__init__(hass, available_languages)

def __call__(self, key: str, language: str | None = None) -> str:
language = self.validate_language(language)
language = self._validate_language(language)
translations = async_get_cached_translations(self._hass, language, "state")
if len(translations) > 0 and key in translations:
return str(translations[key])
Expand Down

0 comments on commit 7067533

Please sign in to comment.