evcc-io/api/actionconfig.go
Michael Geers 5cc4c146ee
Some checks failed
Release / call-build-workflow (push) Has been cancelled
Release / Publish Docker :release (push) Has been cancelled
Release / Github & APT (push) Has been cancelled
Release / Demo (push) Has been cancelled
Release / Hassio Addon (push) Has been cancelled
Restore vehicle onIdentify behavior for targetSoc from yaml (#9685)
* Fix vehicle onIdentify for targetSoc

* fix onDisconnect behavior if config exists; update tests

* deprecated config-based minsoc; api/ui only setting

* fix tests
2023-09-03 16:29:13 +02:00

46 lines
1.4 KiB
Go

package api
import (
"fmt"
"reflect"
"strings"
"github.com/fatih/structs"
"github.com/imdario/mergo"
"github.com/jinzhu/copier"
)
// ActionConfig defines an action to take on event
type ActionConfig struct {
Mode *ChargeMode `mapstructure:"mode,omitempty"` // Charge Mode
MinCurrent *float64 `mapstructure:"minCurrent,omitempty"` // Minimum Current
MaxCurrent *float64 `mapstructure:"maxCurrent,omitempty"` // Maximum Current
MinSoc_ *int `mapstructure:"minSoc,omitempty"` // Minimum Soc (deprecated)
TargetSoc *int `mapstructure:"targetSoc,omitempty"` // Target Soc
Priority *int `mapstructure:"priority,omitempty"` // Priority
}
// Merge merges all non-nil properties of the additional config into the base config.
// The receiver's config remains immutable.
func (a ActionConfig) Merge(m ActionConfig) ActionConfig {
var res ActionConfig
if err := copier.Copy(&res, a); err != nil {
panic(err)
}
if err := mergo.MergeWithOverwrite(&res, m); err != nil {
panic(err)
}
return res
}
// String implements Stringer and returns the ActionConfig as comma-separated key:value string
func (a ActionConfig) String() string {
var s []string
for k, v := range structs.Map(a) {
val := reflect.ValueOf(v)
if v != nil && !val.IsNil() {
s = append(s, fmt.Sprintf("%s:%v", k, val.Elem()))
}
}
return strings.Join(s, ", ")
}