Tibber: fix subscription with multiple home ids (#5755)
This commit is contained in:
parent
0a4d2dc262
commit
bdcf4325e4
3 changed files with 22 additions and 12 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue