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