Add Volvo (Connected Car) (#20505)

This commit is contained in:
lehmanju 2025-04-12 11:58:37 +02:00 • committed by GitHub
parent 7e9bccf0b8
commit 960a71c97c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 213 additions and 115 deletions

View file

@ -71,6 +71,8 @@ func runToken(cmd *cobra.Command, args []string) {
token, err = tronityToken(conf, vehicleConf)
case "citroen", "ds", "opel", "peugeot":
token, err = psaToken(typ)
case "volvo-connected":
token, err = volvoToken(vehicleConf)
default:
log.FATAL.Fatalf("vehicle type '%s' does not support token authentication", vehicleConf.Type)

63
cmd/token_volvo.go Normal file
View file

@ -0,0 +1,63 @@
package cmd
import (
"context"
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/config"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/vehicle"
"github.com/evcc-io/evcc/vehicle/volvo/connected"
"github.com/samber/lo"
"golang.org/x/oauth2"
)
func volvoToken(conf config.Named) (*oauth2.Token, error) {
var cc struct {
Credentials vehicle.ClientCredentials
}
if err := util.DecodeOther(conf.Other, &cc); err != nil {
return nil, err
}
if cc.Credentials.ID == "" {
if err := survey.AskOne(&survey.Input{
Message: "Please enter your client id:",
}, &cc.Credentials.ID, survey.WithValidator(survey.Required)); err != nil {
return nil, err
}
}
if cc.Credentials.Secret == "" {
if err := survey.AskOne(&survey.Input{
Message: "Please enter your client secret:",
}, &cc.Credentials.Secret, survey.WithValidator(survey.Required)); err != nil {
return nil, err
}
}
oc := connected.Oauth2Config(cc.Credentials.ID, cc.Credentials.Secret)
cv := oauth2.GenerateVerifier()
state := lo.RandomString(16, lo.AlphanumericCharset)
authorize_url := oc.AuthCodeURL(state, oauth2.S256ChallengeOption(cv))
fmt.Println("Please visit: ", authorize_url)
fmt.Println("And grab the authorization code like described here: https://github.com/flobz/psa_car_controller/discussions/779")
var code string
prompt_code := &survey.Input{
Message: "Please enter your authorization code:",
}
if err := survey.AskOne(prompt_code, &code, survey.WithValidator(survey.Required)); err != nil {
return nil, err
}
client := request.NewClient(util.NewLogger("volvo-connected"))
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, client)
return oc.Exchange(ctx, code, oauth2.VerifierOption(cv))
}