Skip to content

Commit bf6f840

Browse files
psyciknzamotl
authored andcommitted
Added publishversion to core and documentation
1 parent 5bb028c commit bf6f840

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

HANDBOOK.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ I've written an introductory post, explaining [what mqttwarn can be used for](ht
132132
; name the service providers you will be using.
133133
launch = file, log, osxnotify, mysql, smtp
134134

135+
; Publish the current mqttwarn version to the topic (retained)
136+
publishversion = mqttwarn/version
137+
135138

136139
; optional: TLS parameters. (Don't forget to set the port number for
137140
; TLS (probably 8883).
@@ -164,6 +167,18 @@ I've written an introductory post, explaining [what mqttwarn can be used for](ht
164167
165168
You should launch every service you want to use from your topic/target definitions here.
166169
170+
###### `publishversion`
171+
172+
A topic that, when it exists, mqttwarn will publish it's current version as a retained payload.
173+
Useful for automated updates (docker watchtower and the like)
174+
175+
For example:
176+
```
177+
root@mymachine:~$ mosquitto_sub -v -t mqttwarn/version
178+
mqttwarn/version 0.25.0
179+
```
180+
181+
167182
#### The `[config:xxx]` sections
168183

169184
Sections called `[config:xxx]` configure settings for a service _xxx_. Each of these sections

mqttwarn/core.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import paho.mqtt.client as paho
2222

23+
from mqttwarn import __version__
2324
from mqttwarn.context import RuntimeContext, FunctionInvoker
2425
from mqttwarn.cron import PeriodicThread
2526
from mqttwarn.util import \
@@ -628,6 +629,12 @@ def connect():
628629
# Update our runtime context (used by functions etc) now we have a connected MQTT client
629630
context.invoker.srv.mqttc = mqttc
630631

632+
try:
633+
if cf.publishversion:
634+
mqttc.publish(cf.publishversion,__version__,retain=True)
635+
except:
636+
pass
637+
631638
# Launch worker threads to operate on queue
632639
start_workers()
633640

mqttwarn/examples/basic/mqttwarn.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ functions = 'samplefuncs.py'
5555
; name the service providers you will be using.
5656
launch = file, log
5757

58+
; Publish the current mqttwarn version to the topic (retained)
59+
publishversion = mqttwarn/version
60+
61+
5862

5963
; -------
6064
; Targets

tests/test_core.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
configfile_service_loading,
1717
configfile_unknown_functions,
1818
)
19+
20+
1921
from tests.util import core_bootstrap, send_message
2022

2123

@@ -282,3 +284,31 @@ def test_config_bad_functions(caplog):
282284
error_message = str(excinfo.value)
283285
assert "UNKNOWN FILE REFERENCE" in error_message
284286
assert "not found" in error_message
287+
288+
289+
def inactive_test_status_publish(caplog):
290+
"""
291+
Aim to test the `status_publish` feature.
292+
Test the core connection method.
293+
294+
Note:
295+
We need figure out how ot mock a connection to a non-existent MQTT server.
296+
"""
297+
298+
from unittest import mock
299+
from unittest.mock import call, PropertyMock
300+
from mqttwarn.core import connect
301+
302+
with caplog.at_level(logging.DEBUG):
303+
304+
# Bootstrap the core machinery without MQTT
305+
core_bootstrap(configfile=configfile_full)
306+
307+
mqtt_publish_mock = mock.MagicMock()
308+
mqttc = mqtt_publish_mock
309+
310+
# Signal mocked MQTT message to the core machinery for processing
311+
outcome = connect().mqttc = mqtt_publish_mock
312+
313+
# Proof that the message has been routed to the "log" plugin properly
314+
#assert assertion here to verify the connection

0 commit comments

Comments
 (0)