Skip to content

Commit

Permalink
Fix resetOnDisconnect overriding default vehicle properties (#4551)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Sep 22, 2022
1 parent ccaea78 commit ab71b6d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
21 changes: 21 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ type ActionConfig struct {
TargetSoC *int `mapstructure:"targetSoC,omitempty"` // Target SoC
}

// 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 {
if m.Mode != nil {
a.Mode = m.Mode
}
if m.MinCurrent != nil {
a.MinCurrent = m.MinCurrent
}
if m.MaxCurrent != nil {
a.MaxCurrent = m.MaxCurrent
}
if m.MinSoC != nil {
a.MinSoC = m.MinSoC
}
if m.TargetSoC != nil {
a.TargetSoC = m.TargetSoC
}
return a
}

// String implements Stringer and returns the ActionConfig as comma-separated key:value string
func (a ActionConfig) String() string {
var s []string
Expand Down
7 changes: 5 additions & 2 deletions core/loadpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,11 @@ func (lp *LoadPoint) evVehicleDisconnectHandler() {

// set default mode on disconnect
if lp.ResetOnDisconnect {
// TODO respect defaultVehicle
lp.applyAction(lp.onDisconnect)
actionCfg := lp.onDisconnect
if v := lp.defaultVehicle; v != nil {
actionCfg = actionCfg.Merge(v.OnIdentified())
}
lp.applyAction(actionCfg)
}

// soc update reset
Expand Down
25 changes: 22 additions & 3 deletions core/loadpoint_vehicle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,19 @@ func TestVehicleDetectByID(t *testing.T) {
func TestDefaultVehicle(t *testing.T) {
ctrl := gomock.NewController(t)

mode := api.ModePV
minsoc := 20
targetsoc := 80

dflt := mock.NewMockVehicle(ctrl)
dflt.EXPECT().Title().Return("default").AnyTimes()
dflt.EXPECT().Capacity().AnyTimes()
dflt.EXPECT().Phases().AnyTimes()
dflt.EXPECT().OnIdentified().AnyTimes()
dflt.EXPECT().OnIdentified().Return(api.ActionConfig{
Mode: &mode,
MinSoC: &minsoc,
TargetSoC: &targetsoc,
}).AnyTimes()

vehicle := mock.NewMockVehicle(ctrl)
vehicle.EXPECT().Title().Return("target").AnyTimes()
Expand All @@ -94,6 +102,7 @@ func TestDefaultVehicle(t *testing.T) {

lp := NewLoadPoint(util.NewLogger("foo"))
lp.defaultVehicle = dflt
lp.collectDefaults()

// populate channels
x, y, z := createChannels(t)
Expand All @@ -119,9 +128,19 @@ func TestDefaultVehicle(t *testing.T) {
}

// default vehicle disconnected
lp.ResetOnDisconnect = true
lp.evVehicleDisconnectHandler()
if lp.vehicle != dflt {
t.Errorf("expected %v, got %v", title(dflt), title(lp.vehicle))
if m := lp.GetMode(); m != mode {
t.Errorf("expected mode %v, got %v", mode, m)
}
if s := lp.GetMinSoC(); s != minsoc {
t.Errorf("expected minsoc %v, got %v", minsoc, s)
}
if s := lp.GetTargetSoC(); s != targetsoc {
t.Errorf("expected targetsoc %v, got %v", targetsoc, s)
}
if m := lp.onDisconnect.Mode; *m != api.ModeOff {
t.Errorf("expected ondisconnect mode %v, got %v", api.ModeOff, m)
}

// set non-default vehicle during disconnect - should be default on connect
Expand Down

0 comments on commit ab71b6d

Please sign in to comment.