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) 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): class APITemplateView(HomeAssistantView):
@@ -366,8 +368,9 @@ class APITemplateView(HomeAssistantView):
raise Unauthorized() raise Unauthorized()
try: try:
data = await request.json() data = await request.json()
tpl = _cached_template(data["template"]) tpl = template.Template(
tpl.hass = request.app["hass"] _cached_template_str(data["template"]), request.app["hass"]
)
return tpl.async_render(variables=data.get("variables"), parse_result=False) return tpl.async_render(variables=data.get("variables"), parse_result=False)
except (ValueError, TemplateError) as ex: except (ValueError, TemplateError) as ex:
return self.json_message( return self.json_message(

View File

@@ -365,7 +365,9 @@ async def webhook_stream_camera(
return webhook_response(resp, registration=config_entry.data) 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") @WEBHOOK_COMMANDS.register("render_template")
@@ -384,8 +386,7 @@ async def webhook_render_template(
resp = {} resp = {}
for key, item in data.items(): for key, item in data.items():
try: try:
tpl = _cached_template(item[ATTR_TEMPLATE]) tpl = template.Template(_cached_template_str(item[ATTR_TEMPLATE]), hass)
tpl.hass = hass
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 TemplateError as ex:
resp[key] = {"error": str(ex)} resp[key] = {"error": str(ex)}

View File

@@ -425,7 +425,9 @@ def handle_ping(
connection.send_message(pong_message(msg["id"])) 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( @decorators.websocket_command(
@@ -444,8 +446,7 @@ async def handle_render_template(
) -> None: ) -> None:
"""Handle render_template command.""" """Handle render_template command."""
template_str = msg["template"] template_str = msg["template"]
template_obj = _cached_template(template_str) template_obj = template.Template(_cached_template_str(template_str), hass)
template_obj.hass = hass
variables = msg.get("variables") variables = msg.get("variables")
timeout = msg.get("timeout") timeout = msg.get("timeout")
info = None info = None