Skip to content

Commit

Permalink
Revert "Tibber: resubscribe on clean disconnect (#18643)"
Browse files Browse the repository at this point in the history
This reverts commit 4f97824.
  • Loading branch information
GrimmiMeloni committed Feb 21, 2025
1 parent a380eb4 commit 37fe9ed
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions meter/tibber-pulse.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ func init() {
}

type Tibber struct {
data *util.Monitor[tibber.LiveMeasurement]
homeID string
data *util.Monitor[tibber.LiveMeasurement]
}

func NewTibberFromConfig(ctx context.Context, other map[string]interface{}) (api.Meter, error) {
Expand Down Expand Up @@ -68,8 +67,7 @@ func NewTibberFromConfig(ctx context.Context, other map[string]interface{}) (api
}

t := &Tibber{
data: util.NewMonitor[tibber.LiveMeasurement](cc.Timeout),
homeID: cc.HomeID,
data: util.NewMonitor[tibber.LiveMeasurement](cc.Timeout),
}

// subscription client
Expand Down Expand Up @@ -101,8 +99,18 @@ func NewTibberFromConfig(ctx context.Context, other map[string]interface{}) (api
return nil
})

if err := t.ensureSubscribed(client, cc.Timeout); err != nil {
return nil, err
done := make(chan error, 1)
go func(done chan error) {
done <- t.subscribe(client, cc.HomeID)
}(done)

select {
case err := <-done:
if err != nil {
return nil, err
}
case <-time.After(cc.Timeout):
return nil, api.ErrTimeout
}

go func() {
Expand All @@ -113,6 +121,9 @@ func NewTibberFromConfig(ctx context.Context, other map[string]interface{}) (api
}()

go func() {
// The pulse sometimes declines valid(!) subscription requests, and asks the client to disconnect.
// Therefore we need to restart the client when exiting gracefully upon server request
// https://github.com/evcc-io/evcc/issues/17925#issuecomment-2621458890
for tick := time.Tick(10 * time.Second); ; {
if err := client.Run(); err != nil {
log.ERROR.Println(err)
Expand All @@ -129,27 +140,13 @@ func NewTibberFromConfig(ctx context.Context, other map[string]interface{}) (api
return t, nil
}

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)
}(done)

select {
case err := <-done:
return err
case <-time.After(timeout):
return api.ErrTimeout
}
}

func (t *Tibber) subscribe(client *graphql.SubscriptionClient) error {
func (t *Tibber) subscribe(client *graphql.SubscriptionClient, homeID string) error {
var query struct {
tibber.LiveMeasurement `graphql:"liveMeasurement(homeId: $homeId)"`
}

_, err := client.Subscribe(&query, map[string]any{
"homeId": graphql.ID(t.homeID),
"homeId": graphql.ID(homeID),
}, func(data []byte, err error) error {
if err != nil {
return err
Expand Down

0 comments on commit 37fe9ed

Please sign in to comment.