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 576051c..241bf64 100644 --- a/config.json +++ b/config.json @@ -1,9 +1,10 @@ { "name": "Xiaomi Mi Scale", - "version": "0.1.3", + "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", + "image": "lolouk44/xiaomi-mi-scale", "arch": ["armhf", "armv7", "aarch64", "amd64", "i386"], "startup": "before", "boot": "auto", @@ -20,6 +21,8 @@ "MQTT_PASSWORD": "", "MQTT_PORT": 1883, "TIME_INTERVAL": 30, + "MQTT_DISCOVERY": true, + "MQTT_DISCOVERY_PREFIX": "homeassistant", "USER1_GT": 70, "USER1_SEX": "male", @@ -48,6 +51,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..dbaf26f 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,23 @@ 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} + ) + sys.stdout.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Discovery Completed...\n") + class ScanProcessor(): def GetAge(self, d1): @@ -197,6 +210,8 @@ class ScanProcessor(): raise def main(): + if MQTT_DISCOVERY: + discovery() BluetoothFailCounter = 0 while True: try: