diff --git a/appdaemon/plugins/hass/hassapi.py b/appdaemon/plugins/hass/hassapi.py index 6677850c1..075c2c2a2 100644 --- a/appdaemon/plugins/hass/hassapi.py +++ b/appdaemon/plugins/hass/hassapi.py @@ -1343,7 +1343,7 @@ def run_script( # Functions that use self.render_template @utils.sync_decorator - async def render_template(self, template: str, namespace: str | None = None) -> Any: + async def render_template(self, template: str, namespace: str | None = None, **kwargs) -> Any: """Renders a Home Assistant Template. See the documentation for the `Template Integration `__ and @@ -1351,9 +1351,9 @@ async def render_template(self, template: str, namespace: str | None = None) -> Args: template (str): The Home Assistant template to be rendered. - namespace (str, optional): Namespace to use for the call. See the section on - `namespaces `__ for a detailed description. - In most cases it is safe to ignore this parameter. + namespace (str, optional): Optional namespace to use. Defaults to using the app's current namespace. See the + `namespace documentation `__ for more information. + **kwargs (optional): Zero or more keyword arguments that get passed to the template rendering. Returns: The rendered template in a native Python type. @@ -1368,11 +1368,14 @@ async def render_template(self, template: str, namespace: str | None = None) -> >>> self.render_template("{{ states('sensor.outside_temp') }}") 97.2 + >>> self.render_template("hello {{ name }}", variables={"name": "bob"}) + hello bob + """ plugin: "HassPlugin" = self.AD.plugins.get_plugin_object( namespace or self.namespace ) - result = await plugin.render_template(self.namespace, template) + result = await plugin.render_template(self.namespace, template, **kwargs) try: return literal_eval(result) except (SyntaxError, ValueError): diff --git a/appdaemon/plugins/hass/hassplugin.py b/appdaemon/plugins/hass/hassplugin.py index 25e825916..5e968a435 100644 --- a/appdaemon/plugins/hass/hassplugin.py +++ b/appdaemon/plugins/hass/hassplugin.py @@ -888,7 +888,7 @@ async def get_logbook( return result @utils.warning_decorator(error_text='Unexpected error rendering template') - async def render_template(self, namespace: str, template: str): + async def render_template(self, namespace: str, template: str, **kwargs): self.logger.debug( "render_template() namespace=%s data=%s", namespace, @@ -897,4 +897,4 @@ async def render_template(self, namespace: str, template: str): # if we get a request for not our namespace something has gone very wrong assert namespace == self.namespace - return await self.http_method("post", "/api/template", template=template) + return await self.http_method("post", "/api/template", template=template, **kwargs) diff --git a/docs/HISTORY.md b/docs/HISTORY.md index fbca89934..a7a38cfcb 100644 --- a/docs/HISTORY.md +++ b/docs/HISTORY.md @@ -11,6 +11,7 @@ None - Fixed a cosmetic error on admin stream disconnect - Fixed spurious entry in service dictionary causing dashboard `__name`` errors - Added setter for setter for `pin_apps` +- Passing through keyword arguments to `render_template` - Fixed a bug in the error text for apps in a custom app_dir **Breaking Changes**