Tibber: fix subscription with multiple home ids (#5755)
Some checks failed
Release / call-build-workflow (push) Has been cancelled
Release / Publish Docker :release (push) Has been cancelled
Release / Github & APT (push) Has been cancelled
Release / Hassio Addon (push) Has been cancelled

This commit is contained in:
andig 2023-01-11 21:26:22 +01:00 • committed by GitHub
parent 0a4d2dc262
commit bdcf4325e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 12 deletions

View file

@ -47,10 +47,11 @@ func NewTibberFromConfig(other map[string]interface{}) (api.Meter, error) {
// query client
qclient := tibber.NewClient(t.log, cc.Token)
var err error
if cc.HomeID, err = qclient.DefaultHomeID(); err != nil {
home, err := qclient.DefaultHome("")
if err != nil {
return nil, err
}
cc.HomeID = home.ID
}
// subscription client

View file

@ -7,6 +7,7 @@ import (
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/shurcooL/graphql"
"golang.org/x/exp/slices"
"golang.org/x/oauth2"
)
@ -30,7 +31,7 @@ func NewClient(log *util.Logger, token string) *Client {
}
}
func (c *Client) Home() (Home, error) {
func (c *Client) Homes() ([]Home, error) {
var res struct {
Viewer struct {
Homes []Home
@ -41,17 +42,25 @@ func (c *Client) Home() (Home, error) {
defer cancel()
if err := c.Query(ctx, &res, nil); err != nil {
return nil, err
}
return res.Viewer.Homes, nil
}
func (c *Client) DefaultHome(id string) (Home, error) {
homes, err := c.Homes()
if err != nil {
return Home{}, err
}
if len(res.Viewer.Homes) != 1 {
return Home{}, fmt.Errorf("could not determine home id: %v", res.Viewer.Homes)
idx := slices.IndexFunc(homes, func(h Home) bool {
return h.ID == id || (id == "" && len(homes) == 1)
})
if idx == -1 {
return Home{}, fmt.Errorf("could not determine home id: %v", homes)
}
return res.Viewer.Homes[0], nil
}
func (c *Client) DefaultHomeID() (string, error) {
home, err := c.Home()
return home.ID, err
return homes[idx], nil
}

View file

@ -55,7 +55,7 @@ func NewTibberFromConfig(other map[string]interface{}) (api.Tariff, error) {
}
if t.homeID == "" || t.unit == "" {
home, err := t.client.Home()
home, err := t.client.DefaultHome(t.homeID)
if err != nil {
return nil, err
}