Skip to content

Commit a484d94

Browse files
author
andzeil
committed
Implemented dynamic parameters in MQTT payloads
1 parent 03d36f0 commit a484d94

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Version 0.1.2
2+
[Chore] Updated depencies and build tooling
3+
[Feature] Implemented dynamic parameters in MQTT payloads
4+
15
# Version 0.1.1
26
[Fix] Makefile fixed to build language files and icons within the build process
37
[Feature] If the user wants SSL/TLS but doesn't provide a server certificate, try to fetch it automatically from the server

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ column. Should you need to add some command line parameters to the executable yo
6969
**Command line arguments** column. Look into the chapter [Integrating with Home Assistant](#integrating-with-home-assistant)
7070
for some example.
7171

72+
Since **Version 0.1.2** there's a new feature for Power Users: It's parameters. With parameters, you can easily pass dynamic
73+
command line arguments to the command you want to run. The process is quite straight-forward:
74+
75+
Set up the MQTT Payload like this: `{"command":"notify","args":"test", "param1":"test1", "param2":"test2"}`.
76+
77+
You can then use `$X` in the **Command line arguments** column. `$X` would be replaced by
78+
the content of `paramX` within the payload. In our example if you put `$1 $2` into the **Command line arguments** column it
79+
will be replaced with `test1 test2` once the above MQTT payload is received.
80+
81+
Should you have, e.g. `$3` defined in the **Command line arguments**, but the MQTT payload does not contain `param3`, `
82+
$3` will be removed from the command line arguments automatically, in order to not be passed to the command you want to run.
83+
7284
## Logs tab
7385

7486
![Logs](docs/assets/logs-tab.png)

src/easy_mqtt_handler/util/MQTTWorkerThread.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import gettext
1111
import json
1212
import os
13+
import re
1314
from ssl import SSLError, SSLZeroReturnError
1415

1516
from PyQt5.QtCore import QThread, pyqtSignal
@@ -89,12 +90,26 @@ def router(self, data):
8990
mqtt_command = data.get("command")
9091
mqtt_arg = data.get("args")
9192

93+
# gather parameters delivered by the MQTT payload
94+
i = 1
95+
mqtt_params = []
96+
while data.get("param" + str(i)) is not None:
97+
mqtt_params.append(data.get("param"+(str(i))))
98+
i += 1
99+
92100
if mqtt_command is not None:
93101
self.add_log_line.emit(_("Received command \"{0}\" with argument \"{1}\".").format(mqtt_command, mqtt_arg))
94102

95103
command_to_run = find_command_to_run(mqtt_command, mqtt_arg)
96104
command_line_args = find_command_line_arguments(mqtt_command, mqtt_arg)
97105

106+
# replace $X with paramX
107+
for i, mqtt_param in enumerate(mqtt_params):
108+
command_line_args = command_line_args.replace("$"+str(i+1), mqtt_param)
109+
110+
# remove remaining $X items (those who don't have a matching paramX provided by the MQTT payload
111+
command_line_args = re.sub("\\$[0-9]*", "", command_line_args)
112+
98113
if command_to_run is not None:
99114
# when there are spaces in the given command we should set it in quotes
100115
if ' ' in command_to_run:

0 commit comments

Comments
 (0)