Skip to content

Commit

Permalink
Vaillant: add power reading (#17994)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Jan 12, 2025
1 parent 10b9f31 commit 23c2065
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 53 deletions.
71 changes: 30 additions & 41 deletions charger/vaillant.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ package charger

import (
"context"
"errors"
"fmt"
"reflect"
"strings"
"time"

"github.com/WulfgarW/sensonet"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/samber/lo"
"golang.org/x/oauth2"
)

Expand All @@ -42,7 +44,7 @@ type Vaillant struct {
systemId string
}

//go:generate decorate -f decorateVaillant -b *Vaillant -r api.Charger -t "api.Battery,Soc,func() (float64, error)"
//go:generate decorate -f decorateVaillant -b *Vaillant -r api.Charger -t "api.Meter,CurrentPower,func() (float64, error)" -t "api.Battery,Soc,func() (float64, error)"

// NewVaillantFromConfig creates an Vaillant configurable charger from generic config
func NewVaillantFromConfig(ctx context.Context, other map[string]interface{}) (api.Charger, error) {
Expand All @@ -53,13 +55,15 @@ func NewVaillantFromConfig(ctx context.Context, other map[string]interface{}) (a
HeatingZone int
HeatingSetpoint float32
Phases int
Cache time.Duration
}{
embed: embed{
Icon_: "heatpump",
Features_: []api.Feature{api.Heating, api.IntegratedDevice},
},
Realm: sensonet.REALM_GERMANY,
Phases: 1,
Cache: time.Minute,
}

if err := util.DecodeOther(other, &cc); err != nil {
Expand All @@ -79,9 +83,7 @@ func NewVaillantFromConfig(ctx context.Context, other map[string]interface{}) (a
return nil, err
}

conn, err := sensonet.NewConnection(oc.TokenSource(logCtx, token),
sensonet.WithHttpClient(request.NewClient(log)),
sensonet.WithLogger(log.TRACE))
conn, err := sensonet.NewConnection(oc.TokenSource(logCtx, token), sensonet.WithHttpClient(request.NewClient(log)))
if err != nil {
return nil, err
}
Expand All @@ -92,43 +94,20 @@ func NewVaillantFromConfig(ctx context.Context, other map[string]interface{}) (a
}

systemId := homes[0].SystemID

system, err := conn.GetSystem(systemId)
if err != nil {
return nil, err
}

var strategy int
hotWater := len(system.State.Dhw)+len(system.State.DomesticHotWater) > 0

switch {
case hotWater && cc.HeatingSetpoint != 0:
strategy = sensonet.STRATEGY_HOTWATER_THEN_HEATING
case hotWater:
strategy = sensonet.STRATEGY_HOTWATER
case cc.HeatingSetpoint != 0:
strategy = sensonet.STRATEGY_HEATING
default:
return nil, errors.New("could not determine boost strategy, need either hot water or heating zone and setpoint")
}

heatingPar := sensonet.HeatingParStruct{
ZoneIndex: cc.HeatingZone,
VetoSetpoint: cc.HeatingSetpoint,
VetoDuration: -1, // negative value means: use default
}
hotwaterPar := sensonet.HotwaterParStruct{
Index: -1, // negative value means: use default
}
heating := cc.HeatingZone > 0 && cc.HeatingSetpoint > 0

set := func(mode int64) error {
switch mode {
case Normal:
_, err := conn.StopStrategybased(systemId, &heatingPar, &hotwaterPar)
return err
if heating {
return conn.StopZoneQuickVeto(systemId, cc.HeatingZone)
}
return conn.StopHotWaterBoost(systemId, sensonet.HOTWATERINDEX_DEFAULT)
case Boost:
_, err := conn.StartStrategybased(systemId, strategy, &heatingPar, &hotwaterPar)
return err
if heating {
return conn.StartZoneQuickVeto(systemId, cc.HeatingZone, cc.HeatingSetpoint, sensonet.ZONEVETODURATION_DEFAULT)
}
return conn.StartHotWaterBoost(systemId, sensonet.HOTWATERINDEX_DEFAULT)
default:
return api.ErrNotAvailable
}
Expand All @@ -146,9 +125,19 @@ func NewVaillantFromConfig(ctx context.Context, other map[string]interface{}) (a
SgReady: sgr,
}

var power func() (float64, error)
if devices, _ := conn.GetMpcData(systemId); len(devices) > 0 {
power = provider.Cached(func() (float64, error) {
res, err := conn.GetMpcData(systemId)
return lo.SumBy(res, func(d sensonet.MpcDevice) float64 {
return d.CurrentPower
}), err
}, cc.Cache)
}

var temp func() (float64, error)
if hotWater {
temp = func() (float64, error) {
if !heating {
temp = provider.Cached(func() (float64, error) {
system, err := conn.GetSystem(systemId)
if err != nil {
return 0, err
Expand All @@ -162,10 +151,10 @@ func NewVaillantFromConfig(ctx context.Context, other map[string]interface{}) (a
default:
return 0, api.ErrNotAvailable
}
}
}, cc.Cache)
}

return decorateVaillant(res, temp), nil
return decorateVaillant(res, power, temp), nil
}

func (v *Vaillant) print(chapter int, prefix string, zz ...any) {
Expand Down
40 changes: 37 additions & 3 deletions charger/vaillant_decorators.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/BurntSushi/toml v1.4.0
github.com/Masterminds/sprig/v3 v3.3.0
github.com/PuerkitoBio/goquery v1.10.1
github.com/WulfgarW/sensonet v0.0.2
github.com/WulfgarW/sensonet v0.0.3
github.com/andig/go-powerwall v0.2.1-0.20230808194509-dd70cdb6e140
github.com/andig/gosunspec v0.0.0-20240918203654-860ce51d602b
github.com/andig/mbserver v0.0.0-20230310211055-1d29cbb5820e
Expand All @@ -23,7 +23,7 @@ require (
github.com/cloudfoundry/jibber_jabber v0.0.0-20151120183258-bcc4c8345a21
github.com/coder/websocket v1.8.12
github.com/containrrr/shoutrrr v0.8.0
github.com/coreos/go-oidc/v3 v3.11.0
github.com/coreos/go-oidc/v3 v3.12.0
github.com/denisbrodbeck/machineid v1.0.1
github.com/dmarkham/enumer v1.5.10
github.com/dylanmei/iso8601 v0.1.0
Expand Down Expand Up @@ -99,7 +99,7 @@ require (
golang.org/x/crypto/x509roots/fallback v0.0.0-20241211175049-b4f1988a35de
golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67
golang.org/x/net v0.33.0
golang.org/x/oauth2 v0.24.0
golang.org/x/oauth2 v0.25.0
golang.org/x/sync v0.10.0
golang.org/x/text v0.21.0
golang.org/x/tools v0.28.0
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWX
github.com/Shopify/toxiproxy v2.1.4+incompatible h1:TKdv8HiTLgE5wdJuEML90aBgNWsokNbMijUGhmcoBJc=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
github.com/WulfgarW/sensonet v0.0.2 h1:KRvDs8wxzt72JxuqXGC4ecrZaPYZPD0kPsZZtq4Av38=
github.com/WulfgarW/sensonet v0.0.2/go.mod h1:QZodMch+UskApzlhhGa5ydSY0v5knlF4uQ5WO78xrEs=
github.com/WulfgarW/sensonet v0.0.3 h1:lS+8PVlHTzM4xm1Ok43v9SsDc5h5ZRSdHgyHep+5yJY=
github.com/WulfgarW/sensonet v0.0.3/go.mod h1:zNfnJUhHiDL8V4f9ZC0puUOWpss0F7xqCbJ87UY6dpA=
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
github.com/ahmetb/go-linq/v3 v3.2.0 h1:BEuMfp+b59io8g5wYzNoFe9pWPalRklhlhbiU3hYZDE=
github.com/ahmetb/go-linq/v3 v3.2.0/go.mod h1:haQ3JfOeWK8HpVxMtHHEMPVgBKiYyQ+f1/kLZh/cj9U=
Expand Down Expand Up @@ -96,8 +96,8 @@ github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NA
github.com/coder/websocket v1.8.12/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs=
github.com/containrrr/shoutrrr v0.8.0 h1:mfG2ATzIS7NR2Ec6XL+xyoHzN97H8WPjir8aYzJUSec=
github.com/containrrr/shoutrrr v0.8.0/go.mod h1:ioyQAyu1LJY6sILuNyKaQaw+9Ttik5QePU8atnAdO2o=
github.com/coreos/go-oidc/v3 v3.11.0 h1:Ia3MxdwpSw702YW0xgfmP1GVCMA9aEFWu12XUZ3/OtI=
github.com/coreos/go-oidc/v3 v3.11.0/go.mod h1:gE3LgjOgFoHi9a4ce4/tJczr0Ai2/BoDhf0r5lltWI0=
github.com/coreos/go-oidc/v3 v3.12.0 h1:sJk+8G2qq94rDI6ehZ71Bol3oUHy63qNYmkiSjrc/Jo=
github.com/coreos/go-oidc/v3 v3.12.0/go.mod h1:gE3LgjOgFoHi9a4ce4/tJczr0Ai2/BoDhf0r5lltWI0=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd v0.0.0-20180511133405-39ca1b05acc7/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
Expand Down Expand Up @@ -786,8 +786,8 @@ golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.24.0 h1:KTBBxWqUa0ykRPLtV69rRto9TLXcqYkeswu48x/gvNE=
golang.org/x/oauth2 v0.24.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/oauth2 v0.25.0 h1:CY4y7XT9v0cRI9oupztF8AgiIu99L/ksR/Xp/6jrZ70=
golang.org/x/oauth2 v0.25.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down

0 comments on commit 23c2065

Please sign in to comment.