Skip to content

Commit

Permalink
Merge pull request #13 from atbore-phx/feat/ha
Browse files Browse the repository at this point in the history
Adding Home Assistant add-ons
  • Loading branch information
atbore-phx authored Mar 10, 2024
2 parents 010ca1e + f257fe6 commit 6edbab0
Show file tree
Hide file tree
Showing 23 changed files with 340 additions and 135 deletions.
23 changes: 21 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ on:

permissions:
contents: write
packages: write
id-token: write

jobs:
goreleaser:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -25,4 +27,21 @@ jobs:
version: latest
args: release --clean
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
- name: copy binary to local ha folder
run: |
mkdir home-assistant/addons/sbam/bin/
cp -v dist/sbam_linux_amd64_v1/bin/sbam home-assistant/addons/sbam/bin/
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Publish build - Home Assistant builder
uses: home-assistant/[email protected]
with:
args: |
--amd64 \
--target /data/home-assistant/addons/sbam \
--addon
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*.so
*.dylib
bin/

home-assistant/addons/sbam/bin/
# Test binary, built with `go test -c`
*.test

Expand Down
1 change: 1 addition & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ builds:
binary: bin/sbam
env:
- CGO_ENABLED=0

changelog:
sort: asc
filters:
Expand Down
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,16 @@ Usage:
sbam schedule [flags]

Flags:
-k, --apikey string APIKEY
-e, --end_hr string END_HR (default "06:00")
-H, --fronius_ip string FRONIUS_IP
-h, --help help for schedule
-m, --max_charge float MAX_CHARGE (default 3500)
-k, --apikey string APIKEY
-t, --crontab string crontab (default "0 0 0 0 0")
-d, --defaults DEFAULTS (default true)
-e, --end_hr string END_HR (default "05:55")
-H, --fronius_ip string FRONIUS_IP
-h, --help help for schedule
-m, --max_charge float MAX_CHARGE (default 3500)
-r, --pw_batt_reserve float PW_BATT_RESERVE
-c, --pw_consumption float PW_CONSUMPTION
-s, --start_hr string START_HR (default "00:00")
-u, --url string URL
-c, --pw_consumption float PW_CONSUMPTION
-s, --start_hr string START_HR (default "00:00")
-u, --url string URL

