-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(vehicle): add support for LeapMotor API #29666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
41237d0
feat(vehicle): add support for leapmotor
syphernl 0cc838b
fmt
syphernl 06a68c5
refactor(leapmotor): simplify identity helpers
syphernl 0f76630
refactor(leapmotor): address review feedback
syphernl 3847d5a
go mod tidy: promote gmsm to direct dependency
syphernl 8ca5a81
Merge branch 'master' into feat/leapmotor_api_integration
syphernl 52dc5c0
refactor(leapmotor): address review feedback (round 2)
syphernl 71a5e8c
Merge branch 'master' into feat/leapmotor_api_integration
syphernl 9239a15
feat(leapmotor): add optional vehicle-side charge control
syphernl c7e9c56
Merge branch 'master' into feat/leapmotor_api_integration
syphernl 7a626c9
fix(leapmotor): persist refreshed token to settings in Refresh()
syphernl 7897de9
Merge branch 'master' into feat/leapmotor_api_integration
syphernl f13d020
Merge branch 'master' into feat/leapmotor_api_integration
syphernl 90c5605
Leapmotor: use C10 status endpoint for B10/B11
syphernl 59f7d9a
Leapmotor: decode signal-based C10/B10 status
syphernl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| template: leapmotor | ||
| products: | ||
| - brand: Leapmotor | ||
| requirements: | ||
| description: | ||
| en: | | ||
| App certificates are automatically downloaded from the community repository on first use. | ||
| de: | | ||
| App-Zertifikate werden beim ersten Start automatisch aus dem Community-Repository heruntergeladen. | ||
| params: | ||
| - preset: vehicle-base | ||
| render: | | ||
| type: leapmotor | ||
| {{ include "vehicle-base" . }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| package vehicle | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/evcc-io/evcc/api" | ||
| "github.com/evcc-io/evcc/server/db/settings" | ||
| "github.com/evcc-io/evcc/util" | ||
| "github.com/evcc-io/evcc/util/request" | ||
| "github.com/evcc-io/evcc/vehicle/leapmotor" | ||
| "golang.org/x/sync/errgroup" | ||
| ) | ||
|
|
||
| const ( | ||
| leapmotorCertURL = "https://raw.githubusercontent.com/markoceri/leapmotor-certs/main/app.crt" | ||
| leapmotorKeyURL = "https://raw.githubusercontent.com/markoceri/leapmotor-certs/main/app.key" | ||
| leapmotorDBKeyCert = "leapmotor.app.cert" | ||
| leapmotorDBKeyKey = "leapmotor.app.key" | ||
| ) | ||
|
|
||
| // Leapmotor is an api.Vehicle implementation for Leapmotor cars. | ||
| type Leapmotor struct { | ||
| *embed | ||
| *leapmotor.Provider | ||
| } | ||
|
|
||
| // LeapmotorWithControl extends Leapmotor with vehicle-side charge control (requires vehicle PIN). | ||
| type LeapmotorWithControl struct { | ||
| *Leapmotor | ||
| api *leapmotor.API | ||
| vin string | ||
| } | ||
|
|
||
| var _ api.ChargeController = (*LeapmotorWithControl)(nil) | ||
|
|
||
| func (l *LeapmotorWithControl) ChargeEnable(enable bool) error { | ||
| return l.api.ChargeToggle(l.vin, enable) | ||
| } | ||
|
|
||
| // SinglePhaseLeapmotor wraps Leapmotor to report 1-phase charging (e.g. T03). | ||
| type SinglePhaseLeapmotor struct { | ||
| *Leapmotor | ||
| } | ||
|
|
||
| // SinglePhaseLeapmotorWithControl wraps LeapmotorWithControl for single-phase vehicles. | ||
| type SinglePhaseLeapmotorWithControl struct { | ||
| *LeapmotorWithControl | ||
| } | ||
|
|
||
| func (o SinglePhaseLeapmotor) Phases() int { | ||
| return 1 | ||
| } | ||
|
|
||
| func (o SinglePhaseLeapmotorWithControl) Phases() int { | ||
| return 1 | ||
| } | ||
|
|
||
| func init() { | ||
| registry.Add("leapmotor", NewLeapmotorFromConfig) | ||
| } | ||
|
|
||
| func getAppCertFromDB() (certPEM, keyPEM []byte, err error) { | ||
| cert, err1 := settings.String(leapmotorDBKeyCert) | ||
| key, err2 := settings.String(leapmotorDBKeyKey) | ||
| if err1 != nil || err2 != nil { | ||
| return nil, nil, fmt.Errorf("not cached") | ||
| } | ||
| return []byte(cert), []byte(key), nil | ||
| } | ||
|
|
||
| func updateAppCertInDB(log *util.Logger) (certPEM, keyPEM []byte, err error) { | ||
| client := request.NewHelper(log) | ||
| eg := new(errgroup.Group) | ||
| eg.Go(func() error { | ||
| var e error | ||
| certPEM, e = client.GetBody(leapmotorCertURL) | ||
| return e | ||
| }) | ||
| eg.Go(func() error { | ||
| var e error | ||
| keyPEM, e = client.GetBody(leapmotorKeyURL) | ||
| return e | ||
| }) | ||
| if err = eg.Wait(); err != nil { | ||
| return nil, nil, err | ||
| } | ||
| settings.SetString(leapmotorDBKeyCert, string(certPEM)) | ||
| settings.SetString(leapmotorDBKeyKey, string(keyPEM)) | ||
| return certPEM, keyPEM, nil | ||
| } | ||
|
|
||
| // NewLeapmotorFromConfig creates a new Leapmotor vehicle from config. | ||
| func NewLeapmotorFromConfig(other map[string]any) (api.Vehicle, error) { | ||
| cc := struct { | ||
| embed `mapstructure:",squash"` | ||
| User, Password, VIN string | ||
| Pin 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 | ||
| } | ||
|
|
||
| log := util.NewLogger("leapmotor").Redact(cc.User, cc.Password, cc.VIN, cc.Pin) | ||
|
|
||
| certPEM, keyPEM, err := getAppCertFromDB() | ||
| if err != nil { | ||
| if certPEM, keyPEM, err = updateAppCertInDB(log); err != nil { | ||
| return nil, fmt.Errorf("leapmotor: fetch app certs: %w", err) | ||
| } | ||
| } | ||
|
|
||
| newIdentity := func(certPEM, keyPEM []byte) (*leapmotor.Identity, error) { | ||
| return leapmotor.NewIdentity(log, certPEM, keyPEM, cc.User, cc.Password, cc.Pin) | ||
| } | ||
|
|
||
| identity, err := newIdentity(certPEM, keyPEM) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if err := identity.TryRestore(); err != nil { | ||
| if loginErr := identity.Login(); loginErr != nil { | ||
| if strings.Contains(loginErr.Error(), "tls") { | ||
| // App cert may be stale — refresh and retry once | ||
| if certPEM, keyPEM, err = updateAppCertInDB(log); err != nil { | ||
| return nil, fmt.Errorf("leapmotor: refresh app certs: %w", err) | ||
| } | ||
| if identity, err = newIdentity(certPEM, keyPEM); err != nil { | ||
| return nil, err | ||
| } | ||
| if err = identity.Login(); err != nil { | ||
| return nil, err | ||
| } | ||
| } else { | ||
| return nil, loginErr | ||
| } | ||
| } | ||
| } | ||
|
|
||
| api := leapmotor.NewAPI(log, identity) | ||
|
|
||
| vehicles, err := api.Vehicles() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("leapmotor: get vehicles: %w", err) | ||
| } | ||
| if len(vehicles) == 0 { | ||
| return nil, fmt.Errorf("leapmotor: no vehicles found on account") | ||
| } | ||
|
|
||
| var matched *leapmotor.Vehicle | ||
| for i := range vehicles { | ||
| v := &vehicles[i] | ||
| if cc.VIN == "" || v.VIN == cc.VIN { | ||
| matched = v | ||
| break | ||
| } | ||
| } | ||
| if matched == nil { | ||
| return nil, fmt.Errorf("leapmotor: VIN %s not found on account", cc.VIN) | ||
| } | ||
|
|
||
| lm := &Leapmotor{ | ||
| embed: &cc.embed, | ||
| Provider: leapmotor.NewProvider(api, matched.VIN, matched.CarType, cc.Cache), | ||
| } | ||
|
|
||
| if cc.Pin != "" { | ||
| lmCtl := &LeapmotorWithControl{Leapmotor: lm, api: api, vin: matched.VIN} | ||
| if matched.CarType == "T03" { | ||
| return SinglePhaseLeapmotorWithControl{lmCtl}, nil | ||
| } | ||
| return lmCtl, nil | ||
| } | ||
|
|
||
| if matched.CarType == "T03" { | ||
| return SinglePhaseLeapmotor{lm}, nil | ||
| } | ||
| return lm, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.