Skip to content

Commit

Permalink
Add waking up of ID vehicles (#559)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Jan 4, 2021
1 parent cc969c3 commit b5a98bd
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
5 changes: 5 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ type VehicleRange interface {
Range() (int64, error)
}

// VehicleStartCharge starts the charging session on the vehicle side
type VehicleStartCharge interface {
StartCharge() error
}

// Climater provides climatisation data
type Climater interface {
Climater() (active bool, outsideTemp float64, targetTemp float64, err error)
Expand Down
9 changes: 9 additions & 0 deletions core/loadpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,16 @@ func (lp *LoadPoint) setLimit(chargeCurrent float64, force bool) (err error) {
if err = lp.charger.Enable(enabled); err == nil {
lp.enabled = enabled
lp.guardUpdated = lp.clock.Now()

lp.bus.Publish(evChargeCurrent, chargeCurrent)
lp.log.DEBUG.Printf("charger %s", status[enabled])

// wake up vehicle
if car, ok := lp.vehicle.(api.VehicleStartCharge); enabled && ok {
if err := car.StartCharge(); err != nil {
lp.log.ERROR.Printf("vehicle remote charge start: %v", err)
}
}
} else {
lp.log.ERROR.Printf("charger %s: %v", status[enabled], err)
}
Expand Down
29 changes: 29 additions & 0 deletions vehicle/id/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ type API struct {
identity *vw.Identity
}

// Actions and action values
const (
ActionCharge = "charging"
ActionChargeStart = "start"
ActionChargeStop = "stop"
ActionChargeSettings = "settings" // body: targetSOC_pct

ActionClimatisation = "climatisation"
ActionClimatisationStart = "start"
ActionClimatisationStop = "stop"
)

// NewAPI creates a new vehicle
func NewAPI(log *util.Logger, identity *vw.Identity) *API {
v := &API{
Expand Down Expand Up @@ -148,6 +160,23 @@ func (v *API) Status(vin string) (res Status, err error) {
return res, err
}

// Action implements vehicle actions
func (v *API) Action(vin, action, value string) error {
uri := fmt.Sprintf("https://mobileapi.apps.emea.vwapps.io/vehicles/%s/%s/%s", vin, action, value)

req, err := request.New(http.MethodPost, uri, nil, map[string]string{
"Accept": "application/json",
"Authorization": "Bearer " + v.identity.Token(),
})

if err == nil {
var res interface{}
err = v.DoJSON(req, &res)
}

return err
}

// Any implements any api response
func (v *API) Any(uri, vin string) (interface{}, error) {
if strings.Contains(uri, "%s") {
Expand Down
11 changes: 10 additions & 1 deletion vehicle/id/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (

// Provider is an api.Vehicle implementation for VW ID cars
type Provider struct {
statusG func() (interface{}, error)
statusG func() (interface{}, error)
startChargeAction func() error
}

// NewProvider creates a new vehicle
Expand All @@ -19,6 +20,9 @@ func NewProvider(api *API, vin string, cache time.Duration) *Provider {
statusG: provider.NewCached(func() (interface{}, error) {
return api.Status(vin)
}, cache).InterfaceGetter(),
startChargeAction: func() error {
return api.Action(vin, ActionCharge, ActionChargeStart)
},
}
return impl
}
Expand Down Expand Up @@ -94,3 +98,8 @@ func (v *Provider) Climater() (active bool, outsideTemp float64, targetTemp floa

return active, outsideTemp, targetTemp, err
}

// StartCharge implements the VehicleStartCharge interface
func (v *Provider) StartCharge() error {
return v.startChargeAction()
}

0 comments on commit b5a98bd

Please sign in to comment.