Skip to content

Commit

Permalink
chore(deps): adapt to github.com/clambin/solaredge/v2 (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
clambin authored Dec 20, 2024
1 parent 1f9aa18 commit 30bf298
Show file tree
Hide file tree
Showing 19 changed files with 193 additions and 260 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches:
- master
- connstring
- v2

permissions:
packages: write
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches-ignore:
- master
- connstring
- v2
pull_request_target:

jobs:
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/clambin/go-common/charmer v0.2.0
github.com/clambin/go-common/httputils v0.1.2
github.com/clambin/go-common/pubsub v0.2.1
github.com/clambin/solaredge v1.0.3
github.com/clambin/solaredge/v2 v2.0.0
github.com/clambin/tado/v2 v2.3.1
github.com/golang-migrate/migrate/v4 v4.18.1
github.com/jmoiron/sqlx v1.4.0
Expand Down Expand Up @@ -108,7 +108,7 @@ require (
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect
golang.org/x/image v0.21.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/oauth2 v0.24.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ github.com/clambin/go-common/httputils v0.1.2 h1:x06nP/M/SgYqtVtU5998kOmEQppCAmb
github.com/clambin/go-common/httputils v0.1.2/go.mod h1:Wf2E3PpcGVqDTOp+7eOCAePzvzCSk/1FHC4MQVKsRIo=
github.com/clambin/go-common/pubsub v0.2.1 h1:+L8/3JCG95NglPu4w59i3afvn1SbeBz27kbyTK3vc0M=
github.com/clambin/go-common/pubsub v0.2.1/go.mod h1:0/dx43STTMk+soDoTEG5f5QvfKhl60Bq/TpXztyBKvk=
github.com/clambin/go-common/testutils v0.4.1 h1:cEmGbP/o4sEUWTKZMJ0UjVFgqRzNEiBMEaTHw93r+n4=
github.com/clambin/go-common/testutils v0.4.1/go.mod h1:bV0j8D4zhNkleCeluFKLBeLQ0L/dqkxbaR/joLn8kzg=
github.com/clambin/solaredge v1.0.3 h1:rrN7PL35XXlb8Wt/h0b0sO807N1mRz2RWNlMcWTn8TI=
github.com/clambin/solaredge v1.0.3/go.mod h1:9gphI1A0gzuPo8FtY/r3772rS+fc6NwvqMzK+1o+Foc=
github.com/clambin/go-common/testutils v0.5.0 h1:kh/0kuBiFL2oeZJ3EgkRyQphqXQmQYqsS3z07a2R2AM=
github.com/clambin/go-common/testutils v0.5.0/go.mod h1:bV0j8D4zhNkleCeluFKLBeLQ0L/dqkxbaR/joLn8kzg=
github.com/clambin/solaredge/v2 v2.0.0 h1:9xaXtahkBc3qi0icaFA98/rE8t1XWGKOADAtmRaSqUM=
github.com/clambin/solaredge/v2 v2.0.0/go.mod h1:mxpZQkBTlhpM/f+BSHhuDM+IPVoqqlHhYxg0sevEbcQ=
github.com/clambin/tado/v2 v2.3.1 h1:dRvRPZDf1Ll6OC/BtboPGDhaO4zzkjoO9tqrLBCaoTk=
github.com/clambin/tado/v2 v2.3.1/go.mod h1:oBre3Dk7lffgFi4yflgiqsIqjuojXCZoKvpzqdrEC+Y=
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
Expand Down Expand Up @@ -286,8 +286,8 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/oauth2 v0.24.0 h1:KTBBxWqUa0ykRPLtV69rRto9TLXcqYkeswu48x/gvNE=
golang.org/x/oauth2 v0.24.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/clambin/go-common/charmer"
"github.com/clambin/go-common/httputils/metrics"
"github.com/clambin/go-common/httputils/roundtripper"
"github.com/clambin/solaredge"
"github.com/clambin/solaredge/v2"
"github.com/clambin/tado/v2"
"github.com/clambin/tado/v2/tools"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -91,7 +91,7 @@ func newSolarEdgeClient(subsystem string, r prometheus.Registerer, v *viper.Vipe
r.MustRegister(solarEdgeMetrics)

return solaredge.Client{
Token: v.GetString("solaredge.token"),
SiteKey: v.GetString("solaredge.token"),
HTTPClient: &http.Client{
Timeout: 5 * time.Second,
Transport: roundtripper.New(roundtripper.WithRequestMetrics(solarEdgeMetrics)),
Expand Down
7 changes: 3 additions & 4 deletions internal/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/clambin/go-common/httputils"
"github.com/clambin/solaredge-monitor/internal/exporter"
"github.com/clambin/solaredge-monitor/internal/publisher"
"github.com/clambin/solaredge-monitor/internal/publisher/solaredge"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
Expand All @@ -32,7 +31,7 @@ var (
cmd.Root().Version,
viper.GetViper(),
prometheus.DefaultRegisterer,
publisher.SolarEdgeUpdater{Client: solarEdgeClient},
publisher.SolarEdgeUpdater{SolarEdgeClient: &solarEdgeClient},
logger,
)
},
Expand All @@ -44,7 +43,7 @@ func runExport(
version string,
v *viper.Viper,
r prometheus.Registerer,
solarEdgeUpdater publisher.Updater[solaredge.Update],
solarEdgeUpdater publisher.Updater[publisher.SolarEdgeUpdate],
logger *slog.Logger,
) error {
logger.Info("starting solaredge exporter", "version", version)
Expand All @@ -53,7 +52,7 @@ func runExport(
exportMetrics := exporter.NewMetrics()
r.MustRegister(exportMetrics)

solarEdgePoller := publisher.Publisher[solaredge.Update]{
solarEdgePoller := publisher.Publisher[publisher.SolarEdgeUpdate]{
Updater: solarEdgeUpdater,
Interval: v.GetDuration("polling.interval"),
Logger: logger.With("publisher", "solaredge"),
Expand Down
27 changes: 13 additions & 14 deletions internal/cmd/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package cmd

import (
"context"
solaredge2 "github.com/clambin/solaredge"
"github.com/clambin/solaredge-monitor/internal/publisher"
"github.com/clambin/solaredge-monitor/internal/publisher/solaredge"
"github.com/clambin/solaredge/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/spf13/viper"
Expand All @@ -15,16 +14,16 @@ import (
)

func Test_runExport(t *testing.T) {
p := fakeUpdater{Update: solaredge.Update{{
p := fakeUpdater{SolarEdgeUpdate: publisher.SolarEdgeUpdate{{
ID: 1,
Name: "my home",
PowerOverview: solaredge2.PowerOverview{
LastUpdateTime: solaredge2.Time(time.Date(2024, time.December, 12, 12, 0, 0, 0, time.UTC)),
LifeTimeData: solaredge2.EnergyOverview{Energy: 1000},
LastYearData: solaredge2.EnergyOverview{Energy: 100},
LastMonthData: solaredge2.EnergyOverview{Energy: 10},
LastDayData: solaredge2.EnergyOverview{Energy: 1},
CurrentPower: solaredge2.CurrentPower{Power: 500},
PowerOverview: solaredge.PowerOverview{
LastUpdateTime: solaredge.Time(time.Date(2024, time.December, 12, 12, 0, 0, 0, time.UTC)),
LifeTimeData: solaredge.EnergyOverview{Energy: 1000},
LastYearData: solaredge.EnergyOverview{Energy: 100},
LastMonthData: solaredge.EnergyOverview{Energy: 10},
LastDayData: solaredge.EnergyOverview{Energy: 1},
CurrentPower: solaredge.CurrentPower{Power: 500},
},
}}}
v := getViperFromViper(viper.GetViper())
Expand Down Expand Up @@ -69,12 +68,12 @@ solaredge_year_energy{site="my home"} 100
assert.NoError(t, <-errCh)
}

var _ publisher.Updater[solaredge.Update] = fakeUpdater{}
var _ publisher.Updater[publisher.SolarEdgeUpdate] = fakeUpdater{}

type fakeUpdater struct {
solaredge.Update
publisher.SolarEdgeUpdate
}

func (f fakeUpdater) GetUpdate(_ context.Context) (solaredge.Update, error) {
return f.Update, nil
func (f fakeUpdater) GetUpdate(_ context.Context) (publisher.SolarEdgeUpdate, error) {
return f.SolarEdgeUpdate, nil
}
7 changes: 3 additions & 4 deletions internal/cmd/scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/clambin/go-common/httputils"
"github.com/clambin/solaredge-monitor/internal/exporter"
"github.com/clambin/solaredge-monitor/internal/publisher"
"github.com/clambin/solaredge-monitor/internal/publisher/solaredge"
"github.com/clambin/solaredge-monitor/internal/repository"
"github.com/clambin/solaredge-monitor/internal/scraper"
"github.com/clambin/tado/v2"
Expand Down Expand Up @@ -44,7 +43,7 @@ var (
cmd.Root().Version,
viper.GetViper(),
prometheus.DefaultRegisterer,
publisher.SolarEdgeUpdater{Client: solarEdgeClient},
publisher.SolarEdgeUpdater{SolarEdgeClient: &solarEdgeClient},
publisher.TadoUpdater{Client: tadoClient, HomeId: homeId},
logger,
)
Expand All @@ -57,7 +56,7 @@ func runScrape(
version string,
v *viper.Viper,
r prometheus.Registerer,
solarEdgeUpdater publisher.Updater[solaredge.Update],
solarEdgeUpdater publisher.Updater[publisher.SolarEdgeUpdate],
tadoUpdater publisher.Updater[*tado.Weather],
logger *slog.Logger,
) error {
Expand All @@ -71,7 +70,7 @@ func runScrape(

logger.Debug("connected to database")

solarEdgePoller := publisher.Publisher[solaredge.Update]{
solarEdgePoller := publisher.Publisher[publisher.SolarEdgeUpdate]{
Updater: solarEdgeUpdater,
Interval: v.GetDuration("polling.interval"),
Logger: logger.With("publisher", "solaredge"),
Expand Down
19 changes: 9 additions & 10 deletions internal/cmd/scrape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package cmd

import (
"context"
solaredge2 "github.com/clambin/solaredge"
"github.com/clambin/solaredge-monitor/internal/publisher"
"github.com/clambin/solaredge-monitor/internal/publisher/solaredge"
"github.com/clambin/solaredge-monitor/internal/repository"
"github.com/clambin/solaredge-monitor/internal/testutils"
"github.com/clambin/solaredge/v2"
"github.com/clambin/tado/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/viper"
Expand All @@ -29,16 +28,16 @@ func Test_runScrape(t *testing.T) {
v.Set("database.url", connString)
v.Set("polling.interval", time.Second)
v.Set("scrape.interval", 2*time.Second)
solarEdgeUpdater := fakeUpdater{Update: solaredge.Update{{
solarEdgeUpdater := fakeUpdater{SolarEdgeUpdate: publisher.SolarEdgeUpdate{{
ID: 1,
Name: "my home",
PowerOverview: solaredge2.PowerOverview{
LastUpdateTime: solaredge2.Time(time.Date(2024, time.December, 12, 12, 0, 0, 0, time.UTC)),
LifeTimeData: solaredge2.EnergyOverview{Energy: 1000},
LastYearData: solaredge2.EnergyOverview{Energy: 100},
LastMonthData: solaredge2.EnergyOverview{Energy: 10},
LastDayData: solaredge2.EnergyOverview{Energy: 1},
CurrentPower: solaredge2.CurrentPower{Power: 500},
PowerOverview: solaredge.PowerOverview{
LastUpdateTime: solaredge.Time(time.Date(2024, time.December, 12, 12, 0, 0, 0, time.UTC)),
LifeTimeData: solaredge.EnergyOverview{Energy: 1000},
LastYearData: solaredge.EnergyOverview{Energy: 100},
LastMonthData: solaredge.EnergyOverview{Energy: 10},
LastDayData: solaredge.EnergyOverview{Energy: 1},
CurrentPower: solaredge.CurrentPower{Power: 500},
},
}}}
tadoUpdater := publisher.TadoUpdater{Client: fakeTadoGetter{}}
Expand Down
6 changes: 3 additions & 3 deletions internal/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package exporter

import (
"context"
"github.com/clambin/solaredge-monitor/internal/publisher/solaredge"
"github.com/clambin/solaredge-monitor/internal/publisher"
"github.com/prometheus/client_golang/prometheus"
"log/slog"
)

type Exporter struct {
SolarEdge Publisher[solaredge.Update]
SolarEdge Publisher[publisher.SolarEdgeUpdate]
Metrics *Metrics
Logger *slog.Logger
}
Expand All @@ -35,7 +35,7 @@ func (e Exporter) Run(ctx context.Context) error {
}
}

func (e Exporter) export(update solaredge.Update) {
func (e Exporter) export(update publisher.SolarEdgeUpdate) {
e.Logger.Debug("exporting update")
for site := range update {
e.Metrics.currentPower.WithLabelValues(update[site].Name).Set(update[site].PowerOverview.CurrentPower.Power)
Expand Down
4 changes: 2 additions & 2 deletions internal/exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package exporter

import (
"context"
"github.com/clambin/solaredge-monitor/internal/publisher/solaredge"
"github.com/clambin/solaredge-monitor/internal/publisher"
"github.com/clambin/solaredge-monitor/internal/testutils"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/assert"
Expand All @@ -14,7 +14,7 @@ import (
)

func TestExporter(t *testing.T) {
p := testutils.FakePublisher[solaredge.Update]{Ch: make(chan solaredge.Update)}
p := testutils.FakePublisher[publisher.SolarEdgeUpdate]{Ch: make(chan publisher.SolarEdgeUpdate)}

metrics := NewMetrics()
exporter := Exporter{
Expand Down
49 changes: 34 additions & 15 deletions internal/publisher/publisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package publisher
import (
"context"
"github.com/clambin/go-common/pubsub"
"github.com/clambin/solaredge-monitor/internal/publisher/solaredge"
v2 "github.com/clambin/solaredge/v2"
"github.com/clambin/tado/v2"
"github.com/stretchr/testify/assert"
"io"
"log/slog"
"testing"
Expand All @@ -14,29 +15,42 @@ import (
var discardLogger = slog.New(slog.NewTextHandler(io.Discard, nil))

func TestPublisher_SolarEdge(t *testing.T) {
p := Publisher[solaredge.Update]{
Updater: fakeSolarEdgeClient{},
p := Publisher[SolarEdgeUpdate]{
Updater: SolarEdgeUpdater{SolarEdgeClient: fakeSolarEdgeClient{}},
Interval: 100 * time.Millisecond,
Logger: discardLogger,
Publisher: pubsub.Publisher[solaredge.Update]{},
Publisher: pubsub.Publisher[SolarEdgeUpdate]{},
}
ch := p.Subscribe()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
errCh := make(chan error)
go func() { errCh <- p.Run(ctx) }()

<-ch
<-ch
}

var _ Updater[solaredge.Update] = fakeSolarEdgeClient{}
assert.Equal(t, SolarEdgeUpdate{{
ID: 1,
Name: "my home",
PowerOverview: v2.PowerOverview{
LifeTimeData: v2.EnergyOverview{Energy: 1000},
LastYearData: v2.EnergyOverview{Energy: 100},
LastMonthData: v2.EnergyOverview{Energy: 10},
LastDayData: v2.EnergyOverview{Energy: 1},
CurrentPower: v2.CurrentPower{Power: 100},
},
InverterUpdates: []InverterUpdate{{
Name: "foo",
SerialNumber: "1234",
Telemetry: v2.InverterTelemetry{
L1Data: v2.InverterTelemetryL1Data{AcCurrent: 1, AcVoltage: 220},
DcVoltage: 380,
},
}},
}}, <-ch)

type fakeSolarEdgeClient struct{}
<-ch

func (f fakeSolarEdgeClient) GetUpdate(_ context.Context) (solaredge.Update, error) {
return solaredge.Update{}, nil
cancel()
assert.NoError(t, <-errCh)
}

func TestPublisher_Tado(t *testing.T) {
Expand All @@ -49,12 +63,17 @@ func TestPublisher_Tado(t *testing.T) {
ch := p.Subscribe()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
errCh := make(chan error)
go func() { errCh <- p.Run(ctx) }()

update := <-ch
assert.Equal(t, float32(75), *update.SolarIntensity.Percentage)
assert.Equal(t, float32(18), *update.OutsideTemperature.Celsius)
assert.Equal(t, tado.SUN, *update.WeatherState.Value)
<-ch
<-ch

cancel()
assert.NoError(t, <-errCh)
}

var _ Updater[*tado.Weather] = fakeTadoClient{}
Expand Down
Loading

0 comments on commit 30bf298

Please sign in to comment.