Skip to content

Commit d0d1636

Browse files
committed
Got webhook trigger functioning!
1 parent 99ea648 commit d0d1636

File tree

2 files changed

+43
-24
lines changed

2 files changed

+43
-24
lines changed

custom_components/pyscript/eval.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@
7575
"trigger_time",
7676
"var_name",
7777
"value",
78+
"webhook_id",
79+
"webhook_data",
7880
}
7981

8082

custom_components/pyscript/webhook.py

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
from .const import LOGGER_PATH
66

7+
from aiohttp import hdrs
8+
9+
from homeassistant.components import webhook
10+
711
_LOGGER = logging.getLogger(LOGGER_PATH + ".webhook")
812

913

@@ -32,45 +36,58 @@ def init(cls, hass):
3236
cls.hass = hass
3337

3438
@classmethod
35-
async def webhook_listener(cls, event):
39+
async def webhook_handler(cls, hass, webhook_id, request):
3640
"""Listen callback for given webhook which updates any notifications."""
3741

3842
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,
4245
}
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)
4554

4655
@classmethod
47-
def notify_add(cls, webhook_type, queue):
56+
def notify_add(cls, webhook_id, queue):
4857
"""Register to notify for webhooks of given type to be sent to queue."""
4958

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)
5572

5673
@classmethod
57-
def notify_del(cls, webhook_type, queue):
74+
def notify_del(cls, webhook_id, queue):
5875
"""Unregister to notify for webhooks of given type for given queue."""
5976

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]:
6178
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]
6885

6986
@classmethod
70-
async def update(cls, webhook_type, func_args):
87+
async def update(cls, webhook_id, func_args):
7188
"""Deliver all notifications for an webhook of the given type."""
7289

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]:
7693
await queue.put(["webhook", func_args.copy()])

0 commit comments

Comments
 (0)