diff --git a/custom_components/xiaomi_miot/core/miio2miot.py b/custom_components/xiaomi_miot/core/miio2miot.py index b6b30bc4f..b680aa61d 100644 --- a/custom_components/xiaomi_miot/core/miio2miot.py +++ b/custom_components/xiaomi_miot/core/miio2miot.py @@ -5,8 +5,8 @@ from functools import partial from .utils import is_offline_exception +from .templates import template from .miot_spec import (MiotSpec, MiotProperty, MiotAction) -from .templates import CUSTOM_TEMPLATES from .miio2miot_specs import MIIO_TO_MIOT_SPECS import homeassistant.helpers.config_validation as cv @@ -86,9 +86,7 @@ def get_miio_props(self, device): if kls is True: kls = c.get('params', []) if tpl := c.get('template'): - tpl = CUSTOM_TEMPLATES.get(tpl, tpl) - tpl = cv.template(tpl) - tpl.hass = self.hass + tpl = template(tpl, self.hass) pdt = tpl.render({'results': vls}) if isinstance(pdt, dict): dic.update(pdt) @@ -102,9 +100,7 @@ def get_miio_props(self, device): dic[k] = vls[i] i += 1 if tpl := self.config.get('miio_template'): - tpl = CUSTOM_TEMPLATES.get(tpl, tpl) - tpl = cv.template(tpl) - tpl.hass = self.hass + tpl = template(tpl, self.hass) pdt = tpl.render({'props': dic}) if isinstance(pdt, dict): dic.update(pdt) @@ -135,9 +131,7 @@ def get_miot_props(self, device, mapping: dict = None): fmt = c.get('format') try: if tpl := c.get('template', {}): - tpl = CUSTOM_TEMPLATES.get(tpl, tpl) - tpl = cv.template(tpl) - tpl.hass = self.hass + tpl = template(tpl, self.hass) val = tpl.render({ 'value': val, 'props': dic, @@ -194,9 +188,7 @@ def set_property(self, device, siid, piid, value): mph = MiioPropertyHelper(prop, reverse=True) fmt = cfg.get('format') if tpl := cfg.get('set_template'): - tpl = CUSTOM_TEMPLATES.get(tpl, tpl) - tpl = cv.template(tpl) - tpl.hass = self.hass + tpl = template(tpl, self.hass) pms = tpl.render({ 'value': value, 'props': self.miio_props_values, @@ -243,9 +235,7 @@ def call_action(self, device, siid, aiid, params): act = self.miot_spec.specs.get(key) if act and isinstance(act, MiotAction): if tpl := cfg.get('set_template'): - tpl = CUSTOM_TEMPLATES.get(tpl, tpl) - tpl = cv.template(tpl) - tpl.hass = self.hass + tpl = template(tpl, self.hass) pms = tpl.render({ 'params': pms, 'props': self.miio_props_values, diff --git a/custom_components/xiaomi_miot/core/templates.py b/custom_components/xiaomi_miot/core/templates.py index dd0fb0665..ab3dfa8ed 100644 --- a/custom_components/xiaomi_miot/core/templates.py +++ b/custom_components/xiaomi_miot/core/templates.py @@ -1,3 +1,5 @@ +from homeassistant.helpers.template import Template + CUSTOM_TEMPLATES = { # https://iot.mi.com/new/doc/accesses/direct-access/embedded-development/ble/object-definition#%E7%89%99%E5%88%B7%E4%BA%8B%E4%BB%B6 'ble_toothbrush_events': "{%- set dat = props.get('event.16') | default('{}',true) | from_json %}" @@ -182,3 +184,14 @@ "'month': dat.month | round(3)," "} }}", } + + +def template(value, hass): + if value is None: + raise ValueError('template value is None') + if isinstance(value, (list, dict, Template)): + raise TypeError('template value should be a string') + value = CUSTOM_TEMPLATES.get(value, value) + template_value = Template(str(value), hass) + template_value.ensure_valid() + return template_value