From 22498e16378109336b9098301d188824deea7210 Mon Sep 17 00:00:00 2001 From: andig Date: Fri, 15 Mar 2024 13:38:24 +0100 Subject: [PATCH] Add SmartEnergy tariff (#12971) --- tariff/smartenergy.go | 94 ++++++++++++++++++++ tariff/smartenergy/types.go | 14 +++ templates/definition/tariff/smartenergy.yaml | 10 +++ 3 files changed, 118 insertions(+) create mode 100644 tariff/smartenergy.go create mode 100644 tariff/smartenergy/types.go create mode 100644 templates/definition/tariff/smartenergy.yaml diff --git a/tariff/smartenergy.go b/tariff/smartenergy.go new file mode 100644 index 000000000..20fbc65a6 --- /dev/null +++ b/tariff/smartenergy.go @@ -0,0 +1,94 @@ +package tariff + +import ( + "slices" + "sync" + "time" + + "github.com/cenkalti/backoff/v4" + "github.com/evcc-io/evcc/api" + "github.com/evcc-io/evcc/tariff/smartenergy" + "github.com/evcc-io/evcc/util" + "github.com/evcc-io/evcc/util/request" +) + +type SmartEnergy struct { + *embed + log *util.Logger + data *util.Monitor[api.Rates] +} + +var _ api.Tariff = (*SmartEnergy)(nil) + +func init() { + registry.Add("smartenergy", NewSmartEnergyFromConfig) +} + +func NewSmartEnergyFromConfig(other map[string]interface{}) (api.Tariff, error) { + var cc struct { + embed `mapstructure:",squash"` + } + + if err := util.DecodeOther(other, &cc); err != nil { + return nil, err + } + + t := &SmartEnergy{ + embed: &cc.embed, + log: util.NewLogger("smartenergy"), + data: util.NewMonitor[api.Rates](2 * time.Hour), + } + + done := make(chan error) + go t.run(done) + err := <-done + + return t, err +} + +func (t *SmartEnergy) run(done chan error) { + var once sync.Once + client := request.NewHelper(t.log) + bo := newBackoff() + + for ; true; <-time.Tick(time.Hour) { + var res smartenergy.Prices + + if err := backoff.Retry(func() error { + return client.GetJSON(smartenergy.URI, &res) + }, bo); err != nil { + once.Do(func() { done <- err }) + + t.log.ERROR.Println(err) + continue + } + + data := make(api.Rates, 0, len(res.Data)) + for _, r := range res.Data { + ar := api.Rate{ + Start: r.Date.Local(), + End: r.Date.Add(15 * time.Minute).Local(), + Price: t.totalPrice(r.Value / 100), + } + data = append(data, ar) + } + data.Sort() + + t.data.Set(data) + once.Do(func() { close(done) }) + } +} + +// Rates implements the api.Tariff interface +func (t *SmartEnergy) Rates() (api.Rates, error) { + var res api.Rates + err := t.data.GetFunc(func(val api.Rates) { + res = slices.Clone(val) + }) + return res, err +} + +// Type implements the api.Tariff interface +func (t *SmartEnergy) Type() api.TariffType { + return api.TariffTypePriceForecast +} diff --git a/tariff/smartenergy/types.go b/tariff/smartenergy/types.go new file mode 100644 index 000000000..f76790add --- /dev/null +++ b/tariff/smartenergy/types.go @@ -0,0 +1,14 @@ +package smartenergy + +import "time" + +const URI = "https://apis.smartenergy.at/market/v1/price" + +type Prices struct { + Data []Price +} + +type Price struct { + Date time.Time + Value float64 +} diff --git a/templates/definition/tariff/smartenergy.yaml b/templates/definition/tariff/smartenergy.yaml new file mode 100644 index 000000000..1d1083065 --- /dev/null +++ b/templates/definition/tariff/smartenergy.yaml @@ -0,0 +1,10 @@ +template: smartenergy +products: + - brand: SmartEnergy (AT) + description: + generic: smartCONTROL +params: + - preset: tariff-base +render: | + type: smartenergy + {{ include "tariff-base" . }}