Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions homeassistant/components/mobile_app/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@
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

Expand Down Expand Up @@ -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,
}
}
Expand All @@ -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)
Expand Down
17 changes: 15 additions & 2 deletions tests/components/mobile_app/test_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down