|
4 | 4 |
|
5 | 5 | from .const import LOGGER_PATH
|
6 | 6 |
|
| 7 | +from aiohttp import hdrs |
| 8 | + |
| 9 | +from homeassistant.components import webhook |
| 10 | + |
7 | 11 | _LOGGER = logging.getLogger(LOGGER_PATH + ".webhook")
|
8 | 12 |
|
9 | 13 |
|
@@ -32,45 +36,58 @@ def init(cls, hass):
|
32 | 36 | cls.hass = hass
|
33 | 37 |
|
34 | 38 | @classmethod
|
35 |
| - async def webhook_listener(cls, event): |
| 39 | + async def webhook_handler(cls, hass, webhook_id, request): |
36 | 40 | """Listen callback for given webhook which updates any notifications."""
|
37 | 41 |
|
38 | 42 | func_args = {
|
39 |
| - "trigger_type": "event", |
40 |
| - "event_type": event.event_type, |
41 |
| - "context": event.context, |
| 43 | + "trigger_type": "webhook", |
| 44 | + "webhook_id": webhook_id, |
42 | 45 | }
|
43 |
| - func_args.update(event.data) |
44 |
| - await cls.update(event.event_type, func_args) |
| 46 | + |
| 47 | + if "json" in request.headers.get(hdrs.CONTENT_TYPE, ""): |
| 48 | + func_args["webhook_data"] = await request.json() |
| 49 | + else: |
| 50 | + func_args["webhook_data"] = await request.post() |
| 51 | + |
| 52 | + |
| 53 | + await cls.update(webhook_id, func_args) |
45 | 54 |
|
46 | 55 | @classmethod
|
47 |
| - def notify_add(cls, webhook_type, queue): |
| 56 | + def notify_add(cls, webhook_id, queue): |
48 | 57 | """Register to notify for webhooks of given type to be sent to queue."""
|
49 | 58 |
|
50 |
| - if webhook_type not in cls.notify: |
51 |
| - cls.notify[webhook_type] = set() |
52 |
| - _LOGGER.debug("webhook.notify_add(%s) -> adding webhook listener", webhook_type) |
53 |
| - cls.notify_remove[webhook_type] = cls.hass.bus.async_listen(webhook_type, cls.webhook_listener) |
54 |
| - cls.notify[webhook_type].add(queue) |
| 59 | + if webhook_id not in cls.notify: |
| 60 | + cls.notify[webhook_id] = set() |
| 61 | + _LOGGER.debug("webhook.notify_add(%s) -> adding webhook listener", webhook_id) |
| 62 | + webhook.async_register( |
| 63 | + cls.hass, |
| 64 | + "webhook", |
| 65 | + "my_name", |
| 66 | + webhook_id, |
| 67 | + cls.webhook_handler, |
| 68 | + ) |
| 69 | + cls.notify_remove[webhook_id] = lambda : webhook.async_unregister(cls.hass, webhook_id) |
| 70 | + |
| 71 | + cls.notify[webhook_id].add(queue) |
55 | 72 |
|
56 | 73 | @classmethod
|
57 |
| - def notify_del(cls, webhook_type, queue): |
| 74 | + def notify_del(cls, webhook_id, queue): |
58 | 75 | """Unregister to notify for webhooks of given type for given queue."""
|
59 | 76 |
|
60 |
| - if webhook_type not in cls.notify or queue not in cls.notify[webhook_type]: |
| 77 | + if webhook_id not in cls.notify or queue not in cls.notify[webhook_id]: |
61 | 78 | return
|
62 |
| - cls.notify[webhook_type].discard(queue) |
63 |
| - if len(cls.notify[webhook_type]) == 0: |
64 |
| - cls.notify_remove[webhook_type]() |
65 |
| - _LOGGER.debug("webhook.notify_del(%s) -> removing webhook listener", webhook_type) |
66 |
| - del cls.notify[webhook_type] |
67 |
| - del cls.notify_remove[webhook_type] |
| 79 | + cls.notify[webhook_id].discard(queue) |
| 80 | + if len(cls.notify[webhook_id]) == 0: |
| 81 | + cls.notify_remove[webhook_id]() |
| 82 | + _LOGGER.debug("webhook.notify_del(%s) -> removing webhook listener", webhook_id) |
| 83 | + del cls.notify[webhook_id] |
| 84 | + del cls.notify_remove[webhook_id] |
68 | 85 |
|
69 | 86 | @classmethod
|
70 |
| - async def update(cls, webhook_type, func_args): |
| 87 | + async def update(cls, webhook_id, func_args): |
71 | 88 | """Deliver all notifications for an webhook of the given type."""
|
72 | 89 |
|
73 |
| - _LOGGER.debug("webhook.update(%s, %s)", webhook_type, func_args) |
74 |
| - if webhook_type in cls.notify: |
75 |
| - for queue in cls.notify[webhook_type]: |
| 90 | + _LOGGER.debug("webhook.update(%s, %s)", webhook_id, func_args) |
| 91 | + if webhook_id in cls.notify: |
| 92 | + for queue in cls.notify[webhook_id]: |
76 | 93 | await queue.put(["webhook", func_args.copy()])
|
0 commit comments