Skip to content

Commit

Permalink
Allow passing keyword arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
extinctpotato committed Nov 11, 2024
1 parent 458ba50 commit 6aeffa3
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions custom_components/zha_toolkit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@
vol.Optional(P.ENDPOINT): vol.Any(cv.byte, [cv.byte]),
vol.Optional(P.CLUSTER): vol.Range(0, 0xFFFF),
vol.Optional(P.MANF): vol.Range(0, 0xFFFF),
vol.Optional(P.KWARGS): dict,
vol.Optional(P.ARGS): vol.Any(
int, list, cv.string
), # Arguments to command
Expand Down
2 changes: 2 additions & 0 deletions custom_components/zha_toolkit/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class USER_PARAMS_consts: # pylint: disable=too-few-public-methods
TRIES = "tries"
EXPECT_REPLY = "expect_reply"
ARGS = "args"
KWARGS = "kwargs"
STATE_ID = "state_id"
STATE_ATTR = "state_attr"
ALLOW_CREATE = "allow_create"
Expand Down Expand Up @@ -105,6 +106,7 @@ class INTERNAL_PARAMS_consts: # pylint: disable=too-few-public-methods
__slots__ = ()
ALLOW_CREATE = "allow_create"
ARGS = "args"
KWARGS = "kwargs"
ATTR_ID = "attr_id"
ATTR_TYPE = "attr_type"
ATTR_VAL = "attr_val"
Expand Down
7 changes: 7 additions & 0 deletions custom_components/zha_toolkit/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2399,6 +2399,13 @@ zcl_cmd:
example: [1, abcd, 3]
selector:
text:
kwargs:
name: Command Keyword Arguments
description:
Keyword arguments for command
example: {"code": "cool_code"}
selector:
text:
tries:
name: Tries
description: Number of times the zigbee packet should be attempted
Expand Down
4 changes: 4 additions & 0 deletions custom_components/zha_toolkit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,7 @@ def extractParams( # noqa: C901
p.TRIES: 1,
p.EXPECT_REPLY: True,
p.ARGS: [],
p.KWARGS: {},
p.STATE_ID: None,
p.STATE_ATTR: None,
p.STATE_VALUE_TEMPLATE: None,
Expand Down Expand Up @@ -978,6 +979,9 @@ def extractParams( # noqa: C901
LOGGER.debug("cmd converted arg %s", lval)
params[p.ARGS] = cmd_args

if P.KWARGS in rawParams:
params[P.KWARGS] = rawParams[P.KWARGS]

if P.MIN_INTRVL in rawParams:
params[p.MIN_INTERVAL] = str2int(rawParams[P.MIN_INTRVL])
if P.MAX_INTRVL in rawParams:
Expand Down
4 changes: 3 additions & 1 deletion custom_components/zha_toolkit/zcl_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ async def zcl_cmd(app, listener, ieee, cmd, data, service, params, event_data):
expect_reply = params[p.EXPECT_REPLY]

cmd_args = params[p.ARGS]
kw_args = params[p.KWARGS]

# Direction 0 = Client to Server, as in protocol bit
is_in_cluster = dir_int == 0
Expand Down Expand Up @@ -120,6 +121,7 @@ async def zcl_cmd(app, listener, ieee, cmd, data, service, params, event_data):
manufacturer=manf,
expect_reply=expect_reply,
tries=tries,
**kw_args
)
else:
if cluster_id not in endpoint.out_clusters:
Expand All @@ -134,7 +136,7 @@ async def zcl_cmd(app, listener, ieee, cmd, data, service, params, event_data):

# Note: client_command not tested
event_data["cmd_reply"] = await cluster.client_command(
cmd_id, *cmd_args, manufacturer=manf
cmd_id, *cmd_args, manufacturer=manf, **kw_args
)
except Exception as e:
caught_e = e
Expand Down

0 comments on commit 6aeffa3

Please sign in to comment.