Skip to content

Commit ffa3d57

Browse files
committed
Got args/kwargs working
1 parent d0d1636 commit ffa3d57

File tree

5 files changed

+43
-22
lines changed

5 files changed

+43
-22
lines changed

custom_components/pyscript/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121
EVENT_STATE_CHANGED,
2222
SERVICE_RELOAD,
2323
)
24-
from homeassistant.core import Config, HomeAssistant, ServiceCall
24+
from homeassistant.core import Config, Event as HAEvent, HomeAssistant, ServiceCall
2525
from homeassistant.exceptions import HomeAssistantError
2626
import homeassistant.helpers.config_validation as cv
27-
from homeassistant.core import Event as HAEvent
2827
from homeassistant.helpers.restore_state import DATA_RESTORE_STATE
2928
from homeassistant.loader import bind_hass
3029

custom_components/pyscript/entity.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
"""Entity Classes"""
1+
"""Entity Classes."""
2+
from homeassistant.const import STATE_UNKNOWN
23
from homeassistant.helpers.restore_state import RestoreEntity
34
from homeassistant.helpers.typing import StateType
4-
from homeassistant.const import STATE_UNKNOWN
55

66

77
class PyscriptEntity(RestoreEntity):
8-
"""Generic Pyscript Entity"""
8+
"""Generic Pyscript Entity."""
99

1010
_attr_extra_state_attributes: dict
1111
_attr_state: StateType = STATE_UNKNOWN
1212

1313
def set_state(self, state):
14-
"""Set the state"""
14+
"""Set the state."""
1515
self._attr_state = state
1616

1717
def set_attributes(self, attributes):
18-
"""Set Attributes"""
18+
"""Set Attributes."""
1919
self._attr_extra_state_attributes = attributes

custom_components/pyscript/eval.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,7 @@ async def trigger_init(self, trig_ctx, func_name):
377377
"task_unique": {"arg_cnt": {1, 2}},
378378
"time_active": {"arg_cnt": {"*"}},
379379
"time_trigger": {"arg_cnt": {0, "*"}, "rep_ok": True},
380-
# TODO: Add in functionality to webhook with arguments
381-
"webhook_trigger": {"arg_cnt": {1, 2, 3}, "rep_ok": True},
380+
"webhook_trigger": {"arg_cnt": {1, 2}, "rep_ok": True},
382381
}
383382
kwarg_check = {
384383
"event_trigger": {"kwargs": {dict}},
@@ -394,8 +393,11 @@ async def trigger_init(self, trig_ctx, func_name):
394393
"state_hold_false": {int, float},
395394
"watch": {set, list},
396395
},
397-
# "webhook_trigger": {"call": {str, list}, "local": bool},
398-
"webhook_trigger": {"kwargs": {dict}},
396+
"webhook_trigger": {
397+
"kwargs": {dict},
398+
"local_only": {bool},
399+
"methods": {list, set},
400+
},
399401
}
400402

401403
for dec in self.decorators:

custom_components/pyscript/trigger.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,13 @@ async def wait_until(
230230
__test_handshake__=None,
231231
):
232232
"""Wait for zero or more triggers, until an optional timeout."""
233-
if state_trigger is None and time_trigger is None and event_trigger is None and mqtt_trigger is None and webhook_trigger is None:
233+
if (
234+
state_trigger is None
235+
and time_trigger is None
236+
and event_trigger is None
237+
and mqtt_trigger is None
238+
and webhook_trigger is None
239+
):
234240
if timeout is not None:
235241
await asyncio.sleep(timeout)
236242
return {"trigger_type": "timeout"}
@@ -413,7 +419,12 @@ async def wait_until(
413419
state_trig_timeout = True
414420
time_next = now + dt.timedelta(seconds=this_timeout)
415421
if this_timeout is None:
416-
if state_trigger is None and event_trigger is None and mqtt_trigger is None and webhook_trigger is None:
422+
if (
423+
state_trigger is None
424+
and event_trigger is None
425+
and mqtt_trigger is None
426+
and webhook_trigger is None
427+
):
417428
_LOGGER.debug(
418429
"trigger %s wait_until no next time - returning with none",
419430
ast_ctx.name,
@@ -860,6 +871,8 @@ def __init__(
860871
self.mqtt_trigger_kwargs = trig_cfg.get("mqtt_trigger", {}).get("kwargs", {})
861872
self.webhook_trigger = trig_cfg.get("webhook_trigger", {}).get("args", None)
862873
self.webhook_trigger_kwargs = trig_cfg.get("webhook_trigger", {}).get("kwargs", {})
874+
self.webhook_local_only = self.webhook_trigger_kwargs.get("local_only", True)
875+
self.webhook_methods = self.webhook_trigger_kwargs.get("methods", {"POST", "PUT"})
863876
self.state_active = trig_cfg.get("state_active", {}).get("args", None)
864877
self.time_active = trig_cfg.get("time_active", {}).get("args", None)
865878
self.time_active_hold_off = trig_cfg.get("time_active", {}).get("kwargs", {}).get("hold_off", None)
@@ -1049,7 +1062,9 @@ async def trigger_watch(self):
10491062
await Mqtt.notify_add(self.mqtt_trigger[0], self.notify_q)
10501063
if self.webhook_trigger is not None:
10511064
_LOGGER.debug("trigger %s adding webhook_trigger %s", self.name, self.webhook_trigger[0])
1052-
Webhook.notify_add(self.webhook_trigger[0], self.notify_q)
1065+
Webhook.notify_add(
1066+
self.webhook_trigger[0], self.webhook_local_only, self.webhook_methods, self.notify_q
1067+
)
10531068

10541069
last_trig_time = None
10551070
last_state_trig_time = None
@@ -1237,6 +1252,11 @@ async def trigger_watch(self):
12371252
user_kwargs = self.mqtt_trigger_kwargs.get("kwargs", {})
12381253
if self.mqtt_trig_expr:
12391254
trig_ok = await self.mqtt_trig_expr.eval(notify_info)
1255+
elif notify_type == "webhook":
1256+
func_args = notify_info
1257+
user_kwargs = self.webhook_trigger_kwargs.get("kwargs", {})
1258+
if self.webhook_trig_expr:
1259+
trig_ok = await self.webhook_trig_expr.eval(notify_info)
12401260

12411261
else:
12421262
user_kwargs = self.time_trigger_kwargs.get("kwargs", {})

custom_components/pyscript/webhook.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import logging
44

5-
from .const import LOGGER_PATH
6-
75
from aiohttp import hdrs
86

97
from homeassistant.components import webhook
108

9+
from .const import LOGGER_PATH
10+
1111
_LOGGER = logging.getLogger(LOGGER_PATH + ".webhook")
1212

1313

@@ -49,24 +49,24 @@ async def webhook_handler(cls, hass, webhook_id, request):
4949
else:
5050
func_args["webhook_data"] = await request.post()
5151

52-
5352
await cls.update(webhook_id, func_args)
5453

5554
@classmethod
56-
def notify_add(cls, webhook_id, queue):
55+
def notify_add(cls, webhook_id, local_only, methods, queue):
5756
"""Register to notify for webhooks of given type to be sent to queue."""
58-
5957
if webhook_id not in cls.notify:
6058
cls.notify[webhook_id] = set()
6159
_LOGGER.debug("webhook.notify_add(%s) -> adding webhook listener", webhook_id)
6260
webhook.async_register(
6361
cls.hass,
64-
"webhook",
65-
"my_name",
62+
"webhook", # DOMAIN - unclear what this is used for
63+
"pyscript", # NAME - unclear what this is used for
6664
webhook_id,
6765
cls.webhook_handler,
66+
local_only=local_only,
67+
allowed_methods=methods,
6868
)
69-
cls.notify_remove[webhook_id] = lambda : webhook.async_unregister(cls.hass, webhook_id)
69+
cls.notify_remove[webhook_id] = lambda: webhook.async_unregister(cls.hass, webhook_id)
7070

7171
cls.notify[webhook_id].add(queue)
7272

0 commit comments

Comments
 (0)