From 0f158886ef923fc1d9abaf46a521493bf1df1e18 Mon Sep 17 00:00:00 2001 From: 0xee Date: Sat, 3 Apr 2021 14:38:36 +0200 Subject: [PATCH] Add retain option to slider widget --- mqtt_dash/static/main.js | 6 ++++-- mqtt_dash/widgets.py | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/mqtt_dash/static/main.js b/mqtt_dash/static/main.js index f0cb1c3..abb7552 100644 --- a/mqtt_dash/static/main.js +++ b/mqtt_dash/static/main.js @@ -133,9 +133,10 @@ function updateScroll() { element.scrollTop = element.scrollHeight; } -function publish(topic, value) { +function publish(topic, value, retain=false) { var message = new Paho.MQTT.Message(value); message.destinationName = topic; + message.retained = retain; client.send(message); } @@ -159,7 +160,8 @@ function init(brokerUri) { slider.oninput = function() { this.onchange() var topic = this.getAttribute('data-pub-topic'); - publish(topic, this.value); + var retain = Boolean(this.getAttribute('data-retain')); + publish(topic, this.value, retain); }; } diff --git a/mqtt_dash/widgets.py b/mqtt_dash/widgets.py index 31f64a6..3a32039 100644 --- a/mqtt_dash/widgets.py +++ b/mqtt_dash/widgets.py @@ -49,7 +49,8 @@ def slider_widget(label, unit='', min_val=0, max_val=255, - step=1): + step=1, + retain=False): id_ = make_id() sub_topic = sub_topic or topic template = ''' @@ -58,6 +59,7 @@ def slider_widget(label, @@ -76,6 +78,7 @@ def slider_widget(label, value_path=value_path, topic=topic, unit=unit, + retain=retain, id_=id_)