Skip to content

Commit

Permalink
🔧 fix warnings for template (#1879)
Browse files Browse the repository at this point in the history
  • Loading branch information
al-one committed Sep 29, 2024
1 parent 587b6d7 commit c432bdb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
22 changes: 6 additions & 16 deletions custom_components/xiaomi_miot/core/miio2miot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions custom_components/xiaomi_miot/core/templates.py
Original file line number Diff line number Diff line change
@@ -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 %}"
Expand Down Expand Up @@ -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

0 comments on commit c432bdb

Please sign in to comment.