diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml deleted file mode 100644 index 4b06265..0000000 --- a/.github/workflows/main.yml +++ /dev/null @@ -1,35 +0,0 @@ -name: Publish Docker image -on: - release: - types: [published] - -jobs: - push_to_registry: - name: Push Docker image to Docker Hub - runs-on: ubuntu-latest - steps: - - name: Check out the repo - uses: actions/checkout@v2 - - - name: Set up Docker Buildx - id: buildx - uses: docker/setup-buildx-action@v1 - - - name: Set up QEMU - uses: docker/setup-qemu-action@v1 - - - name: Login to DockerHub - uses: docker/login-action@v1 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - - - name: Build image - run: | - docker buildx build --no-cache --push \ - --tag lolouk44/xiaomi-mi-scale:${{ github.event.release.tag_name }} \ - --tag lolouk44/xiaomi-mi-scale:latest \ - --platform linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8 . - - - name: Image digest - run: echo ${{ steps.docker_build.outputs.digest }} diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index abca157..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,65 +0,0 @@ -## [0.1.14] - 2020-11-26 -### Changed -- Reduced docker image size - -## [0.1.13] - 2020-11-26 -### Changed -- Fixed MQTT Discovery Message - -## [0.1.12] - 2020-11-23 -### Changed -- Updated workflow to automatically build docker images on new releases with version and latest tags - -## [0.1.11] - 2020-11-23 -### Changed -- Remove additional logging for Scale V1 that was used for testing -- Changed timestamp to default python format (fixes https://github.com/lolouk44/xiaomi_mi_scale/issues/29) -- Removed hard-coded 'unit_f_measurement' in the MQTT Discovery (fixes https://github.com/lolouk44/hassio-addons/issues/22) -- Fixed hard coded MQTT Discovery Prefix (fixes https://github.com/lolouk44/xiaomi_mi_scale/issues/35) -- Change measures format to be numbers instead of string where applicable (https://github.com/lolouk44/xiaomi_mi_scale/pull/36) -### Added -- Created workflow to automatically build docker images on new releases (Thanks [@AiiR42](https://github.com/AiiR42) for your help) - - -## [0.1.10] - 2020-09-09 -### Changed -- Fixed issue with detection of boolean in MQTT_DISCOVERY (https://github.com/lolouk44/hassio-addons/issues/16 and https://github.com/lolouk44/xiaomi_mi_scale/issues/31) - -## [0.1.9] - 2020-09-08 -### Changed -- Fixed typo in MQTT message following the **breaking change** to snake_case attributes in 0.1.8 - -## [0.1.8] - 2020-09-08 -### Breaking Changes -- Attributes are now snake_case (fixes https://github.com/lolouk44/xiaomi_mi_scale/issues/24) -### Changed -- Fixed default MQTT Prefix in config.json typo (fixes https://github.com/lolouk44/hassio-addons/issues/6) -- Fixed MQTT Discovery value check to discover -- Changed timestamp to default python format -- Changes the bluetooth reset from reset to down-wait-up (fixes https://github.com/lolouk44/hassio-addons/issues/13) -- Fixed hard coded hci0 to provided hci interface when performing a reset -- Fixed weight in Lbs not detected on Scale V1 (XMTZCO1HM) (fixes https://github.com/lolouk44/xiaomi_mi_scale/issues/28) -- Fixed body calculations for non kg weights -- Updated README -### Added -- Added unit to attributes - -## [0.1.7] - 2020-07-06 -### Added -- repository.json to make it a real add-on repo (fixes https://github.com/lolouk44/hassio-addons/issues/4) -## Changed -- Now truly handles optional config entries(fixes https://github.com/lolouk44/hassio-addons/issues/3) -- MQTT Discovery set wtih retain flag (fixes https://github.com/lolouk44/hassio-addons/issues/2) -- README updated to use Xiaomi Mi Fit App to retrieve the MAC Address (fixes https://github.com/lolouk44/xiaomi_mi_scale/pull/25) - -## [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 9e137cb..e8c2134 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,9 @@ FROM python:3.8-slim - +LABEL io.hass.version="0.1.7" io.hass.type="addon" io.hass.arch="armhf|aarch64|i386|amd64" WORKDIR /opt/miscale COPY src /opt/miscale -RUN apt-get update && apt-get install --no-install-recommends -y \ - build-essential \ +RUN apt-get update && apt-get install -y \ bluez \ python-pip \ libglib2.0-dev && \ @@ -15,6 +14,5 @@ RUN pip install -r requirements.txt # Copy in docker scripts to root of container... COPY dockerscripts/ / -RUN chmod +x /entrypoint.sh && chmod +x /cmd.sh ENTRYPOINT ["/entrypoint.sh"] CMD ["/cmd.sh"] \ No newline at end of file diff --git a/README.md b/README.md index 4ea9866..9a498df 100644 --- a/README.md +++ b/README.md @@ -2,19 +2,10 @@ Code to read weight measurements from Xiaomi Body Scales. -## BREAKING CHANGE: -Please note there was a breaking change in 0.1.8. The MQTT message json attributes are now in lower snake_case to be compliant with Home-Assistant Attributes. -This means Home-Assistant sensor configuration needs to be adjusted. -For example -`value_template: "{{ value_json['Weight'] }}"` -Needs to be replaced with -`value_template: "{{ value_json['weight'] }}"` -(note the lowercase `w` in `weight`) - ## Supported Scales: Name | Model | Picture --- | --- | :---: -[Mi Smart Scale 2](https://www.mi.com/global/scale)                                                                                               | XMTZCO1HM, XMTZC04HM | ![Mi Scale_2](Screenshots/Mi_Smart_Scale_2_Thumb.png) +[Mi Smart Scale 2](https://www.mi.com/global/scale)                                                                                               | XMTZC04HM | ![Mi Scale_2](Screenshots/Mi_Smart_Scale_2_Thumb.png) [Mi Body Composition Scale](https://www.mi.com/global/mi-body-composition-scale/) | XMTZC02HM | ![Mi Scale](Screenshots/Mi_Body_Composition_Scale_Thumb.png) [Mi Body Composition Scale 2](https://c.mi.com/thread-2289389-1-0.html) | XMTZC05HM | ![Mi Body Composition Scale 2](Screenshots/Mi_Body_Composition_Scale_2_Thumb.png) @@ -136,4 +127,4 @@ Thanks to @syssi (https://gist.github.com/syssi/4108a54877406dc231d95514e538bde9 Special thanks to [@ned-kelly](https://github.com/ned-kelly) for his help turning a "simple" python script into a fully fledged docker container -Thanks to [@bpaulin](https://github.com/bpaulin), [@AiiR42](https://github.com/AiiR42) for their PRs and collaboration +Thanks to [@bpaulin](https://github.com/bpaulin) for his PRs and collaboration diff --git a/src/Xiaomi_Scale.py b/src/Xiaomi_Scale.py index 58cf233..f3e2a24 100644 --- a/src/Xiaomi_Scale.py +++ b/src/Xiaomi_Scale.py @@ -193,8 +193,8 @@ OLD_MEASURE = '' def discovery(): for MQTTUser in (USER1_NAME,USER2_NAME,USER3_NAME): message = '{"name": "' + MQTTUser + ' Weight",' - message+= '"state_topic": "' + MQTT_PREFIX + '/' + MQTTUser + '/weight","value_template": "{{ value_json.weight }}",' - message+= '"json_attributes_topic": "' + MQTT_PREFIX + '/' + MQTTUser + '/weight","icon": "mdi:scale-bathroom"}' + 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, @@ -222,14 +222,16 @@ class ScanProcessor(): ### Xiaomi V1 Scale ### if data.startswith('1d18') and sdid == 22: measunit = data[4:6] + sys.stdout.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Measuring Unit: {measunit}\n") measured = int((data[8:10] + data[6:8]), 16) * 0.01 + sys.stdout.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Measured Raw Weight: {measured}\n") unit = '' if measunit.startswith(('03', 'a3')): unit = 'lbs' if measunit.startswith(('12', 'b2')): unit = 'jin' if measunit.startswith(('22', 'a2')): unit = 'kg' ; measured = measured / 2 if unit: if OLD_MEASURE != round(measured, 2): - self._publish(round(measured, 2), unit, str(datetime.now()), "", "") + self._publish(round(measured, 2), unit, str(datetime.today().strftime('%Y-%m-%d-%H:%M:%S')), "", "") OLD_MEASURE = round(measured, 2) ### Xiaomi V2 Scale ### @@ -248,7 +250,7 @@ class ScanProcessor(): miimpedance = str(int((data[24:26] + data[22:24]), 16)) if unit and isStabilized: if OLD_MEASURE != round(measured, 2) + int(miimpedance): - self._publish(round(measured, 2), unit, str(datetime.now()), hasImpedance, miimpedance) + self._publish(round(measured, 2), unit, str(datetime.today().strftime('%Y-%m-%d-%H:%M:%S')), hasImpedance, miimpedance) OLD_MEASURE = round(measured, 2) + int(miimpedance) @@ -274,23 +276,23 @@ class ScanProcessor(): lib = Xiaomi_Scale_Body_Metrics.bodyMetrics(calcweight, height, age, sex, 0) message = '{' - message += '"weight":' + "{:.2f}".format(weight) + message += '"weight":"' + "{:.2f}".format(weight) + '"' message += ',"weight_unit":"' + str(unit) + '"' - message += ',"bmi":' + "{:.2f}".format(lib.getBMI()) - message += ',"basal_metabolism":' + "{:.2f}".format(lib.getBMR()) - message += ',"visceral_fat":' + "{:.2f}".format(lib.getVisceralFat()) + message += ',"bmi":"' + "{:.2f}".format(lib.getBMI()) + '"' + message += ',"basal_metabolism":"' + "{:.2f}".format(lib.getBMR()) + '"' + message += ',"visceral_fat":"' + "{:.2f}".format(lib.getVisceralFat()) + '"' if hasImpedance: lib = Xiaomi_Scale_Body_Metrics.bodyMetrics(calcweight, height, age, sex, int(miimpedance)) bodyscale = ['Obese', 'Overweight', 'Thick-set', 'Lack-exerscise', 'Balanced', 'Balanced-muscular', 'Skinny', 'Balanced-skinny', 'Skinny-muscular'] - message += ',"lean_body_mass":' + "{:.2f}".format(lib.getLBMCoefficient()) - message += ',"body_fat":' + "{:.2f}".format(lib.getFatPercentage()) - message += ',"water":' + "{:.2f}".format(lib.getWaterPercentage()) - message += ',"bone_mass":' + "{:.2f}".format(lib.getBoneMass()) - message += ',"muscle_mass":' + "{:.2f}".format(lib.getMuscleMass()) - message += ',"protein":' + "{:.2f}".format(lib.getProteinPercentage()) + message += ',"lean_body_mass":"' + "{:.2f}".format(lib.getLBMCoefficient()) + '"' + message += ',"body_fat":"' + "{:.2f}".format(lib.getFatPercentage()) + '"' + message += ',"water":"' + "{:.2f}".format(lib.getWaterPercentage()) + '"' + message += ',"bone_mass":"' + "{:.2f}".format(lib.getBoneMass()) + '"' + message += ',"muscle_mass":"' + "{:.2f}".format(lib.getMuscleMass()) + '"' + message += ',"protein":"' + "{:.2f}".format(lib.getProteinPercentage()) + '"' message += ',"body_type":"' + str(bodyscale[lib.getBodyType()]) + '"' - message += ',"metabolic_age":' + "{:.0f}".format(lib.getMetabolicAge()) + message += ',"metabolic_age":"' + "{:.0f}".format(lib.getMetabolicAge()) + '"' message += ',"timestamp":"' + mitdatetime + '"' message += '}' diff --git a/src/wrapper.sh b/src/wrapper.sh index c8f556d..068d3ee 100644 --- a/src/wrapper.sh +++ b/src/wrapper.sh @@ -1,16 +1,7 @@ #!/bin/bash -sleep 60 # Give the system time after a reboot to connect to WiFi before continuing export MISCALE_MAC=00:00:00:00:00:00 # Mac address of your scale -export HCI_DEV=hci0 # Bluetooth hci device to use -export MQTT_HOST=127.0.0.1 # MQTT Server (defaults to 127.0.0.1) -export MQTT_PREFIX=miscale # MQTT Topic Prefix. Defaults to miscale -export MQTT_USERNAME= # Username for MQTT server (comment out if not required) -export MQTT_PASSWORD= # Password for MQTT (comment out if not required) -export MQTT_PORT=1883 # Defaults to 1883 -export TIME_INTERVAL=30 # Time in sec between each query to the scale, to allow other applications to use the Bluetooth module. Defaults to 30 -export MQTT_DISCOVERY=true # Home Assistant Discovery (true/false), defaults to true -export MQTT_DISCOVERY_PREFIX= # Home Assistant Discovery Prefix, defaults to homeassistant +export MQTT_PREFIX=miScale export USER1_GT=70 # If the weight is greater than this number, we'll assume that we're weighing User #1 export USER1_SEX=male