Abort execution of template renders that overwhelm the system (#40647)

This commit is contained in:
J. Nick Koston 2020-09-28 07:43:22 -05:00 committed by GitHub
parent 8bc62f3678
commit e08ee282ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 212 additions and 15 deletions

View file

@ -239,22 +239,32 @@ def handle_ping(hass, connection, msg):
connection.send_message(pong_message(msg["id"]))
@callback
@decorators.websocket_command(
{
vol.Required("type"): "render_template",
vol.Required("template"): str,
vol.Optional("entity_ids"): cv.entity_ids,
vol.Optional("variables"): dict,
vol.Optional("timeout"): vol.Coerce(float),
}
)
def handle_render_template(hass, connection, msg):
@decorators.async_response
async def handle_render_template(hass, connection, msg):
"""Handle render_template command."""
template_str = msg["template"]
template = Template(template_str, hass)
variables = msg.get("variables")
timeout = msg.get("timeout")
info = None
if timeout and await template.async_render_will_timeout(timeout):
connection.send_error(
msg["id"],
const.ERR_TEMPLATE_ERROR,
f"Exceeded maximum execution time of {timeout}s",
)
return
@callback
def _template_listener(event, updates):
nonlocal info