Skip to content

Commit

Permalink
Powerwall: re-add request logging
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Feb 5, 2022
1 parent 1553455 commit cafd124
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 35 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,5 @@ require (
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)

replace github.com/foogod/go-powerwall => github.com/andig/go-powerwall v0.2.1-0.20220205120646-e5220ad9a9a0
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/alvaroloes/enumer v1.1.2 h1:5khqHB33TZy1GWCO/lZwcroBFh7u+0j40T83VUbfAMY=
github.com/alvaroloes/enumer v1.1.2/go.mod h1:FxrjvuXoDAx9isTJrv4c+T410zFi0DtXIT0m65DJ+Wo=
github.com/andig/go-powerwall v0.2.1-0.20220205120646-e5220ad9a9a0 h1:9fQ/afZNwqEnlAfozBiROWew2FjF2PctrEUVxUncM0Q=
github.com/andig/go-powerwall v0.2.1-0.20220205120646-e5220ad9a9a0/go.mod h1:NA12RXBKXFQFx3Nb9xMTQjHIphu1Fl64i/gQMuRdMZc=
github.com/andig/gosunspec v0.0.0-20211108155140-af2e73b86e71 h1:tnjVNZjuz+CK6fdc7ohJpMHjcEGFI5APp0l5T5Ocr/Y=
github.com/andig/gosunspec v0.0.0-20211108155140-af2e73b86e71/go.mod h1:c6P6szcR+ROkqZruOR4f6qbDKFjZX6OitPpj+yJ/r8k=
github.com/andybalholm/cascadia v1.2.0/go.mod h1:YCyR8vOZT9aZ1CHEd8ap0gMVm2aFgxBp0T0eFw1RUQY=
Expand Down Expand Up @@ -213,8 +215,6 @@ github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/felixge/httpsnoop v1.0.2 h1:+nS9g82KMXccJ/wp0zyRW9ZBHFETmMGtkk+2CTTrW4o=
github.com/felixge/httpsnoop v1.0.2/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/foogod/go-powerwall v0.2.0 h1:T/sSc/+3y9sUee7pLh9U2pCqidmX1lnENF8cGs9+PJc=
github.com/foogod/go-powerwall v0.2.0/go.mod h1:NA12RXBKXFQFx3Nb9xMTQjHIphu1Fl64i/gQMuRdMZc=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI=
Expand Down
47 changes: 29 additions & 18 deletions meter/tesla.go → meter/powerwall.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,31 @@ package meter
import (
"errors"
"fmt"
"net/http"
"strings"
"time"

"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/foogod/go-powerwall"
)

// Tesla is the tesla powerwall meter
type Tesla struct {
// PowerWall is the tesla powerwall meter
type PowerWall struct {
usage string
client *powerwall.Client
}

func init() {
registry.Add("tesla", NewTeslaFromConfig)
registry.Add("tesla", NewPowerWallFromConfig)
registry.Add("powerwall", NewPowerWallFromConfig)
}

//go:generate go run ../cmd/tools/decorate.go -f decorateTesla -b *Tesla -r api.Meter -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.Battery,SoC,func() (float64, error)"
//go:generate go run ../cmd/tools/decorate.go -f decoratePowerWall -b *PowerWall -r api.Meter -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.Battery,SoC,func() (float64, error)"

// NewTeslaFromConfig creates a Tesla Powerwall Meter from generic config
func NewTeslaFromConfig(other map[string]interface{}) (api.Meter, error) {
// NewPowerWallFromConfig creates a PowerWall Powerwall Meter from generic config
func NewPowerWallFromConfig(other map[string]interface{}) (api.Meter, error) {
cc := struct {
URI, Usage, User, Password string
}{}
Expand All @@ -48,17 +52,24 @@ func NewTeslaFromConfig(other map[string]interface{}) (api.Meter, error) {
cc.Usage = "solar"
}

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

// NewTesla creates a Tesla Meter
func NewTesla(uri, usage, user, password string) (api.Meter, error) {
client := powerwall.NewClient(uri, user, password)
// NewPowerWall creates a Tesla PowerWall Meter
func NewPowerWall(uri, usage, user, password string) (api.Meter, error) {
log := util.NewLogger("tesla")

httpClient := &http.Client{
Transport: request.NewTripper(log, powerwall.DefaultTransport()),
Timeout: time.Second * 2, // Timeout after 2 seconds
}

client := powerwall.NewClient(uri, user, password, powerwall.WithHttpClient(httpClient))
if _, err := client.GetStatus(); err != nil {
return nil, err
}

m := &Tesla{
m := &PowerWall{
client: client,
usage: strings.ToLower(usage),
}
Expand All @@ -75,33 +86,33 @@ func NewTesla(uri, usage, user, password string) (api.Meter, error) {
batterySoC = m.batterySoC
}

return decorateTesla(m, totalEnergy, batterySoC), nil
return decoratePowerWall(m, totalEnergy, batterySoC), nil
}

var _ api.Meter = (*Tesla)(nil)
var _ api.Meter = (*PowerWall)(nil)

// CurrentPower implements the api.Meter interface
func (m *Tesla) CurrentPower() (float64, error) {
func (m *PowerWall) CurrentPower() (float64, error) {
res, err := m.client.GetMetersAggregates()
if err != nil {
return 0, err
}

if o, ok := (*res)[m.usage]; ok {
if o, ok := res[m.usage]; ok {
return float64(o.InstantPower), nil
}

return 0, fmt.Errorf("invalid usage: %s", m.usage)
}

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

if o, ok := (*res)[m.usage]; ok {
if o, ok := res[m.usage]; ok {
switch m.usage {
case "load":
return float64(o.EnergyImported), nil
Expand All @@ -114,7 +125,7 @@ func (m *Tesla) totalEnergy() (float64, error) {
}

// batterySoC implements the api.Battery interface
func (m *Tesla) batterySoC() (float64, error) {
func (m *PowerWall) batterySoC() (float64, error) {
res, err := m.client.GetSOE()
if err != nil {
return 0, err
Expand Down
30 changes: 15 additions & 15 deletions meter/tesla_decorators.go → meter/powerwall_decorators.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cafd124

Please sign in to comment.