Add webhook trigger allowed_methods/local_only options (#66494)
Co-authored-by: Franck Nijhof <frenck@frenck.nl> Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
parent
b23cedeae9
commit
94f35ea968
7 changed files with 256 additions and 17 deletions
|
@ -2,6 +2,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
import logging
|
||||
|
||||
from aiohttp import hdrs
|
||||
import voluptuous as vol
|
||||
|
@ -9,17 +10,39 @@ import voluptuous as vol
|
|||
from homeassistant.const import CONF_PLATFORM, CONF_WEBHOOK_ID
|
||||
from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.issue_registry import (
|
||||
IssueSeverity,
|
||||
async_create_issue,
|
||||
async_delete_issue,
|
||||
)
|
||||
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from . import DOMAIN, async_register, async_unregister
|
||||
from . import (
|
||||
DEFAULT_METHODS,
|
||||
DOMAIN,
|
||||
SUPPORTED_METHODS,
|
||||
async_register,
|
||||
async_unregister,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEPENDENCIES = ("webhook",)
|
||||
|
||||
CONF_ALLOWED_METHODS = "allowed_methods"
|
||||
CONF_LOCAL_ONLY = "local_only"
|
||||
|
||||
TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_PLATFORM): "webhook",
|
||||
vol.Required(CONF_WEBHOOK_ID): cv.string,
|
||||
vol.Optional(CONF_ALLOWED_METHODS): vol.All(
|
||||
cv.ensure_list,
|
||||
[vol.All(vol.Upper, vol.In(SUPPORTED_METHODS))],
|
||||
vol.Unique(),
|
||||
),
|
||||
vol.Optional(CONF_LOCAL_ONLY): bool,
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -62,6 +85,32 @@ async def async_attach_trigger(
|
|||
) -> CALLBACK_TYPE:
|
||||
"""Trigger based on incoming webhooks."""
|
||||
webhook_id: str = config[CONF_WEBHOOK_ID]
|
||||
local_only = config.get(CONF_LOCAL_ONLY)
|
||||
issue_id: str | None = None
|
||||
if local_only is None:
|
||||
issue_id = f"trigger_missing_local_only_{webhook_id}"
|
||||
variables = trigger_info["variables"] or {}
|
||||
automation_info = variables.get("this", {})
|
||||
automation_id = automation_info.get("attributes", {}).get("id")
|
||||
automation_entity_id = automation_info.get("entity_id")
|
||||
automation_name = trigger_info.get("name") or automation_entity_id
|
||||
async_create_issue(
|
||||
hass,
|
||||
DOMAIN,
|
||||
issue_id,
|
||||
breaks_in_ha_version="2023.7.0",
|
||||
is_fixable=False,
|
||||
severity=IssueSeverity.WARNING,
|
||||
learn_more_url="https://www.home-assistant.io/docs/automation/trigger/#webhook-trigger",
|
||||
translation_key="trigger_missing_local_only",
|
||||
translation_placeholders={
|
||||
"webhook_id": webhook_id,
|
||||
"automation_name": automation_name,
|
||||
"entity_id": automation_entity_id,
|
||||
"edit": f"/config/automation/edit/{automation_id}",
|
||||
},
|
||||
)
|
||||
allowed_methods = config.get(CONF_ALLOWED_METHODS, DEFAULT_METHODS)
|
||||
job = HassJob(action)
|
||||
|
||||
triggers: dict[str, list[TriggerInstance]] = hass.data.setdefault(
|
||||
|
@ -75,6 +124,8 @@ async def async_attach_trigger(
|
|||
trigger_info["name"],
|
||||
webhook_id,
|
||||
_handle_webhook,
|
||||
local_only=local_only,
|
||||
allowed_methods=allowed_methods,
|
||||
)
|
||||
triggers[webhook_id] = []
|
||||
|
||||
|
@ -84,6 +135,8 @@ async def async_attach_trigger(
|
|||
@callback
|
||||
def unregister():
|
||||
"""Unregister webhook."""
|
||||
if issue_id:
|
||||
async_delete_issue(hass, DOMAIN, issue_id)
|
||||
triggers[webhook_id].remove(trigger_instance)
|
||||
if not triggers[webhook_id]:
|
||||
async_unregister(hass, webhook_id)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue