You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
allow @webhook_trigger functions to control the http response
Webhook handlers now wait for the decorated function and use its return
value to build the response: int -> Response(status=...), aiohttp.web.Response
-> as-is, None -> default 200. When multiple triggers share a webhook_id,
the first non-None return wins.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
"""Trigger when a request is made to a webhook endpoint.
@@ -136,6 +137,7 @@ def webhook_trigger(
136
137
str_expr: Optional expression evaluated against ``trigger_type``, ``webhook_id``, ``request``, and ``payload``.
137
138
local_only: If False, allow requests from anywhere on the internet.
138
139
methods: HTTP methods to allow.
140
+
sets_http_response_code: If True, the function's return value drives the HTTP response (``int`` status code or ``aiohttp.web.Response``); at most one trigger per ``webhook_id`` may set this.
139
141
kwargs: Extra keyword arguments merged into each invocation.
140
142
141
143
Trigger kwargs include ``trigger_type="webhook"``, ``webhook_id``, the parsed payload fields, and ``request`` (the underlying ``aiohttp.web.Request``).
Copy file name to clipboardExpand all lines: docs/reference.rst
+12Lines changed: 12 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -915,6 +915,18 @@ To validate an HMAC signature on incoming requests, declare ``request`` in the f
915
915
return
916
916
log.info(f"verified webhook: {payload}")
917
917
918
+
To control the HTTP response sent back to the webhook caller, opt in by passing ``sets_http_response_code=True``. The flagged function's return value then drives the response: ``None`` produces a ``200 OK``, an ``int`` sends back a response with that status code, and an ``aiohttp.web.Response`` allows full control over the body and headers. Return values from triggers without the flag are ignored. For example:
At most one ``@webhook_trigger`` per ``webhook_id`` may set ``sets_http_response_code=True``; declaring more than one is an error at setup time. The webhook handler waits for all decorated function(s) for the ``webhook_id`` to finish before responding, so use ``task.create()`` to fire-and-forget any long-running work.
929
+
918
930
NOTE: A webhook_id can only be used by either a built-in Home Assistant automation or pyscript, but not both. Trying to use the same webhook_id in both will result in an error.
0 commit comments