Skip to content

Commit

Permalink
Tesla: always expose the controller interface (#12391)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Feb 22, 2024
1 parent 20a6e36 commit 5c5e7d9
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 53 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ require (
github.com/eclipse/paho.mqtt.golang v1.4.3
github.com/enbility/cemd v0.2.2
github.com/enbility/eebus-go v0.2.0
github.com/evcc-io/tesla-proxy-client v0.0.0-20240220130921-cdf51549e213
github.com/evcc-io/tesla-proxy-client v0.0.0-20240221194046-4168b3759701
github.com/fatih/structs v1.1.0
github.com/glebarez/sqlite v1.10.0
github.com/go-http-utils/etag v0.0.0-20161124023236-513ea8f21eb1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/evcc-io/modbus v0.0.0-20230705160100-23e1df2c01c3 h1:QBtLqy85cy9KLRWqKxX+42bN1rWti8iU7pL7Vwf2UbE=
github.com/evcc-io/modbus v0.0.0-20230705160100-23e1df2c01c3/go.mod h1:qVX2WhsI5xyAoM6I/MV1bXSKBPdLAjp7pCvieO/S0AY=
github.com/evcc-io/tesla-proxy-client v0.0.0-20240220130921-cdf51549e213 h1:iHnuiPRtVoHtHd82y50sDQmJbn07xPQ3fr48+qLlvl8=
github.com/evcc-io/tesla-proxy-client v0.0.0-20240220130921-cdf51549e213/go.mod h1:zWtAweBqXJTk3HSrPSecz3Q3a2hAUQ4vOE6paJfn03I=
github.com/evcc-io/tesla-proxy-client v0.0.0-20240221194046-4168b3759701 h1:3JplY3KS6KMDVDNAU+3+KWmSWmoHIU34qwuIpW6SiHk=
github.com/evcc-io/tesla-proxy-client v0.0.0-20240221194046-4168b3759701/go.mod h1:zWtAweBqXJTk3HSrPSecz3Q3a2hAUQ4vOE6paJfn03I=
github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
Expand Down
9 changes: 0 additions & 9 deletions templates/definition/vehicle/tesla.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ params:
- name: capacity
- name: phases
advanced: true
- name: control
type: bool
description:
de: Fahrzeugsteuerung aktivieren
en: Enable vehicle control
help:
de: Notwendig für Tesla Wall Connector. Der Virtual Key für evcc muss eingerichtet sein. Siehe https://tesla.evcc.io
en: Use with Tesla Wall Connector. The Virtual Key for evcc must be installed. See https://tesla.evcc.io
- preset: vehicle-identify
render: |
type: tesla
Expand All @@ -53,7 +45,6 @@ render: |
refresh: {{ .refreshToken }}
capacity: {{ .capacity }}
phases: {{ .phases }}
control: {{ .control }}
vin: {{ .vin }}
{{ include "vehicle-identify" . }}
features: ["coarsecurrent"]
58 changes: 18 additions & 40 deletions vehicle/tesla.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ import (
type Tesla struct {
*embed
*tesla.Provider
}

type teslaController struct {
*embed
*tesla.Provider
*tesla.Controller
}

Expand All @@ -45,7 +40,6 @@ func NewTeslaFromConfig(other map[string]interface{}) (api.Vehicle, error) {
embed `mapstructure:",squash"`
Tokens Tokens
VIN string
Control bool
Timeout time.Duration
Cache time.Duration
}{
Expand Down Expand Up @@ -100,46 +94,30 @@ func NewTeslaFromConfig(other map[string]interface{}) (api.Vehicle, error) {
return nil, err
}

// proxy client
pc := request.NewClient(log)
pc.Transport = &transport.Decorator{
Decorator: transport.DecorateHeaders(map[string]string{
"X-Auth-Token": sponsor.Token,
}),
Base: hc.Transport,
}

tcc, err := teslaclient.NewClient(context.Background(), teslaclient.WithClient(pc))
if err != nil {
return nil, err
}
tcc.SetBaseUrl(tesla.ProxyBaseUrl)

v := &Tesla{
embed: &cc.embed,
Provider: tesla.NewProvider(vehicle, cc.Cache),
embed: &cc.embed,
Provider: tesla.NewProvider(vehicle, cc.Cache),
Controller: tesla.NewController(vehicle.WithClient(tcc)),
}

if v.Title_ == "" {
v.Title_ = vehicle.DisplayName
}

if cc.Control {
if !sponsor.IsAuthorized() {
return nil, api.ErrSponsorRequired
}

// proxy client
pc := request.NewClient(log)
pc.Transport = &transport.Decorator{
Decorator: transport.DecorateHeaders(map[string]string{
"X-Auth-Token": sponsor.Token,
}),
Base: hc.Transport,
}

tc, err := teslaclient.NewClient(context.Background(), teslaclient.WithClient(pc))
if err != nil {
return nil, err
}
tc.SetBaseUrl(tesla.ProxyBaseUrl)

vehicle, err := tc.Vehicle(vehicle.Vin)
if err != nil {
return nil, err
}

return &teslaController{
embed: v.embed,
Provider: v.Provider,
Controller: tesla.NewController(vehicle),
}, nil
}

return v, nil
}
2 changes: 1 addition & 1 deletion vehicle/tesla/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func TestCommandResponse(t *testing.T) {
sponsor.Token = "token"
sponsor.Subject = "any"

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.Path)
Expand Down
13 changes: 13 additions & 0 deletions vehicle/tesla/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"slices"

"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util/sponsor"
"github.com/evcc-io/tesla-proxy-client"
)

Expand All @@ -26,13 +27,21 @@ var _ api.CurrentController = (*Controller)(nil)

// StartCharge implements the api.VehicleChargeController interface
func (v *Controller) MaxCurrent(current int64) error {
if !sponsor.IsAuthorized() {
return api.ErrSponsorRequired
}

return apiError(v.vehicle.SetChargingAmps(int(current)))
}

var _ api.VehicleChargeController = (*Controller)(nil)

// StartCharge implements the api.VehicleChargeController interface
func (v *Controller) StartCharge() error {
if !sponsor.IsAuthorized() {
return api.ErrSponsorRequired
}

err := apiError(v.vehicle.StartCharging())
if err != nil && slices.Contains([]string{"complete", "is_charging"}, err.Error()) {
return nil
Expand All @@ -42,6 +51,10 @@ func (v *Controller) StartCharge() error {

// StopCharge implements the api.VehicleChargeController interface
func (v *Controller) StopCharge() error {
if !sponsor.IsAuthorized() {
return api.ErrSponsorRequired
}

err := apiError(v.vehicle.StopCharging())

// ignore sleeping vehicle
Expand Down

0 comments on commit 5c5e7d9

Please sign in to comment.