diff --git a/charger/switchsocket.go b/charger/switchsocket.go index a2824a96fe..a9f9ea8eaa 100644 --- a/charger/switchsocket.go +++ b/charger/switchsocket.go @@ -19,12 +19,15 @@ type SwitchSocket struct { *switchSocket } +//go:generate go run ../cmd/tools/decorate.go -f decorateSwitchSocket -b *SwitchSocket -r api.Charger -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" + func NewSwitchSocketFromConfig(ctx context.Context, other map[string]interface{}) (api.Charger, error) { var cc struct { embed `mapstructure:",squash"` Enabled provider.Config Enable provider.Config Power provider.Config + Energy *provider.Config StandbyPower float64 } @@ -47,13 +50,21 @@ func NewSwitchSocketFromConfig(ctx context.Context, other map[string]interface{} return nil, err } + var energy func() (float64, error) + if cc.Energy != nil { + energy, err = provider.NewFloatGetterFromConfig(ctx, *cc.Energy) + if err != nil { + return nil, err + } + } + c := &SwitchSocket{ enabled: enabled, enable: enable, switchSocket: NewSwitchSocket(&cc.embed, enabled, power, cc.StandbyPower), } - return c, nil + return decorateSwitchSocket(c, energy), nil } func (c *SwitchSocket) Enabled() (bool, error) { diff --git a/charger/switchsocket_decorators.go b/charger/switchsocket_decorators.go new file mode 100644 index 0000000000..c94042b316 --- /dev/null +++ b/charger/switchsocket_decorators.go @@ -0,0 +1,35 @@ +package charger + +// Code generated by github.com/evcc-io/evcc/cmd/tools/decorate.go. DO NOT EDIT. + +import ( + "github.com/evcc-io/evcc/api" +) + +func decorateSwitchSocket(base *SwitchSocket, meterEnergy func() (float64, error)) api.Charger { + switch { + case meterEnergy == nil: + return base + + case meterEnergy != nil: + return &struct { + *SwitchSocket + api.MeterEnergy + }{ + SwitchSocket: base, + MeterEnergy: &decorateSwitchSocketMeterEnergyImpl{ + meterEnergy: meterEnergy, + }, + } + } + + return nil +} + +type decorateSwitchSocketMeterEnergyImpl struct { + meterEnergy func() (float64, error) +} + +func (impl *decorateSwitchSocketMeterEnergyImpl) TotalEnergy() (float64, error) { + return impl.meterEnergy() +}