Skip to content

Commit

Permalink
Smart EQ: use Mercedes api (#18044)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Jan 4, 2025
1 parent 41da419 commit eaff6de
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 79 deletions.
34 changes: 31 additions & 3 deletions templates/definition/vehicle/smart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,36 @@ products:
- brand: Smart
description:
generic: EQ
requirements:
description:
de: |
Benötigt `access` und `refresh` Tokens. Diese können über den Befehl `evcc token [name]` generiert werden.
en: |
Requires `access` and `refresh` tokens. These can be generated with command `evcc token [name]`.
params:
- preset: vehicle-base
- preset: vehicle-common
- name: user
required: true
- name: region
required: true
choice: [EMEA, APAC, NORAM]
default: EMEA
- name: accessToken
required: true
mask: true
- name: refreshToken
required: true
mask: true
- name: vin
example: V...
- name: cache
default: 15m
render: |
type: smart
{{ include "vehicle-base" . }}
type: smart-eq
vin: {{ .vin }}
user: {{ .user }}
region: {{ .region }}
tokens:
access: {{ .accessToken }}
refresh: {{ .refreshToken }}
{{ include "vehicle-common" . }}
35 changes: 24 additions & 11 deletions vehicle/mercedes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vehicle

import (
"errors"
"time"

"github.com/evcc-io/evcc/api"
Expand All @@ -15,11 +16,16 @@ type Mercedes struct {
}

func init() {
registry.Add("mercedes", NewMercedesFromConfig)
registry.Add("mercedes", func(other map[string]interface{}) (api.Vehicle, error) {
return newMercedesFromConfig("mercedes", other)
})
registry.Add("smart-eq", func(other map[string]interface{}) (api.Vehicle, error) {
return newMercedesFromConfig("smart-eq", other)
})
}

// NewMercedesFromConfig creates a new vehicle
func NewMercedesFromConfig(other map[string]interface{}) (api.Vehicle, error) {
// newMercedesFromConfig creates a new vehicle
func newMercedesFromConfig(brand string, other map[string]interface{}) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
Tokens Tokens
Expand All @@ -45,21 +51,28 @@ func NewMercedesFromConfig(other map[string]interface{}) (api.Vehicle, error) {
cc.User = cc.Account_
}

log := util.NewLogger("mercedes").Redact(cc.Tokens.Access, cc.Tokens.Refresh)
log := util.NewLogger(brand).Redact(cc.Tokens.Access, cc.Tokens.Refresh)
identity, err := mercedes.NewIdentity(log, token, cc.User, cc.Region)
if err != nil {
return nil, err
}

v := &Mercedes{
embed: &cc.embed,
}

api := mercedes.NewAPI(log, identity)

cc.VIN, err = ensureVehicle(cc.VIN, api.Vehicles)
if err == nil {
v.Provider = mercedes.NewProvider(api, cc.VIN, cc.Cache)
if brand == "smart-eq" {
if cc.VIN == "" {
return nil, errors.New("missing VIN")
}
} else {
cc.VIN, err = ensureVehicle(cc.VIN, api.Vehicles)
if err != nil {
return nil, err
}
}

v := &Mercedes{
embed: &cc.embed,
Provider: mercedes.NewProvider(api, cc.VIN, cc.Cache),
}

return v, err
Expand Down
65 changes: 0 additions & 65 deletions vehicle/smart.go

This file was deleted.

0 comments on commit eaff6de

Please sign in to comment.