Improve mobile app template handling (#41703)
Co-authored-by: J. Nick Koston <nick@koston.org>pull/41724/head
parent
feabf99f92
commit
57996e1942
|
@ -32,10 +32,13 @@ from homeassistant.const import (
|
|||
HTTP_CREATED,
|
||||
)
|
||||
from homeassistant.core import EventOrigin
|
||||
from homeassistant.exceptions import HomeAssistantError, ServiceNotFound, TemplateError
|
||||
from homeassistant.helpers import config_validation as cv, device_registry as dr
|
||||
from homeassistant.exceptions import HomeAssistantError, ServiceNotFound
|
||||
from homeassistant.helpers import (
|
||||
config_validation as cv,
|
||||
device_registry as dr,
|
||||
template,
|
||||
)
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.helpers.template import attach
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
from homeassistant.util.decorator import Registry
|
||||
|
||||
|
@ -285,7 +288,7 @@ async def webhook_stream_camera(hass, config_entry, data):
|
|||
@validate_schema(
|
||||
{
|
||||
str: {
|
||||
vol.Required(ATTR_TEMPLATE): cv.template,
|
||||
vol.Required(ATTR_TEMPLATE): cv.string,
|
||||
vol.Optional(ATTR_TEMPLATE_VARIABLES, default={}): dict,
|
||||
}
|
||||
}
|
||||
|
@ -295,10 +298,9 @@ async def webhook_render_template(hass, config_entry, data):
|
|||
resp = {}
|
||||
for key, item in data.items():
|
||||
try:
|
||||
tpl = item[ATTR_TEMPLATE]
|
||||
attach(hass, tpl)
|
||||
tpl = template.Template(item[ATTR_TEMPLATE], hass)
|
||||
resp[key] = tpl.async_render(item.get(ATTR_TEMPLATE_VARIABLES))
|
||||
except TemplateError as ex:
|
||||
except template.TemplateError as ex:
|
||||
resp[key] = {"error": str(ex)}
|
||||
|
||||
return webhook_response(resp, registration=config_entry.data)
|
||||
|
|
|
@ -70,13 +70,26 @@ async def test_webhook_handle_render_template(create_registrations, webhook_clie
|
|||
"""Test that we render templates properly."""
|
||||
resp = await webhook_client.post(
|
||||
"/api/webhook/{}".format(create_registrations[1]["webhook_id"]),
|
||||
json=RENDER_TEMPLATE,
|
||||
json={
|
||||
"type": "render_template",
|
||||
"data": {
|
||||
"one": {"template": "Hello world"},
|
||||
"two": {"template": "{{ now() | random }}"},
|
||||
"three": {"template": "{{ now() 3 }}"},
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
assert resp.status == 200
|
||||
|
||||
json = await resp.json()
|
||||
assert json == {"one": "Hello world"}
|
||||
assert json == {
|
||||
"one": "Hello world",
|
||||
"two": {"error": "TypeError: object of type 'datetime.datetime' has no len()"},
|
||||
"three": {
|
||||
"error": "TemplateSyntaxError: expected token 'end of print statement', got 'integer'"
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
async def test_webhook_handle_call_services(hass, create_registrations, webhook_client):
|
||||
|
|
Loading…
Reference in New Issue