mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Improve mobile app template handling (#41703)
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
parent
feabf99f92
commit
57996e1942
@ -32,10 +32,13 @@ from homeassistant.const import (
|
|||||||
HTTP_CREATED,
|
HTTP_CREATED,
|
||||||
)
|
)
|
||||||
from homeassistant.core import EventOrigin
|
from homeassistant.core import EventOrigin
|
||||||
from homeassistant.exceptions import HomeAssistantError, ServiceNotFound, TemplateError
|
from homeassistant.exceptions import HomeAssistantError, ServiceNotFound
|
||||||
from homeassistant.helpers import config_validation as cv, device_registry as dr
|
from homeassistant.helpers import (
|
||||||
|
config_validation as cv,
|
||||||
|
device_registry as dr,
|
||||||
|
template,
|
||||||
|
)
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||||
from homeassistant.helpers.template import attach
|
|
||||||
from homeassistant.helpers.typing import HomeAssistantType
|
from homeassistant.helpers.typing import HomeAssistantType
|
||||||
from homeassistant.util.decorator import Registry
|
from homeassistant.util.decorator import Registry
|
||||||
|
|
||||||
@ -285,7 +288,7 @@ async def webhook_stream_camera(hass, config_entry, data):
|
|||||||
@validate_schema(
|
@validate_schema(
|
||||||
{
|
{
|
||||||
str: {
|
str: {
|
||||||
vol.Required(ATTR_TEMPLATE): cv.template,
|
vol.Required(ATTR_TEMPLATE): cv.string,
|
||||||
vol.Optional(ATTR_TEMPLATE_VARIABLES, default={}): dict,
|
vol.Optional(ATTR_TEMPLATE_VARIABLES, default={}): dict,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -295,10 +298,9 @@ async def webhook_render_template(hass, config_entry, data):
|
|||||||
resp = {}
|
resp = {}
|
||||||
for key, item in data.items():
|
for key, item in data.items():
|
||||||
try:
|
try:
|
||||||
tpl = item[ATTR_TEMPLATE]
|
tpl = template.Template(item[ATTR_TEMPLATE], hass)
|
||||||
attach(hass, tpl)
|
|
||||||
resp[key] = tpl.async_render(item.get(ATTR_TEMPLATE_VARIABLES))
|
resp[key] = tpl.async_render(item.get(ATTR_TEMPLATE_VARIABLES))
|
||||||
except TemplateError as ex:
|
except template.TemplateError as ex:
|
||||||
resp[key] = {"error": str(ex)}
|
resp[key] = {"error": str(ex)}
|
||||||
|
|
||||||
return webhook_response(resp, registration=config_entry.data)
|
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."""
|
"""Test that we render templates properly."""
|
||||||
resp = await webhook_client.post(
|
resp = await webhook_client.post(
|
||||||
"/api/webhook/{}".format(create_registrations[1]["webhook_id"]),
|
"/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
|
assert resp.status == 200
|
||||||
|
|
||||||
json = await resp.json()
|
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):
|
async def test_webhook_handle_call_services(hass, create_registrations, webhook_client):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user