Files
homeassistant-core/homeassistant/components/webhook/trigger.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

119 lines
3.3 KiB
Python
Raw Normal View History

2019-02-13 21:21:14 +01:00
"""Offer webhook triggered automation rules."""
from __future__ import annotations
from dataclasses import dataclass
import logging
2024-01-13 20:08:26 +01:00
from typing import Any
2018-10-08 20:16:37 +02:00
2024-01-13 20:08:26 +01:00
from aiohttp import hdrs, web
2018-10-08 20:16:37 +02:00
import voluptuous as vol
from homeassistant.const import CONF_PLATFORM, CONF_WEBHOOK_ID
from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback
2018-10-08 20:16:37 +02:00
import homeassistant.helpers.config_validation as cv
2022-08-15 18:15:20 +02:00
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType
2018-10-08 20:16:37 +02:00
from . import (
DEFAULT_METHODS,
DOMAIN,
SUPPORTED_METHODS,
async_register,
async_unregister,
)
_LOGGER = logging.getLogger(__name__)
2022-01-14 12:31:02 +01:00
2018-10-08 20:16:37 +02:00
DEPENDENCIES = ("webhook",)
CONF_ALLOWED_METHODS = "allowed_methods"
CONF_LOCAL_ONLY = "local_only"
2021-06-11 09:51:12 +02:00
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,
2021-06-11 09:51:12 +02:00
}
2018-10-08 20:16:37 +02:00
)
WEBHOOK_TRIGGERS = f"{DOMAIN}_triggers"
2018-10-08 20:16:37 +02:00
@dataclass(slots=True)
class TriggerInstance:
"""Attached trigger settings."""
2022-08-15 18:15:20 +02:00
trigger_info: TriggerInfo
job: HassJob
2024-01-13 20:08:26 +01:00
async def _handle_webhook(
hass: HomeAssistant, webhook_id: str, request: web.Request
) -> None:
2018-10-08 20:16:37 +02:00
"""Handle incoming webhook."""
2024-01-13 20:08:26 +01:00
base_result: dict[str, Any] = {"platform": "webhook", "webhook_id": webhook_id}
2018-10-08 20:16:37 +02:00
if "json" in request.headers.get(hdrs.CONTENT_TYPE, ""):
base_result["json"] = await request.json()
2018-10-08 20:16:37 +02:00
else:
base_result["data"] = await request.post()
2018-10-08 20:16:37 +02:00
base_result["query"] = request.query
base_result["description"] = "webhook"
triggers: dict[str, list[TriggerInstance]] = hass.data.setdefault(
WEBHOOK_TRIGGERS, {}
)
for trigger in triggers[webhook_id]:
2022-08-15 18:15:20 +02:00
result = {**base_result, **trigger.trigger_info["trigger_data"]}
hass.async_run_hass_job(trigger.job, {"trigger": result})
2018-10-08 20:16:37 +02:00
async def async_attach_trigger(
hass: HomeAssistant,
config: ConfigType,
2022-08-15 18:15:20 +02:00
action: TriggerActionType,
trigger_info: TriggerInfo,
) -> CALLBACK_TYPE:
2018-10-08 20:16:37 +02:00
"""Trigger based on incoming webhooks."""
webhook_id: str = config[CONF_WEBHOOK_ID]
local_only = config.get(CONF_LOCAL_ONLY, True)
allowed_methods = config.get(CONF_ALLOWED_METHODS, DEFAULT_METHODS)
2020-10-08 02:44:34 -05:00
job = HassJob(action)
triggers: dict[str, list[TriggerInstance]] = hass.data.setdefault(
WEBHOOK_TRIGGERS, {}
2018-10-08 20:16:37 +02:00
)
if webhook_id not in triggers:
async_register(
hass,
2022-08-15 18:15:20 +02:00
trigger_info["domain"],
trigger_info["name"],
webhook_id,
_handle_webhook,
local_only=local_only,
allowed_methods=allowed_methods,
)
triggers[webhook_id] = []
2022-08-15 18:15:20 +02:00
trigger_instance = TriggerInstance(trigger_info, job)
triggers[webhook_id].append(trigger_instance)
2018-10-08 20:16:37 +02:00
@callback
2024-01-13 20:08:26 +01:00
def unregister() -> None:
2018-10-08 20:16:37 +02:00
"""Unregister webhook."""
triggers[webhook_id].remove(trigger_instance)
if not triggers[webhook_id]:
async_unregister(hass, webhook_id)
triggers.pop(webhook_id)
2018-10-08 20:16:37 +02:00
return unregister