switch method

This commit is contained in:
J. Nick Koston
2023-03-02 11:34:53 -10:00
parent 9004deefae
commit 0e9e1c8cbe
3 changed files with 14 additions and 9 deletions

View File

@@ -351,7 +351,9 @@ class APIComponentsView(HomeAssistantView):
return self.json(request.app["hass"].config.components)
_cached_template = lru_cache(template.Template)
# Hold a reference to the cached template string to prevent it from being
# garbage collected so the template compile cache can be used.
_cached_template_str = lru_cache(str)
class APITemplateView(HomeAssistantView):
@@ -366,8 +368,9 @@ class APITemplateView(HomeAssistantView):
raise Unauthorized()
try:
data = await request.json()
tpl = _cached_template(data["template"])
tpl.hass = request.app["hass"]
tpl = template.Template(
_cached_template_str(data["template"]), request.app["hass"]
)
return tpl.async_render(variables=data.get("variables"), parse_result=False)
except (ValueError, TemplateError) as ex:
return self.json_message(

View File

@@ -365,7 +365,9 @@ async def webhook_stream_camera(
return webhook_response(resp, registration=config_entry.data)
_cached_template = lru_cache(template.Template)
# Hold a reference to the cached template string to prevent it from being
# garbage collected so the template compile cache can be used.
_cached_template_str = lru_cache(str)
@WEBHOOK_COMMANDS.register("render_template")
@@ -384,8 +386,7 @@ async def webhook_render_template(
resp = {}
for key, item in data.items():
try:
tpl = _cached_template(item[ATTR_TEMPLATE])
tpl.hass = hass
tpl = template.Template(_cached_template_str(item[ATTR_TEMPLATE]), hass)
resp[key] = tpl.async_render(item.get(ATTR_TEMPLATE_VARIABLES))
except TemplateError as ex:
resp[key] = {"error": str(ex)}

View File

@@ -425,7 +425,9 @@ def handle_ping(
connection.send_message(pong_message(msg["id"]))
_cached_template = lru_cache(template.Template)
# Hold a reference to the cached template string to prevent it from being
# garbage collected so the template compile cache can be used.
_cached_template_str = lru_cache(str)
@decorators.websocket_command(
@@ -444,8 +446,7 @@ async def handle_render_template(
) -> None:
"""Handle render_template command."""
template_str = msg["template"]
template_obj = _cached_template(template_str)
template_obj.hass = hass
template_obj = template.Template(_cached_template_str(template_str), hass)
variables = msg.get("variables")
timeout = msg.get("timeout")
info = None