70 lines
1.6 KiB
Go
70 lines
1.6 KiB
Go
package vehicle
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/evcc-io/evcc/api"
|
|
"github.com/evcc-io/evcc/util"
|
|
"github.com/evcc-io/evcc/util/oauth"
|
|
"github.com/evcc-io/evcc/vehicle/volvo/connected"
|
|
)
|
|
|
|
// VolvoConnected is an api.Vehicle implementation for Volvo Connected Car vehicles
|
|
type VolvoConnected struct {
|
|
*embed
|
|
*connected.Provider
|
|
}
|
|
|
|
func init() {
|
|
registry.AddCtx("volvo-connected", NewVolvoConnectedFromConfig)
|
|
}
|
|
|
|
// NewVolvoConnectedFromConfig creates a new VolvoConnected vehicle
|
|
func NewVolvoConnectedFromConfig(ctx context.Context, other map[string]any) (api.Vehicle, error) {
|
|
cc := struct {
|
|
embed `mapstructure:",squash"`
|
|
VIN string
|
|
VccApiKey string
|
|
Credentials oauth.ClientCredentials
|
|
RedirectUri string
|
|
Cache time.Duration
|
|
}{
|
|
Cache: interval,
|
|
}
|
|
|
|
if err := util.DecodeOther(other, &cc); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if cc.VccApiKey == "" {
|
|
return nil, errors.New("missing vccapikey")
|
|
}
|
|
|
|
if err := cc.Credentials.Error(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
log := util.NewLogger("volvo-connected").Redact(cc.VIN, cc.Credentials.ID, cc.Credentials.Secret, cc.VccApiKey)
|
|
|
|
oc := connected.OAuthConfig(cc.Credentials.ID, cc.Credentials.Secret, cc.RedirectUri)
|
|
ts, err := connected.NewOAuth(util.WithLogger(context.Background(), log), oc, cc.embed.GetTitle())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
api := connected.NewAPI(log, cc.VccApiKey, ts)
|
|
|
|
cc.VIN, err = ensureVehicle(cc.VIN, api.Vehicles)
|
|
|
|
v := &VolvoConnected{
|
|
embed: &cc.embed,
|
|
}
|
|
|
|
if err == nil {
|
|
v.Provider = connected.NewProvider(api, ts, cc.VIN, cc.Cache)
|
|
}
|
|
|
|
return v, err
|
|
}
|