From b1293cdf3f3930de2a3f9c59081997f701ec9ddd Mon Sep 17 00:00:00 2001 From: andig Date: Mon, 5 Aug 2024 13:29:25 +0200 Subject: [PATCH] Fix determining required battery mode (#15255) --- core/site_battery.go | 4 ++-- core/site_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/core/site_battery.go b/core/site_battery.go index e292fad02..08da6f36f 100644 --- a/core/site_battery.go +++ b/core/site_battery.go @@ -41,8 +41,8 @@ func (site *Site) requiredBatteryMode(batteryGridChargeActive bool, rate api.Rat batMode := site.GetBatteryMode() switch { - case batteryGridChargeActive && batMode != api.BatteryCharge: - res = api.BatteryCharge + case batteryGridChargeActive: + res = map[bool]api.BatteryMode{false: api.BatteryCharge, true: api.BatteryUnknown}[batMode == api.BatteryCharge] case !batteryGridChargeActive && site.dischargeControlActive(rate) && batMode != api.BatteryHold: res = api.BatteryHold case batteryModeModified(batMode): diff --git a/core/site_test.go b/core/site_test.go index d03a78011..49851d94d 100644 --- a/core/site_test.go +++ b/core/site_test.go @@ -3,7 +3,9 @@ package core import ( "testing" + "github.com/evcc-io/evcc/api" "github.com/evcc-io/evcc/util" + "github.com/stretchr/testify/assert" ) func TestSitePower(t *testing.T) { @@ -143,3 +145,31 @@ func TestGreenShare(t *testing.T) { } } } + +func TestRequiredBatteryMode(t *testing.T) { + tc := []struct { + gridChargeActive bool + mode, res api.BatteryMode + }{ + {false, api.BatteryUnknown, api.BatteryUnknown}, // ignore + {false, api.BatteryNormal, api.BatteryUnknown}, // ignore + {false, api.BatteryHold, api.BatteryNormal}, + {false, api.BatteryCharge, api.BatteryNormal}, + + {true, api.BatteryUnknown, api.BatteryCharge}, + {true, api.BatteryNormal, api.BatteryCharge}, + {true, api.BatteryHold, api.BatteryCharge}, + {true, api.BatteryCharge, api.BatteryUnknown}, // ignore + } + + for _, tc := range tc { + t.Logf("%+vv", tc) + + s := &Site{ + batteryMode: tc.mode, + } + + res := s.requiredBatteryMode(tc.gridChargeActive, api.Rate{}) + assert.Equal(t, tc.res, res, "expected %s, got %s", tc.res, res) + } +}