From 4d53782b6c07705f111f87d6805569997a9116fa Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 12 Apr 2026 14:42:07 +0200 Subject: [PATCH] chore: add hems-pro platform (#25835) --- go.mod | 2 ++ go.sum | 4 ++++ util/sponsor/auth.go | 15 ++++++++++----- util/sponsor/hemspro.go | 35 +++++++++++++++++++++++++++++++++++ util/sponsor/hemspro_linux.go | 16 ++++++++++++++++ util/sponsor/pulsares.go | 2 +- util/sponsor/victron.go | 2 ++ 7 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 util/sponsor/hemspro.go create mode 100644 util/sponsor/hemspro_linux.go diff --git a/go.mod b/go.mod index 732ea8cc7..478b12dea 100644 --- a/go.mod +++ b/go.mod @@ -25,6 +25,7 @@ require ( github.com/cloudfoundry/jibber_jabber v0.0.0-20151120183258-bcc4c8345a21 github.com/coder/websocket v1.8.14 github.com/coreos/go-oidc/v3 v3.17.0 + github.com/d2r2/go-i2c v0.0.0-20191123181816-73a8a799d6bc github.com/denisbrodbeck/machineid v1.0.1 github.com/dylanmei/iso8601 v0.1.0 github.com/eclipse/paho.mqtt.golang v1.5.1 @@ -145,6 +146,7 @@ require ( github.com/creack/goselect v0.1.2 // indirect github.com/cronokirby/saferith v0.33.0 // indirect github.com/cstockton/go-conv v1.0.0 // indirect + github.com/d2r2/go-logger v0.0.0-20210606094344-60e9d1233e22 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/dmarkham/enumer v1.6.1 // indirect github.com/donovanhide/eventsource v0.0.0-20210830082556-c59027999da0 // indirect diff --git a/go.sum b/go.sum index 3ef475b07..d029cc289 100644 --- a/go.sum +++ b/go.sum @@ -148,6 +148,10 @@ github.com/cronokirby/saferith v0.33.0 h1:TgoQlfsD4LIwx71+ChfRcIpjkw+RPOapDEVxa+ github.com/cronokirby/saferith v0.33.0/go.mod h1:QKJhjoqUtBsXCAVEjw38mFqoi7DebT7kthcD7UzbnoA= github.com/cstockton/go-conv v1.0.0 h1:zj/q/0MpQ/97XfiC9glWiohO8lhgR4TTnHYZifLTv6I= github.com/cstockton/go-conv v1.0.0/go.mod h1:HuiHkkRgOA0IoBNPC7ysG7kNpjDYlgM7Kj62yQPxjy4= +github.com/d2r2/go-i2c v0.0.0-20191123181816-73a8a799d6bc h1:HLRSIWzUGMLCq4ldt0W1GLs3nnAxa5EGoP+9qHgh6j0= +github.com/d2r2/go-i2c v0.0.0-20191123181816-73a8a799d6bc/go.mod h1:AwxDPnsgIpy47jbGXZHA9Rv7pDkOJvQbezPuK1Y+nNk= +github.com/d2r2/go-logger v0.0.0-20210606094344-60e9d1233e22 h1:nO+SY4KOMsF/LsZ5EtbSKhiT3M6sv/igo2PEru/xEHI= +github.com/d2r2/go-logger v0.0.0-20210606094344-60e9d1233e22/go.mod h1:eSx+YfcVy5vCjRZBNIhpIpfCGFMQ6XSOSQkDk7+VCpg= github.com/dave/jennifer v1.7.1 h1:B4jJJDHelWcDhlRQxWeo0Npa/pYKBLrirAQoTN45txo= github.com/dave/jennifer v1.7.1/go.mod h1:nXbxhEmQfOZhWml3D1cDK5M1FLnMSozpbFN/m3RmGZc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/util/sponsor/auth.go b/util/sponsor/auth.go index eecb0e68e..aa42f0e60 100644 --- a/util/sponsor/auth.go +++ b/util/sponsor/auth.go @@ -20,6 +20,7 @@ package sponsor import ( "context" "fmt" + "os" "strings" "sync" "time" @@ -36,10 +37,7 @@ var ( ExpiresAt time.Time ) -const ( - unavailable = "sponsorship unavailable" - victron = "victron" -) +const unavailable = "sponsorship unavailable" func IsAuthorized() bool { mu.RLock() @@ -64,8 +62,15 @@ func ConfigureSponsorship(token string) error { return nil } + if os.Getenv("HEMSPRO") != "" { + if sub := checkHemsPro(); sub != "" { + Subject = sub + return nil + } + } + var err error - if token, err = readSerial(); token == "" || err != nil { + if token, err = checkPulsares(); token == "" || err != nil { return err } } diff --git a/util/sponsor/hemspro.go b/util/sponsor/hemspro.go new file mode 100644 index 000000000..7e1bdabef --- /dev/null +++ b/util/sponsor/hemspro.go @@ -0,0 +1,35 @@ +//go:build !linux + +package sponsor + +import ( + i2c "github.com/d2r2/go-i2c" +) + +const hemspro = "hemspro" + +// checkHemsPro checks if the hardware is a supported HEMS Pro device and returns sponsor subject +func checkHemsPro() string { + const ( + ADDR = 0b1101000 // 0x68 DS1307 + REG_TIMEDATE = 0x00 + ) + + // Create new connection to I2C bus 1 + i2c, err := i2c.NewI2C(ADDR, 1) + if err != nil { + return "" + } + defer i2c.Close() + + if _, err := i2c.WriteBytes([]byte{REG_TIMEDATE}); err != nil { + return "" + } + + buf := make([]byte, 7) + if n, err := i2c.ReadBytes(buf); err != nil || n != 7 { + return "" + } + + return hemspro +} diff --git a/util/sponsor/hemspro_linux.go b/util/sponsor/hemspro_linux.go new file mode 100644 index 000000000..cfbdf8eed --- /dev/null +++ b/util/sponsor/hemspro_linux.go @@ -0,0 +1,16 @@ +//go:build linux + +package sponsor + +const hemspro = "hemspro" + +// checkHemsPro checks if the hardware is a supported HEMS Pro device and returns sponsor subject +func checkHemsPro() string { + // TODO i2c validation + + if false { + return hemspro + } + + return "" +} diff --git a/util/sponsor/pulsares.go b/util/sponsor/pulsares.go index 0012c76d6..66bf88458 100644 --- a/util/sponsor/pulsares.go +++ b/util/sponsor/pulsares.go @@ -23,7 +23,7 @@ import ( "time" ) -func readSerial() (string, error) { +func checkPulsares() (string, error) { f, err := os.OpenFile("/dev/PulsaresSerial", os.O_RDWR, 0o644) if err != nil { return "", nil diff --git a/util/sponsor/victron.go b/util/sponsor/victron.go index c74bc5e11..51ad01736 100644 --- a/util/sponsor/victron.go +++ b/util/sponsor/victron.go @@ -32,6 +32,8 @@ import ( "google.golang.org/grpc/status" ) +const victron = "victron" + // checkVictron checks if the hardware is a supported victron device and returns sponsor subject func checkVictron() string { vd, err := victronDeviceInfo()