core/homeassistant/components/webhook/trigger.py

53 lines
1.4 KiB
Python
Raw Normal View History

"""Offer webhook triggered automation rules."""
from functools import partial
import logging
from aiohttp import hdrs
import voluptuous as vol
from homeassistant.const import CONF_PLATFORM, CONF_WEBHOOK_ID
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
# mypy: allow-untyped-defs
2019-07-31 19:25:30 +00:00
DEPENDENCIES = ("webhook",)
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
TRIGGER_SCHEMA = vol.Schema(
{vol.Required(CONF_PLATFORM): "webhook", vol.Required(CONF_WEBHOOK_ID): cv.string}
)
async def _handle_webhook(action, hass, webhook_id, request):
"""Handle incoming webhook."""
2019-07-31 19:25:30 +00:00
result = {"platform": "webhook", "webhook_id": webhook_id}
2019-07-31 19:25:30 +00:00
if "json" in request.headers.get(hdrs.CONTENT_TYPE, ""):
result["json"] = await request.json()
else:
2019-07-31 19:25:30 +00:00
result["data"] = await request.post()
2019-07-31 19:25:30 +00:00
result["query"] = request.query
result["description"] = "webhook"
2019-07-31 19:25:30 +00:00
hass.async_run_job(action, {"trigger": result})
async def async_attach_trigger(hass, config, action, automation_info):
"""Trigger based on incoming webhooks."""
webhook_id = config.get(CONF_WEBHOOK_ID)
hass.components.webhook.async_register(
2020-08-17 16:54:56 +00:00
automation_info["domain"],
2019-07-31 19:25:30 +00:00
automation_info["name"],
webhook_id,
partial(_handle_webhook, action),
)
@callback
def unregister():
"""Unregister webhook."""
hass.components.webhook.async_unregister(webhook_id)
return unregister