From 0e9e1c8cbe8753159f4fd6775cdc9cf217d66f0e Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 2 Mar 2023 11:34:53 -1000 Subject: [PATCH] switch method --- homeassistant/components/api/__init__.py | 9 ++++++--- homeassistant/components/mobile_app/webhook.py | 7 ++++--- homeassistant/components/websocket_api/commands.py | 7 ++++--- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/api/__init__.py b/homeassistant/components/api/__init__.py index 2a67308a0b8..ad458d32a94 100644 --- a/homeassistant/components/api/__init__.py +++ b/homeassistant/components/api/__init__.py @@ -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( diff --git a/homeassistant/components/mobile_app/webhook.py b/homeassistant/components/mobile_app/webhook.py index 648efea9d30..ad8a75f2198 100644 --- a/homeassistant/components/mobile_app/webhook.py +++ b/homeassistant/components/mobile_app/webhook.py @@ -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)} diff --git a/homeassistant/components/websocket_api/commands.py b/homeassistant/components/websocket_api/commands.py index b002c834f01..6696c81b704 100644 --- a/homeassistant/components/websocket_api/commands.py +++ b/homeassistant/components/websocket_api/commands.py @@ -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