Add battery grid charging (#14855)

This commit is contained in:
andig 2024-08-03 15:23:50 +02:00 • committed by GitHub
parent bbf989c3c0
commit b0ea0582e2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
37 changed files with 797 additions and 489 deletions

View file

@ -1,6 +1,7 @@
package core
import (
"fmt"
"time"
"github.com/cenkalti/backoff/v4"
@ -51,3 +52,19 @@ func sitePower(log *util.Logger, maxGrid, grid, battery, residual float64) float
return grid + battery + residual
}
// printPtr returns a string representation of a pointer value
func printPtr[T any](format string, v *T) string {
if v == nil {
return "<nil>"
}
return fmt.Sprintf(format, *v)
}
func ptrValueEqual[T comparable](a, b *T) bool {
if (a == nil) != (b == nil) {
return false
}
return a == nil && b == nil || (*a) == (*b)
}

View file

@ -39,6 +39,8 @@ const (
// battery settings
BatteryCapacity = "batteryCapacity"
BatteryDischargeControl = "batteryDischargeControl"
BatteryGridChargeLimit = "batteryGridChargeLimit"
BatteryGridChargeActive = "batteryGridChargeActive"
BufferSoc = "bufferSoc"
BufferStartSoc = "bufferStartSoc"
MaxGridSupplyWhileBatteryCharging = "maxGridSupplyWhileBatteryCharging"

View file

@ -121,12 +121,12 @@ type Loadpoint struct {
MinCurrent_ float64 `mapstructure:"minCurrent"`
MaxCurrent_ float64 `mapstructure:"maxCurrent"`
minCurrent float64 // PV mode: start current Min+PV mode: min current
maxCurrent float64 // Max allowed current. Physically ensured by the charger
configuredPhases int // Charger configured phase mode 0/1/3
limitSoc int // Session limit for soc
limitEnergy float64 // Session limit for energy
smartCostLimit float64 // always charge if cost is below this value
minCurrent float64 // PV mode: start current Min+PV mode: min current
maxCurrent float64 // Max allowed current. Physically ensured by the charger
configuredPhases int // Charger configured phase mode 0/1/3
limitSoc int // Session limit for soc
limitEnergy float64 // Session limit for energy
smartCostLimit *float64 // always charge if cost is below this value
mode api.ChargeMode
enabled bool // Charger enabled state
@ -337,8 +337,9 @@ func (lp *Loadpoint) restoreSettings() {
lp.setLimitEnergy(v)
}
if v, err := lp.settings.Float(keys.SmartCostLimit); err == nil {
lp.SetSmartCostLimit(v)
lp.SetSmartCostLimit(&v)
}
t, err1 := lp.settings.Time(keys.PlanTime)
v, err2 := lp.settings.Float(keys.PlanEnergy)
if err1 == nil && err2 == nil {
@ -1637,9 +1638,18 @@ func (lp *Loadpoint) phaseSwitchCompleted() bool {
}
// Update is the main control function. It reevaluates meters and charger state
func (lp *Loadpoint) Update(sitePower float64, smartCostActive bool, smartCostNextStart time.Time, batteryBuffered, batteryStart bool, greenShare float64, effPrice, effCo2 *float64) {
func (lp *Loadpoint) Update(sitePower float64, rates api.Rates, batteryBuffered, batteryStart bool, greenShare float64, effPrice, effCo2 *float64) {
// smart cost
smartCostActive := lp.smartCostActive(rates)
lp.publish(keys.SmartCostActive, smartCostActive)
var smartCostNextStart time.Time
if !smartCostActive {
smartCostNextStart = lp.smartCostNextStart(rates)
}
lp.publish(keys.SmartCostNextStart, smartCostNextStart)
// long-running tasks
lp.processTasks()
// read and publish meters first- charge power and currents have already been updated by the site

View file

@ -110,9 +110,9 @@ type API interface {
//
// GetSmartChargingActive determines if smart charging is active
GetSmartCostLimit() float64
GetSmartCostLimit() *float64
// SetSmartCostLimit sets the smart cost limit
SetSmartCostLimit(limit float64)
SetSmartCostLimit(limit *float64)
//
// power and energy

View file

@ -380,10 +380,10 @@ func (mr *MockAPIMockRecorder) GetRemainingEnergy() *gomock.Call {
}
// GetSmartCostLimit mocks base method.
func (m *MockAPI) GetSmartCostLimit() float64 {
func (m *MockAPI) GetSmartCostLimit() *float64 {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetSmartCostLimit")
ret0, _ := ret[0].(float64)
ret0, _ := ret[0].(*float64)
return ret0
}
@ -602,7 +602,7 @@ func (mr *MockAPIMockRecorder) SetPriority(arg0 any) *gomock.Call {
}
// SetSmartCostLimit mocks base method.
func (m *MockAPI) SetSmartCostLimit(arg0 float64) {
func (m *MockAPI) SetSmartCostLimit(arg0 *float64) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "SetSmartCostLimit", arg0)
}

View file

@ -489,23 +489,29 @@ func (lp *Loadpoint) StartVehicleDetection() {
}
// GetSmartCostLimit gets the smart cost limit
func (lp *Loadpoint) GetSmartCostLimit() float64 {
func (lp *Loadpoint) GetSmartCostLimit() *float64 {
lp.RLock()
defer lp.RUnlock()
return lp.smartCostLimit
}
// SetSmartCostLimit sets the smart cost limit
func (lp *Loadpoint) SetSmartCostLimit(val float64) {
func (lp *Loadpoint) SetSmartCostLimit(val *float64) {
lp.Lock()
defer lp.Unlock()
lp.log.DEBUG.Println("set smart cost limit:", val)
lp.log.DEBUG.Println("set smart cost limit:", printPtr("%.1f", val))
if lp.smartCostLimit != val {
if !ptrValueEqual(lp.smartCostLimit, val) {
lp.smartCostLimit = val
lp.settings.SetFloat(keys.SmartCostLimit, lp.smartCostLimit)
lp.publish(keys.SmartCostLimit, lp.smartCostLimit)
if val == nil {
lp.settings.SetString(keys.SmartCostLimit, "")
lp.publish(keys.SmartCostLimit, nil)
} else {
lp.settings.SetFloat(keys.SmartCostLimit, *val)
lp.publish(keys.SmartCostLimit, *val)
}
}
}

View file

@ -0,0 +1,31 @@
package core
import (
"time"
"github.com/evcc-io/evcc/api"
)
func (lp *Loadpoint) smartCostActive(rates api.Rates) bool {
// potential error has already been logged by site, ignore
rate, _ := rates.Current(time.Now())
limit := lp.GetSmartCostLimit()
return limit != nil && !rate.IsEmpty() && rate.Price <= *limit
}
// smartCostNextStart returns the next start time for a smart cost rate below the limit
func (lp *Loadpoint) smartCostNextStart(rates api.Rates) time.Time {
limit := lp.GetSmartCostLimit()
if limit == nil || rates == nil {
return time.Time{}
}
now := time.Now()
for _, slot := range rates {
if slot.Start.After(now) && slot.Price <= *limit {
return slot.Start
}
}
return time.Time{}
}

View file

@ -182,7 +182,7 @@ func TestUpdatePowerZero(t *testing.T) {
}
lp.mode = tc.mode
lp.Update(0, false, time.Time{}, false, false, 0, nil, nil) // false,sitePower false,0
lp.Update(0, nil, false, false, 0, nil, nil) // false,sitePower false,0
ctrl.Finish()
}
@ -428,7 +428,7 @@ func TestDisableAndEnableAtTargetSoc(t *testing.T) {
charger.EXPECT().Status().Return(api.StatusC, nil)
charger.EXPECT().Enabled().Return(lp.enabled, nil)
charger.EXPECT().MaxCurrent(int64(maxA)).Return(nil)
lp.Update(500, false, time.Time{}, false, false, 0, nil, nil)
lp.Update(500, nil, false, false, 0, nil, nil)
ctrl.Finish()
t.Log("charging above target - soc deactivates charger")
@ -437,7 +437,7 @@ func TestDisableAndEnableAtTargetSoc(t *testing.T) {
charger.EXPECT().Status().Return(api.StatusC, nil)
charger.EXPECT().Enabled().Return(lp.enabled, nil)
charger.EXPECT().Enable(false).Return(nil)
lp.Update(500, false, time.Time{}, false, false, 0, nil, nil)
lp.Update(500, nil, false, false, 0, nil, nil)
ctrl.Finish()
t.Log("deactivated charger changes status to B")
@ -445,14 +445,14 @@ func TestDisableAndEnableAtTargetSoc(t *testing.T) {
vehicle.EXPECT().Soc().Return(95.0, nil)
charger.EXPECT().Status().Return(api.StatusB, nil)
charger.EXPECT().Enabled().Return(lp.enabled, nil)
lp.Update(-5000, false, time.Time{}, false, false, 0, nil, nil)
lp.Update(-500, nil, false, false, 0, nil, nil)
ctrl.Finish()
t.Log("soc has risen below target - soc update prevented by timer")
clock.Add(5 * time.Minute)
charger.EXPECT().Status().Return(api.StatusB, nil)
charger.EXPECT().Enabled().Return(lp.enabled, nil)
lp.Update(-5000, false, time.Time{}, false, false, 0, nil, nil)
lp.Update(-500, nil, false, false, 0, nil, nil)
ctrl.Finish()
t.Log("soc has fallen below target - soc update timer expired")
@ -462,7 +462,7 @@ func TestDisableAndEnableAtTargetSoc(t *testing.T) {
charger.EXPECT().Enabled().Return(lp.enabled, nil)
charger.EXPECT().MaxCurrent(int64(maxA)).Return(nil)
charger.EXPECT().Enable(true).Return(nil)
lp.Update(-5000, false, time.Time{}, false, false, 0, nil, nil)
lp.Update(-500, nil, false, false, 0, nil, nil)
ctrl.Finish()
}
@ -497,14 +497,14 @@ func TestSetModeAndSocAtDisconnect(t *testing.T) {
charger.EXPECT().Enabled().Return(lp.enabled, nil)
charger.EXPECT().Status().Return(api.StatusC, nil)
charger.EXPECT().MaxCurrent(int64(maxA)).Return(nil)
lp.Update(500, false, time.Time{}, false, false, 0, nil, nil)
lp.Update(500, nil, false, false, 0, nil, nil)
t.Log("switch off when disconnected")
clock.Add(5 * time.Minute)
charger.EXPECT().Enabled().Return(lp.enabled, nil)
charger.EXPECT().Status().Return(api.StatusA, nil)
charger.EXPECT().Enable(false).Return(nil)
lp.Update(-3000, false, time.Time{}, false, false, 0, nil, nil)
lp.Update(-300, nil, false, false, 0, nil, nil)
if mode := lp.GetMode(); mode != api.ModeOff {
t.Error("unexpected mode", mode)
@ -567,14 +567,14 @@ func TestChargedEnergyAtDisconnect(t *testing.T) {
rater.EXPECT().ChargedEnergy().Return(0.0, nil)
charger.EXPECT().Enabled().Return(lp.enabled, nil)
charger.EXPECT().Status().Return(api.StatusC, nil)
lp.Update(-1, false, time.Time{}, false, false, 0, nil, nil)
lp.Update(-1, nil, false, false, 0, nil, nil)
t.Log("at 1:00h charging at 5 kWh")
clock.Add(time.Hour)
rater.EXPECT().ChargedEnergy().Return(5.0, nil)
charger.EXPECT().Enabled().Return(lp.enabled, nil)
charger.EXPECT().Status().Return(api.StatusC, nil)
lp.Update(-1, false, time.Time{}, false, false, 0, nil, nil)
lp.Update(-1, nil, false, false, 0, nil, nil)
expectCache("chargedEnergy", 5000.0)
t.Log("at 1:00h stop charging at 5 kWh")
@ -582,7 +582,7 @@ func TestChargedEnergyAtDisconnect(t *testing.T) {
rater.EXPECT().ChargedEnergy().Return(5.0, nil)
charger.EXPECT().Enabled().Return(lp.enabled, nil)
charger.EXPECT().Status().Return(api.StatusB, nil)
lp.Update(-1, false, time.Time{}, false, false, 0, nil, nil)
lp.Update(-1, nil, false, false, 0, nil, nil)
expectCache("chargedEnergy", 5000.0)
t.Log("at 1:00h restart charging at 5 kWh")
@ -590,7 +590,7 @@ func TestChargedEnergyAtDisconnect(t *testing.T) {
rater.EXPECT().ChargedEnergy().Return(5.0, nil)
charger.EXPECT().Enabled().Return(lp.enabled, nil)
charger.EXPECT().Status().Return(api.StatusC, nil)
lp.Update(-1, false, time.Time{}, false, false, 0, nil, nil)
lp.Update(-1, nil, false, false, 0, nil, nil)
expectCache("chargedEnergy", 5000.0)
t.Log("at 1:30h continue charging at 7.5 kWh")
@ -598,7 +598,7 @@ func TestChargedEnergyAtDisconnect(t *testing.T) {
rater.EXPECT().ChargedEnergy().Return(7.5, nil)
charger.EXPECT().Enabled().Return(lp.enabled, nil)
charger.EXPECT().Status().Return(api.StatusC, nil)
lp.Update(-1, false, time.Time{}, false, false, 0, nil, nil)
lp.Update(-1, nil, false, false, 0, nil, nil)
expectCache("chargedEnergy", 7500.0)
t.Log("at 2:00h stop charging at 10 kWh")
@ -606,7 +606,7 @@ func TestChargedEnergyAtDisconnect(t *testing.T) {
rater.EXPECT().ChargedEnergy().Return(10.0, nil)
charger.EXPECT().Enabled().Return(lp.enabled, nil)
charger.EXPECT().Status().Return(api.StatusB, nil)
lp.Update(-1, false, time.Time{}, false, false, 0, nil, nil)
lp.Update(-1, nil, false, false, 0, nil, nil)
expectCache("chargedEnergy", 10000.0)
ctrl.Finish()

View file

@ -270,7 +270,7 @@ func TestReconnectVehicle(t *testing.T) {
// vehicle not updated yet
vehicle.MockChargeState.EXPECT().Status().Return(api.StatusA, nil)
lp.Update(0, false, time.Time{}, false, false, 0, nil, nil)
lp.Update(0, nil, false, false, 0, nil, nil)
ctrl.Finish()
// detection started
@ -284,7 +284,7 @@ func TestReconnectVehicle(t *testing.T) {
// vehicle not updated yet
vehicle.MockChargeState.EXPECT().Status().Return(api.StatusB, nil)
lp.Update(0, false, time.Time{}, false, false, 0, nil, nil)
lp.Update(0, nil, false, false, 0, nil, nil)
ctrl.Finish()
// vehicle detected

View file

@ -52,7 +52,7 @@ func AverageCost(plan api.Rates) float64 {
// SlotAt returns the slot for the given time or an empty slot
func SlotAt(time time.Time, plan api.Rates) api.Rate {
for _, slot := range plan {
if (slot.Start.Before(time) || slot.Start.Equal(time)) && slot.End.After(time) {
if !slot.Start.After(time) && slot.End.After(time) {
return slot
}
}

View file

@ -89,12 +89,12 @@ func (t *Planner) continuousPlan(rates api.Rates, start, end time.Time) api.Rate
res := make(api.Rates, 0, len(rates)+2)
for _, r := range rates {
// slot before continuous plan
if r.End.Before(start) || r.End.Equal(start) {
if !r.End.After(start) {
continue
}
// slot after continuous plan
if r.Start.After(end) || r.Start.Equal(end) {
if !r.Start.Before(end) {
continue
}

View file

@ -37,7 +37,7 @@ const standbyPower = 10 // consider less than 10W as charger in standby
// updater abstracts the Loadpoint implementation for testing
type updater interface {
loadpoint.API
Update(availablePower float64, smartCostActive bool, smartCostNextStart time.Time, batteryBuffered, batteryStart bool, greenShare float64, effectivePrice, effectiveCo2 *float64)
Update(availablePower float64, rates api.Rates, batteryBuffered, batteryStart bool, greenShare float64, effectivePrice, effectiveCo2 *float64)
}
// meterMeasurement is used as slice element for publishing structured data
@ -85,10 +85,11 @@ type Site struct {
auxMeters []api.Meter // Auxiliary meters
// battery settings
prioritySoc float64 // prefer battery up to this Soc
bufferSoc float64 // continue charging on battery above this Soc
bufferStartSoc float64 // start charging on battery above this Soc
batteryDischargeControl bool // prevent battery discharge for fast and planned charging
prioritySoc float64 // prefer battery up to this Soc
bufferSoc float64 // continue charging on battery above this Soc
bufferStartSoc float64 // start charging on battery above this Soc
batteryDischargeControl bool // prevent battery discharge for fast and planned charging
batteryGridChargeLimit *float64 // grid charging limit
loadpoints []*Loadpoint // Loadpoints
tariffs *tariff.Tariffs // Tariffs
@ -298,6 +299,10 @@ func (site *Site) restoreSettings() error {
return err
}
}
if v, err := settings.Float(keys.BatteryGridChargeLimit); err == nil {
site.SetBatteryGridChargeLimit(&v)
}
return nil
}
@ -792,19 +797,25 @@ func (site *Site) update(lp updater) {
flexiblePower = site.prioritizer.GetChargePowerFlexibility(lp)
}
var smartCostActive bool
if rate, err := site.plannerRate(); err == nil {
smartCostActive = site.smartCostActive(lp, rate)
} else {
site.log.WARN.Println("smartCostActive:", err)
// battery mode handling
rates, err := site.plannerRates()
if err != nil {
site.log.WARN.Println("planner:", err)
}
var smartCostNextStart time.Time
if !smartCostActive {
if rates, err := site.plannerRates(); err == nil {
smartCostNextStart = site.smartCostNextStart(lp, rates)
rate, err := rates.Current(time.Now())
if rates != nil && err != nil {
site.log.WARN.Println("planner:", err)
}
batteryGridChargeActive := site.batteryGridChargeActive(rate)
site.publish(keys.BatteryGridChargeActive, batteryGridChargeActive)
if batteryMode := site.requiredBatteryMode(batteryGridChargeActive, rate); batteryMode != api.BatteryUnknown {
if err := site.applyBatteryMode(batteryMode); err == nil {
site.SetBatteryMode(batteryMode)
} else {
site.log.WARN.Println("smartCostNextStart:", err)
site.log.ERROR.Println("battery mode:", err)
}
}
@ -820,7 +831,7 @@ func (site *Site) update(lp updater) {
greenShareHome := site.greenShare(0, homePower)
greenShareLoadpoints := site.greenShare(nonChargePower, nonChargePower+totalChargePower)
lp.Update(sitePower, smartCostActive, smartCostNextStart, batteryBuffered, batteryStart, greenShareLoadpoints, site.effectivePrice(greenShareLoadpoints), site.effectiveCo2(greenShareLoadpoints))
lp.Update(sitePower, rates, batteryBuffered, batteryStart, greenShareLoadpoints, site.effectivePrice(greenShareLoadpoints), site.effectiveCo2(greenShareLoadpoints))
site.Health.Update()
@ -833,10 +844,6 @@ func (site *Site) update(lp updater) {
site.log.ERROR.Println(err)
}
if site.GetBatteryDischargeControl() {
site.updateBatteryMode()
}
site.stats.Update(site)
}

View file

@ -40,6 +40,11 @@ type API interface {
GetMaxGridSupplyWhileBatteryCharging() float64
SetMaxGridSupplyWhileBatteryCharging(float64) error
// GetBatteryGridChargeLimit get the grid charge limit
GetBatteryGridChargeLimit() *float64
// SetBatteryGridChargeLimit sets the grid charge limit
SetBatteryGridChargeLimit(limit *float64)
//
// power and energy
//

View file

@ -347,13 +347,6 @@ func (site *Site) SetBatteryDischargeControl(val bool) error {
defer site.Unlock()
if site.batteryDischargeControl != val {
// reset to normal when disabling
if mode := site.batteryMode; !val && batteryModeModified(mode) {
if err := site.applyBatteryMode(api.BatteryNormal); err != nil {
return err
}
}
site.batteryDischargeControl = val
settings.SetBool(keys.BatteryDischargeControl, val)
site.publish(keys.BatteryDischargeControl, val)
@ -361,3 +354,28 @@ func (site *Site) SetBatteryDischargeControl(val bool) error {
return nil
}
func (site *Site) GetBatteryGridChargeLimit() *float64 {
site.RLock()
defer site.RUnlock()
return site.batteryGridChargeLimit
}
func (site *Site) SetBatteryGridChargeLimit(val *float64) {
site.log.DEBUG.Println("set grid charge limit:", printPtr("%.1f", val))
site.Lock()
defer site.Unlock()
if !ptrValueEqual(site.batteryGridChargeLimit, val) {
site.batteryGridChargeLimit = val
if val == nil {
settings.SetString(keys.BatteryGridChargeLimit, "")
site.publish(keys.BatteryGridChargeLimit, nil)
} else {
settings.SetFloat(keys.BatteryGridChargeLimit, *val)
site.publish(keys.BatteryGridChargeLimit, *val)
}
}
}

View file

@ -1,8 +1,6 @@
package core
import (
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/core/keys"
"github.com/evcc-io/evcc/core/loadpoint"
@ -37,10 +35,25 @@ func (site *Site) SetBatteryMode(batMode api.BatteryMode) {
}
}
// applyBatteryMode applies the mode to each battery and updates
// internal state if successful (requires lock)
// requiredBatteryMode determines required battery mode based on grid charge and rate
func (site *Site) requiredBatteryMode(batteryGridChargeActive bool, rate api.Rate) api.BatteryMode {
var res api.BatteryMode
batMode := site.GetBatteryMode()
switch {
case batteryGridChargeActive && batMode != api.BatteryCharge:
res = api.BatteryCharge
case !batteryGridChargeActive && site.dischargeControlActive(rate) && batMode != api.BatteryHold:
res = api.BatteryHold
case batteryModeModified(batMode):
res = api.BatteryNormal
}
return res
}
// applyBatteryMode applies the mode to each battery
func (site *Site) applyBatteryMode(mode api.BatteryMode) error {
// update batteries
for _, meter := range site.batteryMeters {
if batCtrl, ok := meter.(api.BatteryController); ok {
if err := batCtrl.SetBatteryMode(mode); err != nil {
@ -49,9 +62,6 @@ func (site *Site) applyBatteryMode(mode api.BatteryMode) error {
}
}
// update state and publish
site.setBatteryMode(mode)
return nil
}
@ -64,62 +74,27 @@ func (site *Site) plannerRates() (api.Rates, error) {
return tariff.Rates()
}
func (site *Site) plannerRate() (*api.Rate, error) {
rates, err := site.plannerRates()
if rates == nil || err != nil {
return nil, err
}
rate, err := rates.Current(time.Now())
if err != nil {
return nil, err
}
return &rate, nil
}
func (site *Site) smartCostActive(lp loadpoint.API, rate *api.Rate) bool {
func (site *Site) smartCostActive(lp loadpoint.API, rate api.Rate) bool {
limit := lp.GetSmartCostLimit()
return limit != 0 && rate != nil && rate.Price <= limit
return limit != nil && !rate.IsEmpty() && rate.Price <= *limit
}
func (site *Site) smartCostNextStart(lp loadpoint.API, rate api.Rates) time.Time {
limit := lp.GetSmartCostLimit()
if limit == 0 || rate == nil {
return time.Time{}
}
now := time.Now()
for _, slot := range rate {
if slot.Start.After(now) && slot.Price <= limit {
return slot.Start
}
}
return time.Time{}
func (site *Site) batteryGridChargeActive(rate api.Rate) bool {
limit := site.GetBatteryGridChargeLimit()
return limit != nil && !rate.IsEmpty() && rate.Price <= *limit
}
func (site *Site) updateBatteryMode() {
mode := api.BatteryNormal
rate, err := site.plannerRate()
if err != nil {
site.log.WARN.Println("smart cost:", err)
func (site *Site) dischargeControlActive(rate api.Rate) bool {
if !site.GetBatteryDischargeControl() {
return false
}
for _, lp := range site.Loadpoints() {
smartCostActive := site.smartCostActive(lp, rate)
if lp.GetStatus() == api.StatusC && (smartCostActive || lp.IsFastChargingActive()) {
mode = api.BatteryHold
break
return true
}
}
if batMode := site.GetBatteryMode(); mode != batMode {
site.Lock()
if err := site.applyBatteryMode(mode); err != nil {
site.log.ERROR.Println("battery mode:", err)
}
site.Unlock()
}
return false
}

View file

@ -1,36 +0,0 @@
package core
import (
"testing"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)
func TestUpdateBatteryMode(t *testing.T) {
expBatMode := api.BatteryHold
ctrl := gomock.NewController(t)
batCtrl := struct {
*api.MockBatteryController
*api.MockMeter
}{
api.NewMockBatteryController(ctrl),
api.NewMockMeter(ctrl),
}
batCtrl.MockBatteryController.EXPECT().SetBatteryMode(expBatMode).Times(1)
s := &Site{
log: util.NewLogger("foo"),
batteryMeters: []api.Meter{batCtrl},
batteryMode: api.BatteryNormal,
}
err := s.applyBatteryMode(expBatMode)
require.NoError(t, err)
assert.Equal(t, expBatMode, s.GetBatteryMode())
}