Skip to content

Commit

Permalink
Improve error handling, Nightly build (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikandreas authored Dec 10, 2023
1 parent 84d21a0 commit bc713b8
Show file tree
Hide file tree
Showing 15 changed files with 248 additions and 25 deletions.
58 changes: 58 additions & 0 deletions .github/workflows/dockerhub-nightly.yml
Original file line number Diff line number Diff line change
@@ -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 }}
5 changes: 5 additions & 0 deletions .github/workflows/dockerhub.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}
11 changes: 8 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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 .

Expand Down
19 changes: 19 additions & 0 deletions ha-hoymiles-wifi-addon-nightly/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions ha-hoymiles-wifi-addon-nightly/Dockerfile
Original file line number Diff line number Diff line change
@@ -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" ]
33 changes: 33 additions & 0 deletions ha-hoymiles-wifi-addon-nightly/README.md
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions ha-hoymiles-wifi-addon-nightly/build.yaml
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions ha-hoymiles-wifi-addon-nightly/config.yaml
Original file line number Diff line number Diff line change
@@ -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
Binary file added ha-hoymiles-wifi-addon-nightly/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ha-hoymiles-wifi-addon-nightly/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions ha-hoymiles-wifi-addon-nightly/run.sh
Original file line number Diff line number Diff line change
@@ -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 <<EOF > ./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
14 changes: 11 additions & 3 deletions ha-hoymiles-wifi-addon/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Warning: breaking changes! The sensor identifiers have changed
Implemented support for home assistant discovery
Add extra sensors

## [v0.1]
Initial release
2 changes: 1 addition & 1 deletion ha-hoymiles-wifi-addon/Dockerfile
Original file line number Diff line number Diff line change
@@ -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

Expand Down
8 changes: 5 additions & 3 deletions ha-hoymiles-wifi-addon/config.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
mqtt_port: int
debug_logging: bool
21 changes: 6 additions & 15 deletions ha-hoymiles-wifi-addon/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,28 +37,17 @@ fi
cat <<EOF > ./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 <<EOF > ./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

0 comments on commit bc713b8

Please sign in to comment.