From 929b9c411a9c2729d0f53497f01a51a00bcd1b8d Mon Sep 17 00:00:00 2001 From: Lolouk44 Date: Mon, 8 Mar 2021 22:15:01 +0000 Subject: [PATCH] MQTT Retain Option --- CHANGELOG.md | 5 +++++ README.md | 1 + src/Xiaomi_Scale.py | 12 +++++++++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6806cf8..76faa80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## [0.1.15] - 2021-03-08 +### Changed +- Added MQTT Retain Option ([fixes #44](https://github.com/lolouk44/xiaomi_mi_scale/issues/44)) +- Added MQTT TLS Option ([PR 43](https://github.com/lolouk44/xiaomi_mi_scale/pull/43)) + ## [0.1.14] - 2020-11-26 ### Changed - Reduced docker image size diff --git a/README.md b/README.md index 725c171..000210c 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ services: - MQTT_USERNAME= # Username for MQTT server (comment out if not required) - MQTT_PASSWORD= # Password for MQTT (comment out if not required) - MQTT_PORT= # Defaults to 1883 + - MQTT_RETAIN=true # MQTT Retain Option, defaults to true - MQTT_TLS_CACERTS= # MQTT TLS connection: directory with CA certificate(s) that signed MQTT Server's TLS certificate, defaults to None (= no TLS connection) - MQTT_TLS_INSECURE= # MQTT TLS connection: don't verify hostname in TLS certificate, defaults to None (= always check hostname) - TIME_INTERVAL=30 # Time in sec between each query to the scale, to allow other applications to use the Bluetooth module. Defaults to 30 diff --git a/src/Xiaomi_Scale.py b/src/Xiaomi_Scale.py index eeb802e..f877c2e 100644 --- a/src/Xiaomi_Scale.py +++ b/src/Xiaomi_Scale.py @@ -48,6 +48,11 @@ except: sys.stderr.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - MQTT Host not provided...\n") raise + try: + MQTT_RETAIN = data["MQTT_RETAIN"] + except: + MQTT_RETAIN = True + pass try: MQTT_PORT = int(data["MQTT_PORT"]) except: @@ -168,6 +173,11 @@ MQTT_USERNAME = os.getenv('MQTT_USERNAME', 'username') MQTT_PASSWORD = os.getenv('MQTT_PASSWORD', None) MQTT_HOST = os.getenv('MQTT_HOST', '127.0.0.1') + MQTT_RETAIN = os.getenv('MQTT_RETAIN', True) + if MQTT_RETAIN.lower() in ['true', '1', 'y', 'yes']: + MQTT_RETAIN = True + else: + MQTT_RETAIN = False MQTT_PORT = int(os.getenv('MQTT_PORT', 1883)) MQTT_TLS_CACERTS = os.getenv('MQTT_TLS_CACERTS', None) MQTT_TLS_INSECURE = os.getenv('MQTT_TLS_INSECURE', None) @@ -318,7 +328,7 @@ def _publish(self, weight, unit, mitdatetime, hasImpedance, miimpedance): MQTT_PREFIX + '/' + user + '/weight', message, # qos=1, #Removed qos=1 as incorrect connection details will result in the client waiting for ack from broker - retain=True, + retain=MQTT_RETAIN, hostname=MQTT_HOST, port=MQTT_PORT, auth={'username':MQTT_USERNAME, 'password':MQTT_PASSWORD},