Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chargeable battery charger #10814

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions charger/battery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package charger

import (
"fmt"

"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/config"
)

type ChargeableBattery interface {
api.Meter
api.Battery
api.BatteryController
}

// Battery implements an api.Charger for controllable batteries
type Battery struct {
*embed
ChargeableBattery
mode api.BatteryMode
}

func init() {
registry.Add("battery", NewBatteryFromConfig)
}

func NewBatteryFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := struct {
embed `mapstructure:",squash"`
Battery string
}{
embed: embed{
Icon_: "battery",
Features_: []api.Feature{api.IntegratedDevice},
},
}

if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}

dev, err := config.Meters().ByName(cc.Battery)
if err != nil {
return nil, err
}

battery, ok := dev.Instance().(ChargeableBattery)
if !ok {
return nil, fmt.Errorf("'%s' is not a chargeable battery", cc.Battery)
}

c := &Battery{
embed: &cc.embed,
ChargeableBattery: battery,
}

return c, nil
}

// Status calculates the battery charging status
func (c *Battery) Status() (api.ChargeStatus, error) {
res := api.StatusB

p, err := c.ChargeableBattery.CurrentPower()
if p > 0 && err == nil {
res = api.StatusC
}

return res, err
}

// Enabled implements the api.Charger interface
func (c *Battery) Enabled() (bool, error) {
return c.mode == api.BatteryCharge, nil
}

// Enable implements the api.Charger interface
func (c *Battery) Enable(enable bool) error {
mode := api.BatteryNormal
if enable {
mode = api.BatteryCharge
}

// TODO handle locking of battery
err := c.ChargeableBattery.SetBatteryMode(mode)
if err == nil {
c.mode = mode
}

return err
}

// MaxCurrent implements the api.Charger interface
func (c *Battery) MaxCurrent(current int64) error {
return nil
}

var _ api.Meter = (*Battery)(nil)

// CurrentPower implements the api.Meter interface
func (c *Battery) CurrentPower() (float64, error) {
if c.mode != api.BatteryCharge {
// disabled
return 0, nil
}
res, err := c.ChargeableBattery.CurrentPower()
return max(-res, 0), err
}
Loading