From c5b64862b7fa7da0fdd7d39c7df429dbabe0d934 Mon Sep 17 00:00:00 2001 From: lolouk44 Date: Wed, 1 Jul 2020 20:22:34 +0100 Subject: [PATCH 1/3] mqtt discovery support --- config.json | 6 +++++- src/Xiaomi_Scale.py | 34 ++++++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/config.json b/config.json index 576051c..0b0681e 100644 --- a/config.json +++ b/config.json @@ -1,6 +1,6 @@ { "name": "Xiaomi Mi Scale", - "version": "0.1.3", + "version": "0.1.5b", "slug": "xiaomi_mi_scale", "description": "Read weight measurements from Xiamomi scale via BLE", "url": "https://github.com/lolouk44/xiaomi_mi_scale_ha_add_on", @@ -20,6 +20,8 @@ "MQTT_PASSWORD": "", "MQTT_PORT": 1883, "TIME_INTERVAL": 30, + "MQTT_DISCOVERY": true, + "MQTT_DISCOVERY_PREFIX": "homeassistant", "USER1_GT": 70, "USER1_SEX": "male", @@ -48,6 +50,8 @@ "MQTT_PASSWORD": "str", "MQTT_PORT": "int", "TIME_INTERVAL": "int", + "MQTT_DISCOVERY": "bool", + "MQTT_DISCOVERY_PREFIX": "str", "USER1_GT": "int", "USER1_SEX": "str", diff --git a/src/Xiaomi_Scale.py b/src/Xiaomi_Scale.py index 85b04c5..472a42e 100644 --- a/src/Xiaomi_Scale.py +++ b/src/Xiaomi_Scale.py @@ -29,20 +29,15 @@ try: sys.stdout.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Loading Config From Add-On Options...\n") data = json.load(json_file) MISCALE_MAC = data["MISCALE_MAC"] - if(data["MQTT_USERNAME"] == ""): - MQTT_USERNAME = None - else: - MQTT_USERNAME = data["MQTT_USERNAME"] - if(data["MQTT_PASSWORD"] == ""): - MQTT_PASSWORD = None - else: - MQTT_PASSWORD = data["MQTT_PASSWORD"] + MQTT_USERNAME = None if(data["MQTT_USERNAME"] == "") else data["MQTT_USERNAME"] + MQTT_PASSWORD = None if(data["MQTT_PASSWORD"] == "") else data["MQTT_PASSWORD"] MQTT_HOST = data["MQTT_HOST"] MQTT_PORT = int(data["MQTT_PORT"]) MQTT_PREFIX = data["MQTT_PREFIX"] TIME_INTERVAL = int(data["TIME_INTERVAL"]) + MQTT_DISCOVERY = data["MQTT_DISCOVERY"] + MQTT_DISCOVERY_PREFIX = data["MQTT_DISCOVERY_PREFIX"] HCI_DEV = data["HCI_DEV"][-1] - OLD_MEASURE = '' # User Variables... USER1_GT = int(data["USER1_GT"]) @@ -74,8 +69,9 @@ except FileNotFoundError: MQTT_PORT = int(os.getenv('MQTT_PORT', 1883)) MQTT_PREFIX = os.getenv('MQTT_PREFIX', 'miscale') TIME_INTERVAL = int(os.getenv('TIME_INTERVAL', 30)) + MQTT_DISCOVERY = os.getenv('MQTT_DISCOVERY',True) + MQTT_DISCOVERY_PREFIX = os.getenv('MQTT_DISCOVERY_PREFIX','homeassistant') HCI_DEV = os.getenv('HCI_DEV', 'hci0')[-1] - OLD_MEASURE = '' # User Variables... USER1_GT = int(os.getenv('USER1_GT', '70')) # If the weight is greater than this number, we'll assume that we're weighing User #1 @@ -96,6 +92,22 @@ except FileNotFoundError: USER3_DOB = os.getenv('USER3_DOB', '1988-01-01') # DOB (in yyyy-mm-dd format) sys.stdout.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Config Loaded...\n") +OLD_MEASURE = '' + +def discovery(): + for MQTTUser in (USER1_NAME,USER2_NAME,USER3_NAME): + message = '{"name": "' + MQTTUser + ' Weight",' + message+= '"state_topic": "miScale/' + MQTTUser + '/weight","value_template": "{{ value_json.Weight }}","unit_of_measurement": "kg",' + message+= '"json_attributes_topic": "miScale/' + MQTTUser + '/weight","icon": "mdi:scale-bathroom"}' + publish.single( + MQTT_DISCOVERY_PREFIX + '/sensor/' + MQTT_PREFIX + '/' + MQTTUser + '/config', + message, + retain=False, + hostname=MQTT_HOST, + port=MQTT_PORT, + auth={'username':MQTT_USERNAME, 'password':MQTT_PASSWORD} + ) + class ScanProcessor(): def GetAge(self, d1): @@ -197,6 +209,8 @@ class ScanProcessor(): raise def main(): + if MQTT_DISCOVERY: + discovery() BluetoothFailCounter = 0 while True: try: From 6a7225f32d902ad44670d0eadff55b4a51324af0 Mon Sep 17 00:00:00 2001 From: lolouk44 Date: Wed, 1 Jul 2020 23:52:57 +0100 Subject: [PATCH 2/3] 0.1.6 --- CHANGELOG.md | 6 ++++++ Dockerfile | 2 +- config.json | 1 + src/Xiaomi_Scale.py | 1 + 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3f6dee..47f91ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## [0.1.6] - 2020-07-01 +### Added +- Docker Image so install is quicker (no local build required). +## [0.1.5] - 2020-07-01 +### Added +- MQTT Discovery Support. ## [0.1.4] - 2020-06-29 ### Added - First release (version in line with non Add-On script for ease of maintenance). diff --git a/Dockerfile b/Dockerfile index d8e9481..d319573 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ FROM python:3.8-slim - +LABEL io.hass.version="0.1.6" io.hass.type="addon" io.hass.arch="armhf|aarch64|i386|amd64" WORKDIR /opt/miscale COPY src /opt/miscale diff --git a/config.json b/config.json index 0b0681e..9313de7 100644 --- a/config.json +++ b/config.json @@ -4,6 +4,7 @@ "slug": "xiaomi_mi_scale", "description": "Read weight measurements from Xiamomi scale via BLE", "url": "https://github.com/lolouk44/xiaomi_mi_scale_ha_add_on", + "image": "lolouk44/xiaomi-mi-scale", "arch": ["armhf", "armv7", "aarch64", "amd64", "i386"], "startup": "before", "boot": "auto", diff --git a/src/Xiaomi_Scale.py b/src/Xiaomi_Scale.py index 472a42e..dbaf26f 100644 --- a/src/Xiaomi_Scale.py +++ b/src/Xiaomi_Scale.py @@ -107,6 +107,7 @@ def discovery(): port=MQTT_PORT, auth={'username':MQTT_USERNAME, 'password':MQTT_PASSWORD} ) + sys.stdout.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Discovery Completed...\n") class ScanProcessor(): From ca103cae30822286f2add6d343843d06246eda46 Mon Sep 17 00:00:00 2001 From: lolouk44 Date: Wed, 1 Jul 2020 23:57:15 +0100 Subject: [PATCH 3/3] Update config.json --- config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.json b/config.json index 9313de7..241bf64 100644 --- a/config.json +++ b/config.json @@ -1,6 +1,6 @@ { "name": "Xiaomi Mi Scale", - "version": "0.1.5b", + "version": "0.1.6", "slug": "xiaomi_mi_scale", "description": "Read weight measurements from Xiamomi scale via BLE", "url": "https://github.com/lolouk44/xiaomi_mi_scale_ha_add_on",