From 9125e113a3a93f0a6a0093df731efea41f502816 Mon Sep 17 00:00:00 2001 From: andig Date: Wed, 13 Aug 2025 22:58:29 +0200 Subject: [PATCH] chore: fix invalid error returns (#22972) --- charger/abb.go | 6 +++--- charger/abl.go | 2 +- charger/alfen.go | 5 ++++- charger/easee.go | 6 +++--- charger/em2go.go | 2 +- charger/eprowallbox.go | 2 +- charger/evsewifi.go | 4 ++-- charger/hardybarth-ecb1.go | 6 ++++-- charger/kathrein.go | 2 +- charger/keba-modbus.go | 2 +- charger/kse.go | 2 +- charger/mennekes-compact.go | 2 +- charger/peblar.go | 2 +- charger/phoenix-charx.go | 2 +- charger/phoenix-em-eth.go | 2 +- charger/pulsares.go | 2 +- charger/smaevcharger.go | 8 ++++++-- charger/sungrow.go | 6 +++--- charger/vestel.go | 2 +- charger/victron.go | 2 +- charger/warp2.go | 2 +- meter/config/config.go | 4 ++-- meter/goodwe/server.go | 2 +- meter/homematic.go | 5 ++++- meter/homewizard/connection.go | 2 +- meter/tapo/connection.go | 2 +- meter/zendure.go | 2 +- tariff/amber.go | 6 +----- tariff/awattar.go | 6 +----- tariff/edf-tempo.go | 6 +----- tariff/electricitymaps.go | 6 +----- tariff/elering.go | 6 +----- tariff/energinet.go | 6 +----- tariff/entsoe.go | 6 +----- tariff/groupe-e.go | 6 +----- tariff/gruenstromindex.go | 6 +----- tariff/helper.go | 20 ++++++++++++++++++++ tariff/ngeso.go | 6 +----- tariff/octopus.go | 6 +----- tariff/ostrom.go | 7 +++++-- tariff/pun.go | 6 +----- tariff/smartenergy.go | 6 +----- tariff/solcast.go | 7 +++++-- tariff/tariff.go | 7 +++++-- tariff/tibber.go | 6 +----- 45 files changed, 98 insertions(+), 113 deletions(-) diff --git a/charger/abb.go b/charger/abb.go index 456815389..3df59438c 100644 --- a/charger/abb.go +++ b/charger/abb.go @@ -97,7 +97,7 @@ func NewABB(ctx context.Context, uri, device, comset string, baudrate int, proto } }() - return wb, err + return wb, nil } func (wb *ABB) status() (byte, error) { @@ -202,7 +202,7 @@ func (wb *ABB) CurrentPower() (float64, error) { return 0, err } - return float64(binary.BigEndian.Uint32(b)), err + return float64(binary.BigEndian.Uint32(b)), nil } var _ api.ChargeRater = (*ABB)(nil) @@ -214,7 +214,7 @@ func (wb *ABB) ChargedEnergy() (float64, error) { return 0, err } - return float64(binary.BigEndian.Uint32(b)) / 1e3, err + return float64(binary.BigEndian.Uint32(b)) / 1e3, nil } // getPhaseValues returns 3 sequential register values diff --git a/charger/abl.go b/charger/abl.go index de8020b6b..f91944e6f 100644 --- a/charger/abl.go +++ b/charger/abl.go @@ -129,7 +129,7 @@ func NewABLeMH(ctx context.Context, uri, device, comset string, baudrate int, sl return decorateABLeMH(wb, wb.currentPower, wb.currents), nil } - return wb, err + return wb, nil } func (wb *ABLeMH) set(reg, val uint16) error { diff --git a/charger/alfen.go b/charger/alfen.go index d0f2c289e..55dd81467 100644 --- a/charger/alfen.go +++ b/charger/alfen.go @@ -93,6 +93,9 @@ func NewAlfen(ctx context.Context, uri string, slaveID uint8) (api.Charger, erro go wb.heartbeat(ctx) _, v2, v3, err := wb.Voltages() + if err != nil { + return nil, err + } var ( phasesS func(int) error @@ -106,7 +109,7 @@ func NewAlfen(ctx context.Context, uri string, slaveID uint8) (api.Charger, erro wb.log.DEBUG.Println("detected 1p alfen") } - return decorateAlfen(wb, phasesS, phasesG), err + return decorateAlfen(wb, phasesS, phasesG), nil } func (wb *Alfen) heartbeat(ctx context.Context) { diff --git a/charger/easee.go b/charger/easee.go index eea68d298..1e0d77a7d 100644 --- a/charger/easee.go +++ b/charger/easee.go @@ -122,7 +122,7 @@ func NewEasee(ctx context.Context, user, password, charger string, timeout time. ts, err := easee.TokenSource(log, user, password) if err != nil { - return c, err + return nil, err } // replace client transport with authenticated transport @@ -135,11 +135,11 @@ func NewEasee(ctx context.Context, user, password, charger string, timeout time. if charger == "" { chargers, err := c.chargers() if err != nil { - return c, err + return nil, err } if len(chargers) != 1 { - return c, fmt.Errorf("cannot determine charger id, found: %v", lo.Map(chargers, func(c easee.Charger, _ int) string { return c.ID })) + return nil, fmt.Errorf("cannot determine charger id, found: %v", lo.Map(chargers, func(c easee.Charger, _ int) string { return c.ID })) } c.charger = chargers[0].ID diff --git a/charger/em2go.go b/charger/em2go.go index 1e8253873..ded06615e 100644 --- a/charger/em2go.go +++ b/charger/em2go.go @@ -148,7 +148,7 @@ func (wb *Em2Go) initialize() (api.Charger, error) { phasesG = wb.getPhases } - return decorateEm2Go(wb, maxCurrent, phases1p3p, phasesG), err + return decorateEm2Go(wb, maxCurrent, phases1p3p, phasesG), nil } // Status implements the api.Charger interface diff --git a/charger/eprowallbox.go b/charger/eprowallbox.go index e76a5c61b..611f5a15d 100644 --- a/charger/eprowallbox.go +++ b/charger/eprowallbox.go @@ -80,7 +80,7 @@ func NewEProWallbox(ctx context.Context, uri, device, comset string, baudrate in go wb.heartbeat(ctx) - return wb, err + return wb, nil } func (wb *EProWallbox) heartbeat(ctx context.Context) { diff --git a/charger/evsewifi.go b/charger/evsewifi.go index ec4ac6f64..a04d40d9c 100644 --- a/charger/evsewifi.go +++ b/charger/evsewifi.go @@ -47,13 +47,13 @@ func NewEVSEWifiFromConfig(other map[string]interface{}) (api.Charger, error) { wb, err := NewEVSEWifi(util.DefaultScheme(cc.URI, "http"), cc.Cache) if err != nil { - return wb, err + return nil, err } // auto-detect capabilities params, err := wb.paramG.Get() if err != nil { - return wb, err + return nil, err } if !params.AlwaysActive { diff --git a/charger/hardybarth-ecb1.go b/charger/hardybarth-ecb1.go index fc62ccccd..68a53503b 100644 --- a/charger/hardybarth-ecb1.go +++ b/charger/hardybarth-ecb1.go @@ -103,9 +103,11 @@ func NewHardyBarth(uri string, chargecontrol, meter int, cache time.Duration) (a uri = fmt.Sprintf("%s/chargecontrols/%d/mode", wb.uri, wb.chargecontrol) data := url.Values{"mode": {echarge.ModeManual}} - err := wb.post(uri, data) + if err := wb.post(uri, data); err != nil { + return nil, err + } - return wb, err + return wb, nil } func (wb *HardyBarth) getChargeControl() (ecb1.ChargeControl, error) { diff --git a/charger/kathrein.go b/charger/kathrein.go index e3b17be10..a6190f949 100644 --- a/charger/kathrein.go +++ b/charger/kathrein.go @@ -193,7 +193,7 @@ func NewKathrein(ctx context.Context, uri string, id uint8) (*Kathrein, error) { curr: 6000, } - return wb, err + return wb, nil } // getPhaseValues returns 3 sequential register values diff --git a/charger/keba-modbus.go b/charger/keba-modbus.go index 1d743e0d2..f500cf6d4 100644 --- a/charger/keba-modbus.go +++ b/charger/keba-modbus.go @@ -190,7 +190,7 @@ func NewKeba(ctx context.Context, embed embed, uri string, slaveID uint8) (*Keba energyFactor: 1e4, } - return wb, err + return wb, nil } func (wb *Keba) heartbeat(ctx context.Context, u uint32) { diff --git a/charger/kse.go b/charger/kse.go index afcd88852..320ccec77 100644 --- a/charger/kse.go +++ b/charger/kse.go @@ -112,7 +112,7 @@ func NewKSE(ctx context.Context, uri, device, comset string, baudrate int, slave identify = wb.identify } - return decorateKSE(wb, phases1p3p, getPhases, identify), err + return decorateKSE(wb, phases1p3p, getPhases, identify), nil } // Status implements the api.Charger interface diff --git a/charger/mennekes-compact.go b/charger/mennekes-compact.go index 3989d167d..6cc4eb039 100644 --- a/charger/mennekes-compact.go +++ b/charger/mennekes-compact.go @@ -121,7 +121,7 @@ func NewMennekesCompact(ctx context.Context, uri, device, comset string, baudrat // failsafe go wb.heartbeat(ctx, mennekesHeartbeatInterval) - return decorateMennekesCompact(wb, phasesS), err + return decorateMennekesCompact(wb, phasesS), nil } func (wb *MennekesCompact) heartbeat(ctx context.Context, timeout time.Duration) { diff --git a/charger/peblar.go b/charger/peblar.go index 4e21bf728..5c2fe3d61 100644 --- a/charger/peblar.go +++ b/charger/peblar.go @@ -122,7 +122,7 @@ func NewPeblar(ctx context.Context, uri string, id uint8) (api.Charger, error) { phasesG = wb.getPhases } - return decoratePeblar(wb, phasesS, phasesG), err + return decoratePeblar(wb, phasesS, phasesG), nil } // Status implements the api.Charger interface diff --git a/charger/phoenix-charx.go b/charger/phoenix-charx.go index 62a22d379..a7d1ceb9b 100644 --- a/charger/phoenix-charx.go +++ b/charger/phoenix-charx.go @@ -105,7 +105,7 @@ func NewPhoenixCharx(ctx context.Context, uri string, id uint8, connector uint16 return nil, fmt.Errorf("invalid connector: %d", connector) } - return wb, err + return wb, nil } func (wb *PhoenixCharx) controllers() (uint16, error) { diff --git a/charger/phoenix-em-eth.go b/charger/phoenix-em-eth.go index a6dea2cfd..5313f7359 100644 --- a/charger/phoenix-em-eth.go +++ b/charger/phoenix-em-eth.go @@ -66,7 +66,7 @@ func NewPhoenixEMEthFromConfig(ctx context.Context, other map[string]interface{} voltages = wb.voltages } - return decoratePhoenixEMEth(wb, currentPower, totalEnergy, currents, voltages), err + return decoratePhoenixEMEth(wb, currentPower, totalEnergy, currents, voltages), nil } // NewPhoenixEMEth creates a Phoenix charger diff --git a/charger/pulsares.go b/charger/pulsares.go index 7277ae518..c7fd7e623 100644 --- a/charger/pulsares.go +++ b/charger/pulsares.go @@ -124,7 +124,7 @@ func NewPulsares(ctx context.Context, uri, device, comset string, baudrate int, go wb.heartbeat(ctx, t/2) } - return wb, err + return wb, nil } func (wb *Pulsares) heartbeat(ctx context.Context, timeout time.Duration) { diff --git a/charger/smaevcharger.go b/charger/smaevcharger.go index 4f6d24b95..23d0ceac7 100644 --- a/charger/smaevcharger.go +++ b/charger/smaevcharger.go @@ -99,7 +99,7 @@ func NewSmaevcharger(uri, user, password string, cache time.Duration) (api.Charg ts, err := smaevcharger.TokenSource(log, wb.uri, user, password) if err != nil { - return wb, err + return nil, err } // replace client transport with authenticated transport @@ -132,7 +132,11 @@ func NewSmaevcharger(uri, user, password string, cache time.Duration) (api.Charg ) } - return wb, err + if err != nil { + return nil, err + } + + return wb, nil } // Status implements the api.Charger interface diff --git a/charger/sungrow.go b/charger/sungrow.go index be2d2399a..13ccbcf29 100644 --- a/charger/sungrow.go +++ b/charger/sungrow.go @@ -100,7 +100,7 @@ func NewSungrow(ctx context.Context, uri, device, comset string, baudrate int, p curr: 60, } - return wb, err + return wb, nil } // getPhaseValues returns 3 non-sequential register values @@ -212,7 +212,7 @@ func (wb *Sungrow) CurrentPower() (float64, error) { return 0, err } - return rs485.RTUUint32ToFloat64Swapped(b), err + return rs485.RTUUint32ToFloat64Swapped(b), nil } var _ api.PhaseCurrents = (*Sungrow)(nil) @@ -238,7 +238,7 @@ func (wb *Sungrow) TotalEnergy() (float64, error) { return 0, err } - return rs485.RTUUint32ToFloat64Swapped(b) / 1e3, err + return rs485.RTUUint32ToFloat64Swapped(b) / 1e3, nil } var _ api.PhaseSwitcher = (*Sungrow)(nil) diff --git a/charger/vestel.go b/charger/vestel.go index 34be1e1a9..958cf9590 100644 --- a/charger/vestel.go +++ b/charger/vestel.go @@ -150,7 +150,7 @@ func NewVestel(ctx context.Context, uri string, id uint8) (api.Charger, error) { } go wb.heartbeat(ctx, timeout) - return decorateVestel(wb, phasesS, phasesG, identify), err + return decorateVestel(wb, phasesS, phasesG, identify), nil } func (wb *Vestel) heartbeat(ctx context.Context, timeout time.Duration) { diff --git a/charger/victron.go b/charger/victron.go index a35a26686..76d3f198b 100644 --- a/charger/victron.go +++ b/charger/victron.go @@ -119,7 +119,7 @@ func NewVictron(ctx context.Context, uri string, slaveID uint8, regs victronRegs return nil, errors.New("charger must be in manual mode") } - return wb, err + return wb, nil } // Status implements the api.Charger interface diff --git a/charger/warp2.go b/charger/warp2.go index 091e1b332..754fd6b0c 100644 --- a/charger/warp2.go +++ b/charger/warp2.go @@ -85,7 +85,7 @@ func NewWarp2FromConfig(other map[string]interface{}) (api.Charger, error) { } } - return decorateWarp2(wb, currentPower, totalEnergy, currents, voltages, identity, phases, getPhases), err + return decorateWarp2(wb, currentPower, totalEnergy, currents, voltages, identity, phases, getPhases), nil } // NewWarp2 creates a new configurable charger diff --git a/meter/config/config.go b/meter/config/config.go index c7c93971a..1ba8895c8 100644 --- a/meter/config/config.go +++ b/meter/config/config.go @@ -20,8 +20,8 @@ func NewFromConfig(ctx context.Context, typ string, other map[string]interface{} v, err := factory(ctx, other) if err != nil { - err = fmt.Errorf("cannot create meter type '%s': %w", typ, err) + return nil, fmt.Errorf("cannot create meter type '%s': %w", typ, err) } - return v, err + return v, nil } diff --git a/meter/goodwe/server.go b/meter/goodwe/server.go index 681d3b45a..e23e58434 100644 --- a/meter/goodwe/server.go +++ b/meter/goodwe/server.go @@ -43,7 +43,7 @@ func Instance(log *util.Logger) (*Server, error) { go instance.listen() go instance.readData() - return instance, err + return instance, nil } func (m *Server) AddInverter(ip string, timeout time.Duration) *util.Monitor[Inverter] { diff --git a/meter/homematic.go b/meter/homematic.go index 36aec4996..3b8f2b3b5 100644 --- a/meter/homematic.go +++ b/meter/homematic.go @@ -43,13 +43,16 @@ func NewCCUFromConfig(other map[string]interface{}) (api.Meter, error) { // NewCCU creates a new connection with usage for meter func NewCCU(uri, deviceid, meterid, switchid, user, password, usage string, cache time.Duration) (*CCU, error) { conn, err := homematic.NewConnection(uri, deviceid, meterid, switchid, user, password, cache) + if err != nil { + return nil, err + } m := &CCU{ conn: conn, usage: usage, } - return m, err + return m, nil } // CurrentPower implements the api.Meter interface diff --git a/meter/homewizard/connection.go b/meter/homewizard/connection.go index 773cd6b34..fde273f0d 100644 --- a/meter/homewizard/connection.go +++ b/meter/homewizard/connection.go @@ -40,7 +40,7 @@ func NewConnection(uri string, usage string, cache time.Duration) (*Connection, // check and set API version + product type var res ApiResponse if err := c.GetJSON(c.uri, &res); err != nil { - return c, err + return nil, err } if res.ApiVersion != "v1" { return nil, errors.New("unsupported api version: " + res.ApiVersion) diff --git a/meter/tapo/connection.go b/meter/tapo/connection.go index 2431aeb38..7afb81176 100644 --- a/meter/tapo/connection.go +++ b/meter/tapo/connection.go @@ -55,7 +55,7 @@ func NewConnection(uri, user, password string) (*Connection, error) { } conn.log.DEBUG.Printf("%s %s connected (fw:%s,hw:%s,mac:%s)", res.Type, res.Model, res.FWVersion, res.HWVersion, res.MAC) - return conn, err + return conn, nil } // Enable implements the api.Charger interface diff --git a/meter/zendure.go b/meter/zendure.go index d424c80d7..655426b2e 100644 --- a/meter/zendure.go +++ b/meter/zendure.go @@ -43,7 +43,7 @@ func NewZendureFromConfig(other map[string]interface{}) (api.Meter, error) { conn: conn, } - return c, err + return c, nil } // CurrentPower implements the api.Meter interface diff --git a/tariff/amber.go b/tariff/amber.go index 65b5a19b9..2e2b71cf8 100644 --- a/tariff/amber.go +++ b/tariff/amber.go @@ -73,11 +73,7 @@ func NewAmberFromConfig(other map[string]interface{}) (api.Tariff, error) { }), } - done := make(chan error) - go t.run(done) - err := <-done - - return t, err + return runOrError(t) } func (t *Amber) run(done chan error) { diff --git a/tariff/awattar.go b/tariff/awattar.go index 08bc1c5f2..036211928 100644 --- a/tariff/awattar.go +++ b/tariff/awattar.go @@ -50,11 +50,7 @@ func NewAwattarFromConfig(other map[string]interface{}) (api.Tariff, error) { data: util.NewMonitor[api.Rates](2 * time.Hour), } - done := make(chan error) - go t.run(done) - err := <-done - - return t, err + return runOrError(t) } func (t *Awattar) run(done chan error) { diff --git a/tariff/edf-tempo.go b/tariff/edf-tempo.go index d916c1a42..531b97bdc 100644 --- a/tariff/edf-tempo.go +++ b/tariff/edf-tempo.go @@ -82,11 +82,7 @@ func NewEdfTempoFromConfig(other map[string]interface{}) (api.Tariff, error) { Source: oauth.RefreshTokenSource(new(oauth2.Token), t), } - done := make(chan error) - go t.run(done) - err := <-done - - return t, err + return runOrError(t) } func (t *EdfTempo) RefreshToken(_ *oauth2.Token) (*oauth2.Token, error) { diff --git a/tariff/electricitymaps.go b/tariff/electricitymaps.go index c0a49152f..132aa40da 100644 --- a/tariff/electricitymaps.go +++ b/tariff/electricitymaps.go @@ -70,11 +70,7 @@ func NewElectricityMapsFromConfig(other map[string]interface{}) (api.Tariff, err }), } - done := make(chan error) - go t.run(done) - err := <-done - - return t, err + return runOrError(t) } func (t *ElectricityMaps) run(done chan error) { diff --git a/tariff/elering.go b/tariff/elering.go index b6e782f8e..cfa728856 100644 --- a/tariff/elering.go +++ b/tariff/elering.go @@ -54,11 +54,7 @@ func NewEleringFromConfig(other map[string]interface{}) (api.Tariff, error) { data: util.NewMonitor[api.Rates](2 * time.Hour), } - done := make(chan error) - go t.run(done) - err := <-done - - return t, err + return runOrError(t) } func (t *Elering) run(done chan error) { diff --git a/tariff/energinet.go b/tariff/energinet.go index 7921d56e9..bb6467e10 100644 --- a/tariff/energinet.go +++ b/tariff/energinet.go @@ -53,11 +53,7 @@ func NewEnerginetFromConfig(other map[string]interface{}) (api.Tariff, error) { data: util.NewMonitor[api.Rates](2 * time.Hour), } - done := make(chan error) - go t.run(done) - err := <-done - - return t, err + return runOrError(t) } func (t *Energinet) run(done chan error) { diff --git a/tariff/entsoe.go b/tariff/entsoe.go index fc6118978..99f16d055 100644 --- a/tariff/entsoe.go +++ b/tariff/entsoe.go @@ -79,11 +79,7 @@ func NewEntsoeFromConfig(other map[string]interface{}) (api.Tariff, error) { }), } - done := make(chan error) - go t.run(done) - err = <-done - - return t, err + return runOrError(t) } func (t *Entsoe) run(done chan error) { diff --git a/tariff/groupe-e.go b/tariff/groupe-e.go index 0e18a0933..b85cd4d64 100644 --- a/tariff/groupe-e.go +++ b/tariff/groupe-e.go @@ -30,11 +30,7 @@ func NewGroupeEFromConfig(other map[string]interface{}) (api.Tariff, error) { data: util.NewMonitor[api.Rates](2 * time.Hour), } - done := make(chan error) - go t.run(done) - err := <-done - - return t, err + return runOrError(t) } func (t *GroupeE) run(done chan error) { diff --git a/tariff/gruenstromindex.go b/tariff/gruenstromindex.go index 562968d92..0544387e3 100644 --- a/tariff/gruenstromindex.go +++ b/tariff/gruenstromindex.go @@ -52,11 +52,7 @@ func NewGrünStromIndexFromConfig(other map[string]interface{}) (api.Tariff, err Source: corrently.TokenSource(log, &oauth2.Token{AccessToken: cc.Token}), } - done := make(chan error) - go t.run(done) - err := <-done - - return t, err + return runOrError(t) } func (t *GrünStromIndex) run(done chan error) { diff --git a/tariff/helper.go b/tariff/helper.go index 289e58c73..1cf8902d2 100644 --- a/tariff/helper.go +++ b/tariff/helper.go @@ -71,3 +71,23 @@ func mergeRatesAfter(data *util.Monitor[api.Rates], new api.Rates, now time.Time func beginningOfDay() time.Time { return now.With(time.Now()).BeginningOfDay() } + +type runnable interface { + run(done chan error) +} + +// https://groups.google.com/g/golang-nuts/c/1cl9v_hPYHk +// runOrError invokes t.run(chan error) and waits for the channel to return +func runOrError[T any, I interface { + *T + runnable +}](t *T) (*T, error) { + done := make(chan error) + go I(t).run(done) + + if err := <-done; err != nil { + return nil, err + } + + return t, nil +} diff --git a/tariff/ngeso.go b/tariff/ngeso.go index 00942d747..b678f9e5c 100644 --- a/tariff/ngeso.go +++ b/tariff/ngeso.go @@ -47,11 +47,7 @@ func NewNgesoFromConfig(other map[string]interface{}) (api.Tariff, error) { data: util.NewMonitor[api.Rates](2 * time.Hour), } - done := make(chan error) - go t.run(done) - err := <-done - - return t, err + return runOrError(t) } func (t *Ngeso) run(done chan error) { diff --git a/tariff/octopus.go b/tariff/octopus.go index 1b3814ac6..f73450005 100644 --- a/tariff/octopus.go +++ b/tariff/octopus.go @@ -84,11 +84,7 @@ func NewOctopusFromConfig(other map[string]interface{}) (api.Tariff, error) { data: util.NewMonitor[api.Rates](2 * time.Hour), } - done := make(chan error) - go t.run(done) - err := <-done - - return t, err + return runOrError(t) } func (t *Octopus) run(done chan error) { diff --git a/tariff/ostrom.go b/tariff/ostrom.go index ab25a2346..343ee691c 100644 --- a/tariff/ostrom.go +++ b/tariff/ostrom.go @@ -110,9 +110,12 @@ func NewOstromFromConfig(other map[string]interface{}) (api.Tariff, error) { } else { go t.run(done) } - err = <-done - return t, err + if err := <-done; err != nil { + return nil, err + } + + return t, nil } func (t *Ostrom) getContracts() ([]ostrom.Contract, error) { diff --git a/tariff/pun.go b/tariff/pun.go index 48b5a916a..6b76c4c7a 100644 --- a/tariff/pun.go +++ b/tariff/pun.go @@ -65,11 +65,7 @@ func NewPunFromConfig(other map[string]interface{}) (api.Tariff, error) { data: util.NewMonitor[api.Rates](2 * time.Hour), } - done := make(chan error) - go t.run(done) - err := <-done - - return t, err + return runOrError(t) } func (t *Pun) run(done chan error) { diff --git a/tariff/smartenergy.go b/tariff/smartenergy.go index 18e9c6ea5..bfd493774 100644 --- a/tariff/smartenergy.go +++ b/tariff/smartenergy.go @@ -43,11 +43,7 @@ func NewSmartEnergyFromConfig(other map[string]interface{}) (api.Tariff, error) data: util.NewMonitor[api.Rates](2 * time.Hour), } - done := make(chan error) - go t.run(done) - err := <-done - - return t, err + return runOrError(t) } func (t *SmartEnergy) run(done chan error) { diff --git a/tariff/solcast.go b/tariff/solcast.go index 41e7ef8d4..20ff395ea 100644 --- a/tariff/solcast.go +++ b/tariff/solcast.go @@ -66,9 +66,12 @@ func NewSolcastFromConfig(other map[string]interface{}) (api.Tariff, error) { done := make(chan error) go t.run(cc.Interval, done) - err := <-done - return t, err + if err := <-done; err != nil { + return nil, err + } + + return t, nil } func (t *Solcast) run(interval time.Duration, done chan error) { diff --git a/tariff/tariff.go b/tariff/tariff.go index 1f00a20a6..8366ec729 100644 --- a/tariff/tariff.go +++ b/tariff/tariff.go @@ -78,10 +78,13 @@ func NewConfigurableFromConfig(ctx context.Context, other map[string]interface{} if forecastG != nil { done := make(chan error) go t.run(forecastG, done, cc.Interval) - err = <-done + + if err := <-done; err != nil { + return nil, err + } } - return t, err + return t, nil } func (t *Tariff) run(forecastG func() (string, error), done chan error, interval time.Duration) { diff --git a/tariff/tibber.go b/tariff/tibber.go index a9db1dd80..1d4a0940b 100644 --- a/tariff/tibber.go +++ b/tariff/tibber.go @@ -66,11 +66,7 @@ func NewTibberFromConfig(other map[string]interface{}) (api.Tariff, error) { t.homeID = home.ID } - done := make(chan error) - go t.run(done) - err := <-done - - return t, err + return runOrError(t) } func (t *Tibber) run(done chan error) {