```
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/robfig/cron/v3 v3.0.1
github.com/simonvetter/modbus v1.6.0
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.18.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdU
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions home-assistant/addons/sbam/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[sbam](https://github.com/atbore-phx/sbam/releases/latest)
1 change: 1 addition & 0 deletions home-assistant/addons/sbam/DOCS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[sbam](https://github.com/atbore-phx/sbam)
11 changes: 11 additions & 0 deletions home-assistant/addons/sbam/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ARG BUILD_FROM
FROM $BUILD_FROM

# Copy data for add-on
COPY run.sh /
RUN chmod a+x /run.sh

COPY bin/sbam /usr/bin/
RUN chmod a+x /usr/bin/sbam

CMD [ "/run.sh" ]
1 change: 1 addition & 0 deletions home-assistant/addons/sbam/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[sbam](https://github.com/atbore-phx/sbam)
5 changes: 5 additions & 0 deletions home-assistant/addons/sbam/build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"build_from": {
"amd64": "ghcr.io/home-assistant/amd64-base:3.19"
}
}
36 changes: 36 additions & 0 deletions home-assistant/addons/sbam/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "sbam",
"version": "latest",
"slug": "sbam",
"init": "false",
"description": "Smart Battery Avanced Manager",
"arch": [
"amd64"
],
"url": "https://github.com/atbore-phx/sbam",
"image": "ghcr.io/atbore-phx/ha-sbam",
"options": {
"url": "https://api.solcast.com.au/rooftop_sites/<YOUR-SITE>/forecasts?format=json",
"apikey": "",
"fronius_ip": "",
"start_hr": "00:00",
"end_hr": "06:00",
"crontab": "00 00-05 * * *",
"pw_consumption": "11000",
"max_charge": "3500",
"pw_batt_reserve": "4000",
"defaults": "true"
},
"schema": {
"url": "str",
"apikey": "password",
"fronius_ip": "match(^((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)(\\.(?!$)|$)){4}$)",
"start_hr": "match(^([01]?[0-9]|2[0-3]):[0-5][0-9]$)",
"end_hr": "match(^([01]?[0-9]|2[0-3]):[0-5][0-9]$)",
"crontab": "match(^((((\\d+,)+\\d+|(\\d+(\/|-|#)\\d+)|\\d+L?|\\*(\/\\d+)?|L(-\\d+)?|\\?|[A-Z]{3}(-[A-Z]{3})?) ?){5,7})|(@(annually|yearly|monthly|weekly|daily|hourly|reboot))|(@every (\\d+(ns|us|µs|ms|s|m|h))+)$)",
"pw_consumption": "float",
"max_charge": "float",
"pw_batt_reserve": "float",
"defaults": "bool"
}
}
Binary file added home-assistant/addons/sbam/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 home-assistant/addons/sbam/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions home-assistant/addons/sbam/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/with-contenv bashio

export URL=$(bashio::config 'url')
export APIKEY=$(bashio::config 'apikey')
export FRONIUS_IP=$(bashio::config 'fronius_ip')
export START_HR=$(bashio::config 'start_hr')
export END_HR=$(bashio::config 'end_hr')
export CRONTAB=$(bashio::config 'crontab')
export PW_CONSUMPTION=$(bashio::config 'pw_consumption')
export MAX_CHARGE=$(bashio::config 'max_charge')
export PW_BATT_RESERVE=$(bashio::config 'pw_batt_reserve')
export DEFAULTS=$(bashio::config 'defaults')

sbam schedule
24 changes: 24 additions & 0 deletions home-assistant/addons/test_local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

BUILDCONTAINER_DATA_PATH="/data"
PATHTOBUILD="$BUILDCONTAINER_DATA_PATH"
ARCH=amd64


PROJECTDIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )/sbam
ROOTDIR=$PROJECTDIR/../../../
cd $PRROOTDIROJECTDIR/../../../
make build
cd -


rm -rf $PROJECTDIR/bin
mkdir -p $PROJECTDIR/bin
cp $ROOTDIR/bin/sbam $PROJECTDIR/bin/

echo "project directory is $PROJECTDIR"
echo "build container data path is $BUILDCONTAINER_DATA_PATH"
echo "build container target build path is $PATHTOBUILD"
CMD="docker run --rm -ti --name hassio-builder --privileged -v $PROJECTDIR:$BUILDCONTAINER_DATA_PATH -v /var/run/docker.sock:/var/run/docker.sock:ro homeassistant/amd64-builder --target $PATHTOBUILD --$ARCH --test --docker-hub local"
echo "$CMD"
$CMD
51 changes: 38 additions & 13 deletions pkg/cmd/configure.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package cmd

import (
"fmt"
"errors"
"sbam/pkg/fronius"
u "sbam/src/utils"
"strings"

"github.com/spf13/cobra"
Expand All @@ -15,20 +16,15 @@ var cfgCmd = &cobra.Command{
Long: `connect via modbus to the fronius inverter and set charging`,
Run: func(cmd *cobra.Command, args []string) {
fronius_ip := viper.GetString("fronius_ip")
if len(strings.TrimSpace(fronius_ip)) == 0 {
fmt.Println("The --fronius_ip flag must be set")
power := viper.GetViper().GetInt("power")

err := checkConfigure(fronius_ip)
if err != nil {
u.Log.Error(err)
return
}
if cmd.Flags().Changed("defaults") {
fronius.Setdefaults(fronius_ip)
} else if cmd.Flags().Changed("force-charge") {
power := viper.GetViper().GetInt("power")
if power == 0 {
fmt.Println("The --power flag must be set when using --force-charge")
return
}
fronius.ForceCharge(fronius_ip, int16(power))
}

configure(fronius_ip, power, cmd)

},
}
Expand All @@ -42,3 +38,32 @@ func init() {
viper.BindPFlag("power", cfgCmd.Flags().Lookup("power"))
rootCmd.AddCommand(cfgCmd)
}

func checkConfigure(fronius_ip string) error {
if len(strings.TrimSpace(fronius_ip)) == 0 {
err := errors.New("the --fronius_ip flag must be set")
return err
}
return nil
}

func configure(fronius_ip string, power int, cmd *cobra.Command) {
if cmd.Flags().Changed("defaults") {
err := fronius.Setdefaults(fronius_ip)
if err != nil {
u.Log.Error(err)
panic(err)
}
} else if cmd.Flags().Changed("force-charge") {
if power == 0 {
u.Log.Error("The --power flag must be set when using --force-charge")
return
}
err := fronius.ForceCharge(fronius_ip, int16(power))
if err != nil {
u.Log.Error(err)
panic(err)
}
}

}
55 changes: 36 additions & 19 deletions pkg/cmd/estimate.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package cmd

import (
"fmt"
"errors"
"sbam/pkg/power"
"sbam/pkg/storage"
u "sbam/src/utils"
"strings"

"github.com/spf13/cobra"
Expand All @@ -18,28 +19,14 @@ var estCmd = &cobra.Command{
url := viper.GetString("url")
apiKey := viper.GetString("apikey")
fronius_ip := viper.GetString("fronius_ip")
if len(strings.TrimSpace(fronius_ip)) == 0 {
fmt.Println("The --fronius_ip flag must be set")
return
} else if len(strings.TrimSpace(apiKey)) == 0 {
fmt.Println("The --apiKey flag must be set")
return
} else if len(strings.TrimSpace(url)) == 0 {
fmt.Println("The --url flag must be set")
return
}

pwr := power.New()
_, err := pwr.Handler(apiKey, url)
err := CheckEstimate(apiKey, url, fronius_ip)
if err != nil {
panic(err)
u.Log.Error(err)
return
}
estimate(apiKey, url, fronius_ip)

str := storage.New()
_, _, err = str.Handler(fronius_ip)
if err != nil {
panic(err)
}
},
}

Expand All @@ -52,3 +39,33 @@ func init() {
viper.BindPFlag("fronius_ip", estCmd.Flags().Lookup("fronius_ip"))
rootCmd.AddCommand(estCmd)
}

func CheckEstimate(apiKey string, url string, fronius_ip string) error {
if len(strings.TrimSpace(fronius_ip)) == 0 {
err := errors.New("the --fronius_ip flag must be set")
return err
} else if len(strings.TrimSpace(apiKey)) == 0 {
err := errors.New("the --apiKey flag must be set")
return err
} else if len(strings.TrimSpace(url)) == 0 {
err := errors.New("the --url flag must be set")
return err
}
return nil
}

func estimate(apiKey string, url string, fronius_ip string) {
pwr := power.New()
_, err := pwr.Handler(apiKey, url)
if err != nil {
u.Log.Error(err)
panic(err)
}

str := storage.New()
_, _, err = str.Handler(fronius_ip)
if err != nil {
u.Log.Error(err)
panic(err)
}
}
Loading

0 comments on commit 6edbab0

Please sign in to comment.