Skip to content

Commit

Permalink
Powerwall: cache meter data
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Mar 2, 2023
1 parent d69077b commit 12991e1
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions meter/powerwall.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/foogod/go-powerwall"
Expand All @@ -17,6 +18,7 @@ import (
type PowerWall struct {
usage string
client *powerwall.Client
meterG func() (map[string]powerwall.MeterAggregatesData, error)
}

func init() {
Expand All @@ -28,8 +30,11 @@ func init() {

// NewPowerWallFromConfig creates a PowerWall Powerwall Meter from generic config
func NewPowerWallFromConfig(other map[string]interface{}) (api.Meter, error) {
var cc struct {
cc := struct {
URI, Usage, User, Password string
Cache time.Duration
}{
Cache: time.Second,
}

if err := util.DecodeOther(other, &cc); err != nil {
Expand All @@ -52,11 +57,11 @@ func NewPowerWallFromConfig(other map[string]interface{}) (api.Meter, error) {
cc.Usage = "solar"
}

return NewPowerWall(cc.URI, cc.Usage, cc.User, cc.Password)
return NewPowerWall(cc.URI, cc.Usage, cc.User, cc.Password, cc.Cache)
}

// NewPowerWall creates a Tesla PowerWall Meter
func NewPowerWall(uri, usage, user, password string) (api.Meter, error) {
func NewPowerWall(uri, usage, user, password string, cache time.Duration) (api.Meter, error) {
log := util.NewLogger("powerwall").Redact(user, password)

httpClient := &http.Client{
Expand All @@ -72,6 +77,7 @@ func NewPowerWall(uri, usage, user, password string) (api.Meter, error) {
m := &PowerWall{
client: client,
usage: strings.ToLower(usage),
meterG: provider.Cached(client.GetMetersAggregates, cache),
}

// decorate api.MeterEnergy
Expand Down Expand Up @@ -103,7 +109,7 @@ var _ api.Meter = (*PowerWall)(nil)

// CurrentPower implements the api.Meter interface
func (m *PowerWall) CurrentPower() (float64, error) {
res, err := m.client.GetMetersAggregates()
res, err := m.meterG()
if err != nil {
return 0, err
}
Expand All @@ -117,7 +123,7 @@ func (m *PowerWall) CurrentPower() (float64, error) {

// totalEnergy implements the api.MeterEnergy interface
func (m *PowerWall) totalEnergy() (float64, error) {
res, err := m.client.GetMetersAggregates()
res, err := m.meterG()
if err != nil {
return 0, err
}
Expand Down

0 comments on commit 12991e1

Please sign in to comment.