diff --git a/.github/workflows/dockerhub-nightly.yml b/.github/workflows/dockerhub-nightly.yml new file mode 100644 index 00000000..a4590d6f --- /dev/null +++ b/.github/workflows/dockerhub-nightly.yml @@ -0,0 +1,58 @@ +name: Publish multi platform nightly Docker image + +on: + push: + branches: ["*"] + pull_request: + branches: ["*"] + workflow_dispatch: + +jobs: + push_to_registry: + name: Push nightly Docker image to Docker Hub + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_HUB_USER }} + password: ${{ secrets.DOCKER_HUB_PASS }} + + - name: Get current date + id: current_date + run: echo "::set-output name=date::$(date +'%Y%m%d')" + + - name: Get the current Git commit hash + id: git_hash + run: echo "GIT_HASH=$(git rev-parse HEAD)" >> $GITHUB_ENV + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ secrets.DOCKER_HUB_NAMESPACE }}/${{ secrets.DOCKER_HUB_REPOSITORY_NIGHTLY }} + tags: | + type=raw,value=nightly-${{ github.ref_name }}-${{ steps.current_date.outputs.date }} + type=raw,value=latest + + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + platforms: linux/amd64,linux/arm64,linux/arm/v7 + file: ./Dockerfile + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max + build-args: GIT_HASH=${{ env.GIT_HASH }} diff --git a/.github/workflows/dockerhub.yml b/.github/workflows/dockerhub.yml index 3b3d753c..318684a8 100644 --- a/.github/workflows/dockerhub.yml +++ b/.github/workflows/dockerhub.yml @@ -20,6 +20,10 @@ jobs: with: username: ${{ secrets.DOCKER_HUB_USER }} password: ${{ secrets.DOCKER_HUB_PASS }} + + - name: Get the current Git commit hash + id: git_hash + run: echo "GIT_HASH=$(git rev-parse HEAD)" >> $GITHUB_ENV - name: Extract metadata (tags, labels) for Docker id: meta @@ -38,3 +42,4 @@ jobs: labels: ${{ steps.meta.outputs.labels }} cache-from: type=gha cache-to: type=gha,mode=max + build-args: GIT_HASH=${{ env.GIT_HASH }} diff --git a/Dockerfile b/Dockerfile index a15d4ac5..3ced1dd5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,9 @@ # First, we need an image to build the application FROM rust:slim-bullseye as builder +ARG GIT_HASH +ENV GIT_HASH=$GIT_HASH + WORKDIR /usr/src/hms-mqtt-publish # The following builds the rust application in two stages: @@ -14,14 +17,16 @@ WORKDIR /usr/src/hms-mqtt-publish # Stage 1: Build the dependencies COPY ./Cargo.toml ./ +# Remove the hms2mqtt dependency from the Cargo.toml file (compiled in second stage) +RUN sed -i s/hms2mqtt.*//g Cargo.toml RUN mkdir src && \ echo "fn main() {println!(\"hello from dependency build\")}" > src/main.rs && \ cargo build --release -# Stage 2: Build the protobuf files - -COPY ./build.rs ./ +# Stage 2: Build the application +COPY ./Cargo.toml ./ +COPY ./hms2mqtt ./hms2mqtt COPY ./src ./src RUN cargo install --path . diff --git a/ha-hoymiles-wifi-addon-nightly/CHANGELOG.md b/ha-hoymiles-wifi-addon-nightly/CHANGELOG.md new file mode 100644 index 00000000..2a693ab0 --- /dev/null +++ b/ha-hoymiles-wifi-addon-nightly/CHANGELOG.md @@ -0,0 +1,19 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). + +## [v0.4] +Bugfixes and improvements + +## [v0.3] +Add debug_logging option + +## [v0.2] +Warning: breaking changes! The sensor identifiers have changed +Implemented support for home assistant discovery +Add extra sensors + +## [v0.1] +Initial release \ No newline at end of file diff --git a/ha-hoymiles-wifi-addon-nightly/Dockerfile b/ha-hoymiles-wifi-addon-nightly/Dockerfile new file mode 100644 index 00000000..a9edd2f5 --- /dev/null +++ b/ha-hoymiles-wifi-addon-nightly/Dockerfile @@ -0,0 +1,16 @@ +ARG BUILD_FROM + +# Use pre-built image to copy the binary from +FROM dominikandreas/hms-mqtt-publisher-nightly:nightly-main-20231209 AS builder + +FROM $BUILD_FROM + +# Copy the compiled binary from the builder stage +COPY --from=builder /usr/local/bin/hms-mqtt-publish /usr/local/bin/hms-mqtt-publish + +# Copy the run script into the container +COPY run.sh /run.sh +RUN chmod a+x /run.sh + +# When the container starts, run the script +CMD [ "/run.sh" ] diff --git a/ha-hoymiles-wifi-addon-nightly/README.md b/ha-hoymiles-wifi-addon-nightly/README.md new file mode 100644 index 00000000..b710b41e --- /dev/null +++ b/ha-hoymiles-wifi-addon-nightly/README.md @@ -0,0 +1,33 @@ +# Home Assistant Add-on + +> Note: this is the nightly version of the add-on that is used for testing. + +This tool can directly run inside Home Assistant OS as an addon. To get started, add the URL of this repository to the add-on store in Home Assistant and install the add-on. Alternatively, you can also run it stand-alone on any machine that supports rust or docker (see section below). + +## Configuration + +To set up the add-on, fill in the following configuration parameters: + +- `inverter_host`: The hostname or IP address of your inverter. +- `mqtt_broker_host`: The hostname or IP address of your MQTT broker. +- `mqtt_username`: The username for your MQTT broker. +- `mqtt_password`: The password for your MQTT broker. Keep this secret! +- `mqtt_port`: The port of your MQTT broker (default is 1883 for unencrypted MQTT). + +## Example configuration + +```yaml +inverter_host: "192.168.1.100" +mqtt_broker_host: "core-mosquitto" +mqtt_username: "yourusername" +mqtt_password: "yourpassword" +mqtt_port: 1883 +``` + +## Note of caution +Please note: The tool does not come with any guarantees and if by chance you fry your inverter with a funny series of bits, you are on your own. That being said, no inverters have been harmed during development. + +## Known limitations +- One can only fetch updates approximately twice per minute. The inverter firmware seems to implement a mandatory wait period of a little more than 30 seconds. If one makes a request within 30 seconds of the previous one, then the inverter will reply with the previous reading and restart the countdown. It will also not send updated values to S-Miles Cloud if this happens. +- The tools was developed for (and with an) HMS-800W-T2. It may work with the other inverters from the series, but is untested at the time of writing + diff --git a/ha-hoymiles-wifi-addon-nightly/build.yaml b/ha-hoymiles-wifi-addon-nightly/build.yaml new file mode 100644 index 00000000..ee9c18de --- /dev/null +++ b/ha-hoymiles-wifi-addon-nightly/build.yaml @@ -0,0 +1,5 @@ +--- +build_from: + aarch64: ghcr.io/hassio-addons/debian-base:7.2.0 + amd64: ghcr.io/hassio-addons/debian-base:7.2.0 + armv7: ghcr.io/hassio-addons/debian-base:7.2.0 diff --git a/ha-hoymiles-wifi-addon-nightly/config.yaml b/ha-hoymiles-wifi-addon-nightly/config.yaml new file mode 100644 index 00000000..6c2d1a92 --- /dev/null +++ b/ha-hoymiles-wifi-addon-nightly/config.yaml @@ -0,0 +1,28 @@ +--- +name: Hoymiles HMS Wifi Addon (Nightly) +version: 'v0.4' +slug: ha-hoymiles-wifi-addon-nightly +description: Publishes telemetry information from Hoymiles HMS-XXXXW-T2 micro-inverters to your home assistant MQTT broker +url: https://github.com/dominikandreas/hms-mqtt-publisher/tree/main/ha-hoymiles-wifi-addon-nightly +init: false +arch: # List of supported architectures. + - armv7 + - aarch64 + - amd64 +startup: application # When the add-on should start. +boot: auto # Boot options, auto or manual. +services: ['mqtt:want'] +options: # Default options value for the add-on. + inverter_host: '' + mqtt_broker_host: '' + mqtt_username: '' + mqtt_password: '' + mqtt_port: 1883 + debug_logging: false +schema: # Schema validation for the options. + inverter_host: str + mqtt_broker_host: str? + mqtt_username: str? + mqtt_password: str? + mqtt_port: int + debug_logging: bool \ No newline at end of file diff --git a/ha-hoymiles-wifi-addon-nightly/icon.png b/ha-hoymiles-wifi-addon-nightly/icon.png new file mode 100644 index 00000000..a1f9142a Binary files /dev/null and b/ha-hoymiles-wifi-addon-nightly/icon.png differ diff --git a/ha-hoymiles-wifi-addon-nightly/logo.png b/ha-hoymiles-wifi-addon-nightly/logo.png new file mode 100644 index 00000000..be644043 Binary files /dev/null and b/ha-hoymiles-wifi-addon-nightly/logo.png differ diff --git a/ha-hoymiles-wifi-addon-nightly/run.sh b/ha-hoymiles-wifi-addon-nightly/run.sh new file mode 100644 index 00000000..26c03303 --- /dev/null +++ b/ha-hoymiles-wifi-addon-nightly/run.sh @@ -0,0 +1,53 @@ +#!/usr/bin/with-contenv bashio + +# Enable strict mode for bash (exit on error, error on undefined variable, error if any pipeline element fails) +set -euo pipefail +# Fetch values from the add-on configuration by extracting it from /data/options.json + +HA_MQTT_BROKER_HOST=$(bashio::services mqtt "host") +HA_MQTT_USERNAME=$(bashio::services mqtt "username") +HA_MQTT_PASSWORD=$(bashio::services mqtt "password") + +INVERTER_HOST=$(bashio::config 'inverter_host') +MQTT_BROKER_HOST=$(bashio::config 'mqtt_broker_host') +MQTT_USERNAME=$(bashio::config 'mqtt_username') +MQTT_PASSWORD=$(bashio::config 'mqtt_password') +MQTT_PORT=$(bashio::config 'mqtt_port') +DEBUG_LOGGING=$(bashio::config 'debug_logging') + +# Use bashio::config values if they are defined, otherwise fall back to bashio::services values +MQTT_BROKER_HOST=${MQTT_BROKER_HOST:-$HA_MQTT_BROKER_HOST} +MQTT_USERNAME=${MQTT_USERNAME:-$HA_MQTT_USERNAME} +MQTT_PASSWORD=${MQTT_PASSWORD:-$HA_MQTT_PASSWORD} +DEBUG_LOGGING=${DEBUG_LOGGING:-'false'} + + +# Check if the required configs are provided +if [[ -z "$INVERTER_HOST" ]]; then + echo "The inverter_host is not configured." + exit 1 +fi + +if [[ -z "$MQTT_BROKER_HOST" ]]; then + echo "The mqtt_broker_host is not configured." + exit 1 +fi + +# Create the configuration file +cat < ./config.toml +inverter_host = "$INVERTER_HOST" + +[home_assistant] +host = "$MQTT_BROKER_HOST" +username = "$MQTT_USERNAME" +password = "$MQTT_PASSWORD" +port = $MQTT_PORT +EOF + +# if DEBUG_LOGGING is true, set the RUST_LOG=debug environment variable to enable debug logging +if [[ "$DEBUG_LOGGING" == "true" ]]; then + export RUST_LOG=debug +fi + +# Execute the application +/usr/local/bin/hms-mqtt-publish diff --git a/ha-hoymiles-wifi-addon/CHANGELOG.md b/ha-hoymiles-wifi-addon/CHANGELOG.md index 24c0376d..2a693ab0 100644 --- a/ha-hoymiles-wifi-addon/CHANGELOG.md +++ b/ha-hoymiles-wifi-addon/CHANGELOG.md @@ -4,8 +4,16 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). -## [v0.1] -Initial release +## [v0.4] +Bugfixes and improvements + +## [v0.3] +Add debug_logging option ## [v0.2] -Support auto discovery \ No newline at end of file +Warning: breaking changes! The sensor identifiers have changed +Implemented support for home assistant discovery +Add extra sensors + +## [v0.1] +Initial release \ No newline at end of file diff --git a/ha-hoymiles-wifi-addon/Dockerfile b/ha-hoymiles-wifi-addon/Dockerfile index 61cb4563..a9edd2f5 100644 --- a/ha-hoymiles-wifi-addon/Dockerfile +++ b/ha-hoymiles-wifi-addon/Dockerfile @@ -1,7 +1,7 @@ ARG BUILD_FROM # Use pre-built image to copy the binary from -FROM dominikandreas/hms-mqtt-publisher-nightly:experimental-output_channels AS builder +FROM dominikandreas/hms-mqtt-publisher-nightly:nightly-main-20231209 AS builder FROM $BUILD_FROM diff --git a/ha-hoymiles-wifi-addon/config.yaml b/ha-hoymiles-wifi-addon/config.yaml index 38512d1c..90d36cc5 100644 --- a/ha-hoymiles-wifi-addon/config.yaml +++ b/ha-hoymiles-wifi-addon/config.yaml @@ -1,9 +1,9 @@ --- name: Hoymiles HMS Wifi Addon -version: '0.2' +version: 'v0.3' slug: ha-hoymiles-wifi-addon description: Publishes telemetry information from Hoymiles HMS-XXXXW-T2 micro-inverters to your home assistant MQTT broker -url: https://github.com/dennisosrm/hms-mqtt-publisher/tree/master/ha-hoymiles-wifi-addon +url: https://github.com/dominikandreas/hms-mqtt-publisher/tree/master/ha-hoymiles-wifi-addon init: false arch: # List of supported architectures. - armv7 @@ -18,9 +18,11 @@ options: # Default options value for the add-on. mqtt_username: '' mqtt_password: '' mqtt_port: 1883 + debug_logging: false schema: # Schema validation for the options. inverter_host: str mqtt_broker_host: str? mqtt_username: str? mqtt_password: str? - mqtt_port: int \ No newline at end of file + mqtt_port: int + debug_logging: bool \ No newline at end of file diff --git a/ha-hoymiles-wifi-addon/run.sh b/ha-hoymiles-wifi-addon/run.sh index 7413ce8d..26c03303 100644 --- a/ha-hoymiles-wifi-addon/run.sh +++ b/ha-hoymiles-wifi-addon/run.sh @@ -13,11 +13,13 @@ MQTT_BROKER_HOST=$(bashio::config 'mqtt_broker_host') MQTT_USERNAME=$(bashio::config 'mqtt_username') MQTT_PASSWORD=$(bashio::config 'mqtt_password') MQTT_PORT=$(bashio::config 'mqtt_port') +DEBUG_LOGGING=$(bashio::config 'debug_logging') # Use bashio::config values if they are defined, otherwise fall back to bashio::services values MQTT_BROKER_HOST=${MQTT_BROKER_HOST:-$HA_MQTT_BROKER_HOST} MQTT_USERNAME=${MQTT_USERNAME:-$HA_MQTT_USERNAME} MQTT_PASSWORD=${MQTT_PASSWORD:-$HA_MQTT_PASSWORD} +DEBUG_LOGGING=${DEBUG_LOGGING:-'false'} # Check if the required configs are provided @@ -35,28 +37,17 @@ fi cat < ./config.toml inverter_host = "$INVERTER_HOST" -[home_assistent] +[home_assistant] host = "$MQTT_BROKER_HOST" username = "$MQTT_USERNAME" password = "$MQTT_PASSWORD" port = $MQTT_PORT EOF -# Write mqtt broker username if specified -if [[ -n "$MQTT_USERNAME" ]]; then - echo "username = \"$MQTT_USERNAME\"\n" >> ./config.toml +# if DEBUG_LOGGING is true, set the RUST_LOG=debug environment variable to enable debug logging +if [[ "$DEBUG_LOGGING" == "true" ]]; then + export RUST_LOG=debug fi -# Create the configuration file -cat < ./config.toml -inverter_host = "$INVERTER_HOST" - -[home_assistant] -host = "$MQTT_BROKER_HOST" -username = "$MQTT_USERNAME" -password = "$MQTT_PASSWORD" -port = $MQTT_PORT -EOF - # Execute the application /usr/local/bin/hms-mqtt-publish