Skip to content

Commit

Permalink
Tariffs: fix race condition with initial data retrieval (#11350)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Dec 30, 2023
1 parent 8758fe0 commit 4ca9472
Show file tree
Hide file tree
Showing 12 changed files with 15 additions and 25 deletions.
6 changes: 3 additions & 3 deletions core/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,12 +830,12 @@ func (site *Site) prepare() {
site.publish(keys.SmartCostType, nil)
site.publish(keys.SmartCostActive, false)
if tariff := site.GetTariff(PlannerTariff); tariff != nil {
site.publish(keys.SmartCostType, tariff.Type().String())
site.publish(keys.SmartCostType, tariff.Type())
}
site.publish(keys.Currency, site.tariffs.Currency.String())
site.publish(keys.Currency, site.tariffs.Currency)

site.publish(keys.BatteryDischargeControl, site.batteryDischargeControl)
site.publish(keys.BatteryMode, site.batteryMode.String())
site.publish(keys.BatteryMode, site.batteryMode)

if err := site.restoreSettings(); err != nil {
site.log.ERROR.Println(err)
Expand Down
2 changes: 1 addition & 1 deletion core/site_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (site *Site) GetTariff(tariff string) api.Tariff {
return site.tariffs.Planner

case site.tariffs.Grid != nil && site.tariffs.Grid.Type() == api.TariffTypePriceForecast:
// prio 1: dynamic grid tariff
// prio 1: grid tariff with forecast
return site.tariffs.Grid

case site.tariffs.Co2 != nil:
Expand Down
3 changes: 1 addition & 2 deletions tariff/awattar.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ func (t *Awattar) run(done chan error) {
continue
}

once.Do(func() { close(done) })

data := make(api.Rates, 0, len(res.Data))
for _, r := range res.Data {
ar := api.Rate{
Expand All @@ -84,6 +82,7 @@ func (t *Awattar) run(done chan error) {
data.Sort()

t.data.Set(data)
once.Do(func() { close(done) })
}
}

Expand Down
3 changes: 1 addition & 2 deletions tariff/edf-tempo.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ func (t *EdfTempo) run(done chan error) {
continue
}

once.Do(func() { close(done) })

data := make(api.Rates, 0, 24*len(res.Data.Values))
for _, r := range res.Data.Values {
for ts := r.StartDate.Local(); ts.Before(r.EndDate); ts = ts.Add(time.Hour) {
Expand All @@ -147,6 +145,7 @@ func (t *EdfTempo) run(done chan error) {
data.Sort()

t.data.Set(data)
once.Do(func() { close(done) })
}
}

Expand Down
3 changes: 1 addition & 2 deletions tariff/electricitymaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ func (t *ElectricityMaps) run(done chan error) {
continue
}

once.Do(func() { close(done) })

data := make(api.Rates, 0, len(res.Forecast))
for _, r := range res.Forecast {
ar := api.Rate{
Expand All @@ -112,6 +110,7 @@ func (t *ElectricityMaps) run(done chan error) {
data.Sort()

t.data.Set(data)
once.Do(func() { close(done) })
}
}

Expand Down
3 changes: 1 addition & 2 deletions tariff/elering.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ func (t *Elering) run(done chan error) {
continue
}

once.Do(func() { close(done) })

data := make(api.Rates, 0, len(res.Data[t.region]))
for _, r := range res.Data[t.region] {
ts := time.Unix(r.Timestamp, 0)
Expand All @@ -95,6 +93,7 @@ func (t *Elering) run(done chan error) {
data.Sort()

t.data.Set(data)
once.Do(func() { close(done) })
}
}

Expand Down
3 changes: 1 addition & 2 deletions tariff/energinet.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ func (t *Energinet) run(done chan error) {
continue
}

once.Do(func() { close(done) })

data := make(api.Rates, 0, len(res.Records))
for _, r := range res.Records {
date, _ := time.Parse("2006-01-02T15:04:05", r.HourUTC)
Expand All @@ -94,6 +92,7 @@ func (t *Energinet) run(done chan error) {
data.Sort()

t.data.Set(data)
once.Do(func() { close(done) })
}
}

Expand Down
5 changes: 2 additions & 3 deletions tariff/entsoe.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,6 @@ func (t *Entsoe) run(done chan error) {
continue
}

once.Do(func() { close(done) })

data := make(api.Rates, 0, len(res))
for _, r := range res {
ar := api.Rate{
Expand All @@ -168,6 +166,7 @@ func (t *Entsoe) run(done chan error) {
data.Sort()

t.data.Set(data)
once.Do(func() { close(done) })
}
}

Expand All @@ -182,5 +181,5 @@ func (t *Entsoe) Rates() (api.Rates, error) {

// Type implements the api.Tariff interface
func (t *Entsoe) Type() api.TariffType {
return api.TariffTypePriceDynamic
return api.TariffTypePriceForecast
}
3 changes: 1 addition & 2 deletions tariff/gruenstromindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ func (t *GrünStromIndex) run(done chan error) {
continue
}

once.Do(func() { close(done) })

data := make(api.Rates, 0, len(res.Forecast))
for _, r := range res.Forecast {
data = append(data, api.Rate{
Expand All @@ -127,6 +125,7 @@ func (t *GrünStromIndex) run(done chan error) {
data.Sort()

t.data.Set(data)
once.Do(func() { close(done) })
}
}

Expand Down
3 changes: 1 addition & 2 deletions tariff/ngeso.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ func (t *Ngeso) run(done chan error) {
continue
}

once.Do(func() { close(done) })

data := make(api.Rates, 0, len(carbonResponse.Results()))
for _, r := range carbonResponse.Results() {
ar := api.Rate{
Expand All @@ -109,6 +107,7 @@ func (t *Ngeso) run(done chan error) {
data.Sort()

t.data.Set(data)
once.Do(func() { close(done) })
}
}

Expand Down
3 changes: 1 addition & 2 deletions tariff/octopus.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ func (t *Octopus) run(done chan error) {
continue
}

once.Do(func() { close(done) })

data := make(api.Rates, 0, len(res.Results))
for _, r := range res.Results {
ar := api.Rate{
Expand All @@ -89,6 +87,7 @@ func (t *Octopus) run(done chan error) {
data.Sort()

t.data.Set(data)
once.Do(func() { close(done) })
}
}

Expand Down
3 changes: 1 addition & 2 deletions tariff/tibber.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,12 @@ func (t *Tibber) run(done chan error) {
continue
}

once.Do(func() { close(done) })

pi := res.Viewer.Home.CurrentSubscription.PriceInfo
data := append(t.rates(pi.Today), t.rates(pi.Tomorrow)...)
data.Sort()

t.data.Set(data)
once.Do(func() { close(done) })
}
}

Expand Down

0 comments on commit 4ca9472

Please sign in to comment.