Zendure: fix battery capacity usage (#23870)
This commit is contained in:
parent
31f4ae4fd9
commit
17d1ba757e
2 changed files with 70 additions and 3 deletions
|
|
@ -19,9 +19,12 @@ type Zendure struct {
|
|||
conn *zendure.Connection
|
||||
}
|
||||
|
||||
//go:generate go tool decorate -f decorateZendure -b *Zendure -r api.Meter -t "api.Battery,Soc,func() (float64, error)" -t "api.BatteryCapacity,Capacity,func() float64"
|
||||
|
||||
// NewZendureFromConfig creates a Zendure meter from generic config
|
||||
func NewZendureFromConfig(other map[string]interface{}) (api.Meter, error) {
|
||||
cc := struct {
|
||||
batteryCapacity `mapstructure:",squash"`
|
||||
Usage, Account, Serial, Region string
|
||||
Timeout time.Duration
|
||||
}{
|
||||
|
|
@ -43,7 +46,13 @@ func NewZendureFromConfig(other map[string]interface{}) (api.Meter, error) {
|
|||
conn: conn,
|
||||
}
|
||||
|
||||
return c, nil
|
||||
// decorate battery
|
||||
var soc func() (float64, error)
|
||||
if cc.Usage == "battery" {
|
||||
soc = c.soc
|
||||
}
|
||||
|
||||
return decorateZendure(c, soc, cc.batteryCapacity.Decorator()), nil
|
||||
}
|
||||
|
||||
// CurrentPower implements the api.Meter interface
|
||||
|
|
@ -63,8 +72,8 @@ func (c *Zendure) CurrentPower() (float64, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// Soc implements the api.Battery interface
|
||||
func (c *Zendure) Soc() (float64, error) {
|
||||
// soc implements the api.Battery interface
|
||||
func (c *Zendure) soc() (float64, error) {
|
||||
res, err := c.conn.Data()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
|
|
|
|||
58
meter/zendure_decorators.go
Normal file
58
meter/zendure_decorators.go
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
package meter
|
||||
|
||||
// Code generated by github.com/evcc-io/evcc/cmd/tools/decorate.go. DO NOT EDIT.
|
||||
|
||||
import (
|
||||
"github.com/evcc-io/evcc/api"
|
||||
)
|
||||
|
||||
func decorateZendure(base *Zendure, battery func() (float64, error), batteryCapacity func() float64) api.Meter {
|
||||
switch {
|
||||
case battery == nil:
|
||||
return base
|
||||
|
||||
case battery != nil && batteryCapacity == nil:
|
||||
return &struct {
|
||||
*Zendure
|
||||
api.Battery
|
||||
}{
|
||||
Zendure: base,
|
||||
Battery: &decorateZendureBatteryImpl{
|
||||
battery: battery,
|
||||
},
|
||||
}
|
||||
|
||||
case battery != nil && batteryCapacity != nil:
|
||||
return &struct {
|
||||
*Zendure
|
||||
api.Battery
|
||||
api.BatteryCapacity
|
||||
}{
|
||||
Zendure: base,
|
||||
Battery: &decorateZendureBatteryImpl{
|
||||
battery: battery,
|
||||
},
|
||||
BatteryCapacity: &decorateZendureBatteryCapacityImpl{
|
||||
batteryCapacity: batteryCapacity,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type decorateZendureBatteryImpl struct {
|
||||
battery func() (float64, error)
|
||||
}
|
||||
|
||||
func (impl *decorateZendureBatteryImpl) Soc() (float64, error) {
|
||||
return impl.battery()
|
||||
}
|
||||
|
||||
type decorateZendureBatteryCapacityImpl struct {
|
||||
batteryCapacity func() float64
|
||||
}
|
||||
|
||||
func (impl *decorateZendureBatteryCapacityImpl) Capacity() float64 {
|
||||
return impl.batteryCapacity()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue