evcc-io/vehicle/toyota.go
2026-04-05 18:21:58 +02:00

65 lines
1.4 KiB
Go

package vehicle
import (
"fmt"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/vehicle/toyota"
)
// Toyota is an api.Vehicle implementation for Toyota/Lexus cars
type Toyota struct {
*embed
*toyota.Provider
}
func init() {
registry.Add("toyota", func(other map[string]any) (api.Vehicle, error) {
return newToyotaFromConfig("toyota", "T", other)
})
registry.Add("lexus", func(other map[string]any) (api.Vehicle, error) {
return newToyotaFromConfig("lexus", "L", other)
})
}
// newToyotaFromConfig creates a new vehicle
func newToyotaFromConfig(brand, brandCode string, other map[string]any) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
User, Password, VIN string
Cache time.Duration
}{
Cache: interval,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
if cc.User == "" || cc.Password == "" {
return nil, api.ErrMissingCredentials
}
v := &Toyota{
embed: &cc.embed,
}
log := util.NewLogger(brand).Redact(cc.User, cc.Password, cc.VIN)
identity := toyota.NewIdentity(log, brandCode)
err := identity.Login(cc.User, cc.Password)
if err != nil {
return v, fmt.Errorf("login failed: %w", err)
}
api := toyota.NewAPI(log, identity, brandCode)
cc.VIN, err = ensureVehicle(cc.VIN, api.Vehicles)
if err == nil {
v.Provider = toyota.NewProvider(log, api, cc.VIN, cc.Cache)
}
return v, err
}