44
55from collections .abc import Callable
66from dataclasses import dataclass
7- from typing import TYPE_CHECKING , Any
7+ from typing import TYPE_CHECKING , Any , NoReturn
88
99from jinja2 .ext import Extension
1010from jinja2 .nodes import Node
1111from jinja2 .parser import Parser
1212
13+ from homeassistant .exceptions import TemplateError
14+
1315if TYPE_CHECKING :
1416 from homeassistant .core import HomeAssistant
1517 from homeassistant .helpers .template import TemplateEnvironment
@@ -50,8 +52,17 @@ def __init__(
5052 if template_func .requires_hass and self .environment .hass is None :
5153 continue
5254
53- # Skip functions not allowed in limited environments
55+ # Register unsupported stub for functions not allowed in limited environments
5456 if self .environment .limited and not template_func .limited_ok :
57+ unsupported_func = self ._create_unsupported_function (
58+ template_func .name
59+ )
60+ if template_func .as_global :
61+ environment .globals [template_func .name ] = unsupported_func
62+ if template_func .as_filter :
63+ environment .filters [template_func .name ] = unsupported_func
64+ if template_func .as_test :
65+ environment .tests [template_func .name ] = unsupported_func
5566 continue
5667
5768 if template_func .as_global :
@@ -61,6 +72,17 @@ def __init__(
6172 if template_func .as_test :
6273 environment .tests [template_func .name ] = template_func .func
6374
75+ @staticmethod
76+ def _create_unsupported_function (name : str ) -> Callable [[], NoReturn ]:
77+ """Create a function that raises an error for unsupported functions in limited templates."""
78+
79+ def unsupported (* args : Any , ** kwargs : Any ) -> NoReturn :
80+ raise TemplateError (
81+ f"Use of '{ name } ' is not supported in limited templates"
82+ )
83+
84+ return unsupported
85+
6486 @property
6587 def hass (self ) -> HomeAssistant :
6688 """Return the Home Assistant instance.
0 commit comments