Allow full templates in the webhook trigger webhook_id (#187023)

This commit is contained in:
Franck Nijhof
2026-10-18 21:45:03 -04:00
committed by GitHub
parent a4f4ad0b0a
commit 29c64760cc
2 changed files with 151 additions and 3 deletions
+1 -3
View File
@@ -89,9 +89,7 @@ async def async_attach_trigger(
if trigger_info:
variables = trigger_info.get("variables")
webhook_id_template: Template = config[CONF_WEBHOOK_ID]
webhook_id: str = webhook_id_template.async_render(
variables, limited=True, parse_result=False
)
webhook_id: str = webhook_id_template.async_render(variables, parse_result=False)
local_only = config.get(CONF_LOCAL_ONLY, True)
allowed_methods = config.get(CONF_ALLOWED_METHODS, DEFAULT_METHODS)
job = HassJob(action)
+150
View File
@@ -1,5 +1,7 @@
"""The tests for the webhook automation trigger."""
import asyncio
import hashlib
from ipaddress import ip_address
from unittest.mock import Mock, patch
@@ -376,6 +378,154 @@ async def test_webhook_template(
assert events[0].data["hello"] == "yo world"
async def test_webhook_template_uses_state(
hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator
) -> None:
"""Test a template webhook ID is rendered with states, once at setup."""
events = async_capture_events(hass, "test_success")
hass.states.async_set("input_text.hook", "first")
assert await async_setup_component(
hass,
"automation",
{
"automation": {
"id": "hook",
"alias": "hook",
"trigger": {
"platform": "webhook",
"webhook_id": "webhook-{{ states('input_text.hook') }}",
"local_only": True,
},
"action": {"event": "test_success"},
}
},
)
await hass.async_block_till_done()
client = await hass_client_no_auth()
await client.post("/api/webhook/webhook-first")
await client.post("/api/webhook/webhook-other")
await hass.async_block_till_done()
assert len(events) == 1
# The webhook ID is only rendered when the trigger is set up, so a state
# change doesn't move the webhook
hass.states.async_set("input_text.hook", "second")
await hass.async_block_till_done()
await client.post("/api/webhook/webhook-first")
await client.post("/api/webhook/webhook-second")
await hass.async_block_till_done()
assert len(events) == 2
assert "webhook-first" in hass.data[DOMAIN]
assert "webhook-second" not in hass.data[DOMAIN]
# Setting up the trigger again renders the new webhook ID
await hass.services.async_call(
"automation", "turn_off", {"entity_id": "automation.hook"}, blocking=True
)
await hass.services.async_call(
"automation", "turn_on", {"entity_id": "automation.hook"}, blocking=True
)
await hass.async_block_till_done()
assert "webhook-first" not in hass.data[DOMAIN]
assert "webhook-second" in hass.data[DOMAIN]
await client.post("/api/webhook/webhook-first")
await client.post("/api/webhook/webhook-second")
await hass.async_block_till_done()
assert len(events) == 3
async def test_webhook_template_hash(
hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator
) -> None:
"""Test a template webhook ID can use hash functions."""
events = async_capture_events(hass, "test_success")
webhook_id = hashlib.sha256(b"my-hook").hexdigest()
assert await async_setup_component(
hass,
"automation",
{
"automation": {
"trigger": {
"platform": "webhook",
"webhook_id": "{{ sha256('my-hook') }}",
"local_only": True,
},
"action": {"event": "test_success"},
}
},
)
await hass.async_block_till_done()
client = await hass_client_no_auth()
await client.post(f"/api/webhook/{webhook_id}")
await hass.async_block_till_done()
assert len(events) == 1
async def test_webhook_template_wait_for_trigger(
hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator
) -> None:
"""Test a template webhook ID in a wait is rendered with variables and states."""
events = async_capture_events(hass, "test_success")
hass.states.async_set("input_text.hook", "state")
assert await async_setup_component(
hass,
"automation",
{
"automation": {
"trigger": {"platform": "event", "event_type": "test_start"},
"action": [
{"variables": {"prefix": "{{ trigger.event.data.prefix }}"}},
{
"wait_for_trigger": {
"platform": "webhook",
"webhook_id": (
"{{ prefix }}-{{ states('input_text.hook') }}"
),
"local_only": True,
},
},
{
"event": "test_success",
"event_data": {"webhook_id": "{{ wait.trigger.webhook_id }}"},
},
],
}
},
)
await hass.async_block_till_done()
async def webhook_registered() -> None:
while "run-state" not in hass.data.get(DOMAIN, {}):
await asyncio.sleep(0)
# Can't block till done while the action is waiting
hass.bus.async_fire("test_start", {"prefix": "run"})
await asyncio.wait_for(webhook_registered(), 1)
client = await hass_client_no_auth()
await client.post("/api/webhook/run-state")
await hass.async_block_till_done()
assert len(events) == 1
assert events[0].data["webhook_id"] == "run-state"
assert "run-state" not in hass.data[DOMAIN]
async def test_webhook_query_json_header_no_payload(
hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator
) -> None: