Skip to content

Commit 2af0ce4

Browse files
committed
Fixed wait_until, added documentation
1 parent ecc9c47 commit 2af0ce4

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

custom_components/pyscript/trigger.py

+6
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ async def wait_until(
224224
event_trigger=None,
225225
mqtt_trigger=None,
226226
webhook_trigger=None,
227+
webhook_local_only=True,
228+
webhook_methods=None,
227229
timeout=None,
228230
state_hold=None,
229231
state_hold_false=None,
@@ -374,6 +376,10 @@ async def wait_until(
374376
if len(state_trig_ident) > 0:
375377
State.notify_del(state_trig_ident, notify_q)
376378
raise exc
379+
if webhook_methods is None:
380+
webhook_methods = {"POST", "PUT"}
381+
Webhook.notify_add(webhook_trigger[0], webhook_local_only, webhook_methods, notify_q)
382+
377383
time0 = time.monotonic()
378384

379385
if __test_handshake__:

docs/reference.rst

+48-2
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ Wildcards in topics are supported. The ``topic`` variables will be set to the fu
787787
the message arrived on.
788788

789789
NOTE: The `MQTT Integration in Home Assistant <https://www.home-assistant.io/integrations/mqtt/>`__
790-
must be set up to use ``@mqtt_trigger``.
790+
must be set up to use ``@mqtt_trigger``.
791791

792792
@state_active
793793
^^^^^^^^^^^^^
@@ -857,6 +857,45 @@ true if the current time doesn't match any of the "not" (negative) specification
857857
allows multiple arguments with and without ``not``. The condition will be met if the current time
858858
matches any of the positive arguments, and none of the negative arguments.
859859

860+
@webhook_trigger
861+
^^^^^^^^^^^^^^^^
862+
863+
.. code:: python
864+
865+
@webhook_trigger(webhook_id, str_expr=None, local_only=True, methods={"POST", "PUT", kwargs=None)
866+
867+
``@webhook_trigger`` listens for calls to a `Home Assistant webhook <https://www.home-assistant.io/docs/automation/trigger/#webhook-trigger>`__
868+
at `your_hass_url/api/webhook/webhook_id` and triggers whenever a request is made at that endpoint.
869+
Multiple ``@webhook_trigger`` decorators can be applied to a single function if you want
870+
to trigger off different mqtt topics.
871+
872+
An optional ``str_expr`` can be used to match against payload message data, and the trigger will only occur
873+
if that expression evaluates to ``True`` or non-zero. This expression has available these four
874+
variables:
875+
876+
Setting `local_only` option to `False` will allow request made from anywhere on the internet (as opposed to just locally).
877+
The methods option needs to be an list or set with elements `GET`, `HEAD`, `POST`, or `PUT`.
878+
879+
- ``trigger_type`` is set to "mqtt"
880+
- ``webhook_id`` is set to the webhook_id that was called.
881+
- ``webhook_data`` is the data/json that was sent in the request returned as a `MultiDictProxy <https://aiohttp-kxepal-test.readthedocs.io/en/latest/multidict.html#aiohttp.MultiDictProxy>`__ (ie a python dictionary that allows multiple of the same keys).
882+
883+
When the ``@webhook_trigger`` occurs, those same variables are passed as keyword arguments to the
884+
function in case it needs them. Additional keyword parameters can be specified by setting the
885+
optional ``kwargs`` argument to a ``dict`` with the keywords and values.
886+
887+
An simple example looks like
888+
889+
.. code:: python
890+
891+
@webhook_trigger("myid", kwargs={"extra": 10})
892+
def webhook_test(webhook_data, extra):
893+
log.info(f"It ran! {webhook_data}, {extra}")
894+
895+
which if called using the curl command `curl -X POST -d 'key1=xyz&key2=abc' hass_url/api/webhook/myid
896+
bhook` outputs `It ran! <MultiDictProxy('key1': 'xyz', 'key2': 'abc')>, 10`
897+
898+
860899
Other Function Decorators
861900
-------------------------
862901
@@ -1313,6 +1352,13 @@ It takes the following keyword arguments (all are optional):
13131352
- ``mqtt_trigger=None`` can be set to a string or list of two strings, just like
13141353
``@mqtt_trigger``. The first string is the MQTT topic, and the second string
13151354
(when the setting is a two-element list) is an expression based on the message variables.
1355+
- ``webhook_trigger=None`` can be set to a string or list of two strings, just like
1356+
``@webhook_trigger``. The first string is the webhook id, and the second string
1357+
(when the setting is a two-element list) is an expression based on the message variables.
1358+
- ``webhook_local_only=True`` is used with ``webhook_trigger`` to specify whether to only allow
1359+
local webhooks.
1360+
- ``webhook_methods={"POST", "PUT"}`` is used with ``webhook_trigger`` to specify allowed webhook
1361+
request methods.
13161362
- ``timeout=None`` an overall timeout in seconds, which can be floating point.
13171363
- ``state_check_now=True`` if set, ``task.wait_until()`` checks any ``state_trigger``
13181364
immediately to see if it is already ``True``, and will return immediately if so. If
@@ -1566,7 +1612,7 @@ Pyscript supports importing two types of packages or modules:
15661612
will cause all of the module files to be unloaded, and any scripts or apps that import that module
15671613
will be reloaded. Imports of pyscript modules and packages are not affected by the ``allow_all_imports``
15681614
setting - if a file is in the ``<config>/pyscript/modules`` folder then it can be imported.
1569-
1615+
15701616
Package-style layout is also supported where a PACKAGE is defined in
15711617
``<config>/pyscript/modules/PACKAGE/__init__.py``, and that file can, in turn,
15721618
do relative imports of other files in that same directory. This form is most convenient for

0 commit comments

Comments
 (0)