Skip to content

Commit 2dfda6a

Browse files
committed
add payload_obj to list of optional trigger kwargs; fixed unexpected keyword arg exception; fixes #170
1 parent 810db11 commit 2dfda6a

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

custom_components/pyscript/eval.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"event_type",
6666
"old_value",
6767
"payload",
68+
"payload_obj",
6869
"qos",
6970
"topic",
7071
"trigger_type",
@@ -705,9 +706,8 @@ async def call(self, ast_ctx, *args, **kwargs):
705706
# don't raise an exception for extra trigger keyword parameters;
706707
# it's difficult to apply this exception to just trigger functions
707708
# since they could have non-trigger decorators too
708-
raise TypeError(
709-
f"{self.name}() called with unexpected keyword arguments: {', '.join(sorted(kwargs.keys()))}"
710-
)
709+
unexpected = ", ".join(sorted(set(kwargs.keys()) - TRIGGER_KWARGS))
710+
raise TypeError(f"{self.name}() called with unexpected keyword arguments: {unexpected}")
711711
if self.func_def.args.vararg:
712712
if len(args) > len(self.func_def.args.args):
713713
sym_table[self.func_def.args.vararg.arg] = tuple(args[len(self.func_def.args.args) :])

docs/reference.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ variables:
749749
- ``payload`` is set to the string payload of the message
750750
- ``payload_obj`` if the payload was valid JSON, this will be set to the native python object
751751
representing that payload.
752+
- ``qos`` is set to the message QoS.
752753

753754
When the ``@mqtt_trigger`` occurs, those same variables are passed as keyword arguments to the
754755
function in case it needs them. Additional keyword parameters can be specified by setting the

tests/test_unit_eval.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,15 @@ async def test_eval(hass):
14271427
"def func(*a, b):\n pass\nfunc(1, 2)",
14281428
"Exception in test line 3 column 8: func() missing required keyword-only arguments",
14291429
],
1430+
[
1431+
"""
1432+
def func(b=1):
1433+
pass
1434+
1435+
func(a=2, trigger_type=1)
1436+
""",
1437+
"Exception in test line 5 column 23: func() called with unexpected keyword arguments: a",
1438+
],
14301439
[
14311440
"from .xyz import abc",
14321441
"Exception in test line 1 column 0: attempted relative import with no known parent package",

0 commit comments

Comments
 (0)