From 6fb3dbb47fc72402fe380015366b7f50e1c35173 Mon Sep 17 00:00:00 2001 From: andig Date: Fri, 14 Feb 2025 17:30:39 +0100 Subject: [PATCH] chore: minor --- meter/tibber-pulse.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/meter/tibber-pulse.go b/meter/tibber-pulse.go index be6918d34..f395857ac 100644 --- a/meter/tibber-pulse.go +++ b/meter/tibber-pulse.go @@ -20,9 +20,8 @@ func init() { } type Tibber struct { - data *util.Monitor[tibber.LiveMeasurement] - homeID string - timeout time.Duration + data *util.Monitor[tibber.LiveMeasurement] + homeID string } func NewTibberFromConfig(ctx context.Context, other map[string]interface{}) (api.Meter, error) { @@ -69,9 +68,8 @@ func NewTibberFromConfig(ctx context.Context, other map[string]interface{}) (api } t := &Tibber{ - data: util.NewMonitor[tibber.LiveMeasurement](cc.Timeout), - homeID: cc.HomeID, - timeout: cc.Timeout, + data: util.NewMonitor[tibber.LiveMeasurement](cc.Timeout), + homeID: cc.HomeID, } // subscription client @@ -103,7 +101,7 @@ func NewTibberFromConfig(ctx context.Context, other map[string]interface{}) (api return nil }) - if err := t.ensureSubscribed(client); err != nil { + if err := t.ensureSubscribed(client, cc.Timeout); err != nil { return nil, err } @@ -122,7 +120,7 @@ func NewTibberFromConfig(ctx context.Context, other map[string]interface{}) (api // This invalidates the subscription, and therefore we resubscribe when exiting Run() gracefully // upon server request. // https://github.com/evcc-io/evcc/issues/17925#issuecomment-2621458890 - err = t.ensureSubscribed(client) + err = t.ensureSubscribed(client, cc.Timeout) } if err != nil { log.ERROR.Println(err) @@ -139,7 +137,7 @@ func NewTibberFromConfig(ctx context.Context, other map[string]interface{}) (api return t, nil } -func (t *Tibber) ensureSubscribed(client *graphql.SubscriptionClient) error { +func (t *Tibber) ensureSubscribed(client *graphql.SubscriptionClient, timeout time.Duration) error { done := make(chan error, 1) go func(done chan error) { done <- t.subscribe(client) @@ -148,7 +146,7 @@ func (t *Tibber) ensureSubscribed(client *graphql.SubscriptionClient) error { select { case err := <-done: return err - case <-time.After(t.timeout): + case <-time.After(timeout): return api.ErrTimeout } }