Replace Skoda Connect with MySkoda (#14186)
This commit is contained in:
parent
f7229ed994
commit
d8875bf6c3
8 changed files with 223 additions and 82 deletions
|
|
@ -7,11 +7,10 @@ import (
|
|||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/request"
|
||||
"github.com/evcc-io/evcc/vehicle/skoda"
|
||||
"github.com/evcc-io/evcc/vehicle/skoda/connect"
|
||||
"github.com/evcc-io/evcc/vehicle/vag/service"
|
||||
"github.com/evcc-io/evcc/vehicle/skoda/service"
|
||||
)
|
||||
|
||||
// https://github.com/lendy007/skodaconnect
|
||||
// https://gitlab.com/prior99/skoda
|
||||
|
||||
// Enyaq is an api.Vehicle implementation for Skoda Enyaq cars
|
||||
type Enyaq struct {
|
||||
|
|
@ -66,17 +65,16 @@ func NewEnyaqFromConfig(other map[string]interface{}) (api.Vehicle, error) {
|
|||
},
|
||||
)
|
||||
|
||||
if err == nil {
|
||||
vehicle, err = api.VehicleDetails(vehicle.VIN)
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
v.fromVehicle(vehicle.Name, float64(vehicle.Specification.Battery.CapacityInKWh))
|
||||
}
|
||||
|
||||
// use Connect credentials to build provider
|
||||
// reuse tokenService to build provider
|
||||
if err == nil {
|
||||
ts, err := service.TokenRefreshServiceTokenSource(log, skoda.TRSParams, connect.AuthParams, cc.User, cc.Password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
api := skoda.NewAPI(log, ts)
|
||||
api.Client.Timeout = cc.Timeout
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ import (
|
|||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
const BaseURI = "https://api.connect.skoda-auto.cz/api"
|
||||
const BaseURI = "https://mysmob.api.connect.skoda-auto.cz/api"
|
||||
|
||||
// API is the Skoda api client
|
||||
type API struct {
|
||||
|
|
@ -30,28 +30,35 @@ func NewAPI(log *util.Logger, ts oauth2.TokenSource) *API {
|
|||
return v
|
||||
}
|
||||
|
||||
// Vehicles implements the /v3/garage response
|
||||
// Vehicles implements the /v2/garage response
|
||||
func (v *API) Vehicles() ([]Vehicle, error) {
|
||||
var res VehiclesResponse
|
||||
|
||||
uri := fmt.Sprintf("%s/v3/garage", BaseURI)
|
||||
uri := fmt.Sprintf("%s/v2/garage", BaseURI)
|
||||
err := v.GetJSON(uri, &res)
|
||||
|
||||
return res.Vehicles, err
|
||||
}
|
||||
|
||||
// Status implements the /v2/vehicle-status/<vin> response
|
||||
func (v *API) Status(vin string) (StatusResponse, error) {
|
||||
var res StatusResponse
|
||||
uri := fmt.Sprintf("%s/v2/vehicle-status/%s", BaseURI, vin)
|
||||
func (v *API) VehicleDetails(vin string) (Vehicle, error) {
|
||||
var res Vehicle
|
||||
uri := fmt.Sprintf("%s/v2/garage/vehicles/%s", BaseURI, vin)
|
||||
err := v.GetJSON(uri, &res)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Charger implements the /v1/charging/<vin>/status response
|
||||
// Status implements the /v2/vehicle-status/<vin> response
|
||||
func (v *API) Status(vin string) (StatusResponse, error) {
|
||||
var res StatusResponse
|
||||
uri := fmt.Sprintf("%s/v1/vehicle-health-report/warning-lights/%s", BaseURI, vin)
|
||||
err := v.GetJSON(uri, &res)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Charger implements the /v1/charging/<vin> response
|
||||
func (v *API) Charger(vin string) (ChargerResponse, error) {
|
||||
var res ChargerResponse
|
||||
uri := fmt.Sprintf("%s/v1/charging/%s/status", BaseURI, vin)
|
||||
uri := fmt.Sprintf("%s/v1/charging/%s", BaseURI, vin)
|
||||
err := v.GetJSON(uri, &res)
|
||||
return res, err
|
||||
}
|
||||
|
|
@ -59,33 +66,48 @@ func (v *API) Charger(vin string) (ChargerResponse, error) {
|
|||
// Settings implements the /v1/charging/<vin>/settings response
|
||||
func (v *API) Settings(vin string) (SettingsResponse, error) {
|
||||
var res SettingsResponse
|
||||
uri := fmt.Sprintf("%s/v1/charging/%s/settings", BaseURI, vin)
|
||||
|
||||
chrgRes, err := v.Charger(vin)
|
||||
if err == nil {
|
||||
res = chrgRes.Settings
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Climater implements the /v2/air-conditioning/<vin> response
|
||||
func (v *API) Climater(vin string) (ClimaterResponse, error) {
|
||||
var res ClimaterResponse
|
||||
uri := fmt.Sprintf("%s/v2/air-conditioning/%s", BaseURI, vin)
|
||||
err := v.GetJSON(uri, &res)
|
||||
return res, err
|
||||
}
|
||||
|
||||
const (
|
||||
ActionCharge = "charging"
|
||||
ActionChargeStart = "Start"
|
||||
ActionChargeStop = "Stop"
|
||||
ActionChargeStart = "start"
|
||||
ActionChargeStop = "stop"
|
||||
)
|
||||
|
||||
// Action executes a vehicle action
|
||||
func (v *API) Action(vin, action, value string) error {
|
||||
var res map[string]interface{}
|
||||
uri := fmt.Sprintf("%s/v1/%s/operation-requests?vin=%s", BaseURI, action, vin)
|
||||
// @POST("api/v1/charging/{vin}/start")
|
||||
// @POST("api/v1/charging/{vin}/stop")
|
||||
uri := fmt.Sprintf("%s/v1/%s/%s/%s", BaseURI, action, vin, value)
|
||||
|
||||
data := struct {
|
||||
Typ string `json:"type"`
|
||||
}{
|
||||
Typ: value,
|
||||
}
|
||||
|
||||
req, err := request.New(http.MethodPost, uri, request.MarshalJSON(data), request.JSONEncoding)
|
||||
req, err := request.New(http.MethodPost, uri, nil, request.JSONEncoding)
|
||||
if err == nil {
|
||||
// {"id":"61991908906fa40af9a5cba4","status":"InProgress","deeplink":""}
|
||||
err = v.DoJSON(req, &res)
|
||||
err = v.DoJSON(req, nil)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (v *API) WakeUp(vin string) error {
|
||||
// @POST("api/v1/vehicle-wakeup/{vin}")
|
||||
uri := fmt.Sprintf("%s/v1/vehicle-wakeup/%s", BaseURI, vin)
|
||||
|
||||
req, err := request.New(http.MethodPost, uri, nil, request.JSONEncoding)
|
||||
if err == nil {
|
||||
err = v.DoJSON(req, nil)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +0,0 @@
|
|||
package connect
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// Skoda connect api
|
||||
var AuthParams = url.Values{
|
||||
"response_type": {"code id_token"},
|
||||
"redirect_uri": {"skodaconnect://oidc.login/"},
|
||||
"client_id": {"7f045eee-7003-4379-9968-9355ed2adb06@apps_vw-dilab_com"},
|
||||
"scope": {"openid profile mbb mileage"}, // phone address cars email birthdate badge dealers driversLicense
|
||||
}
|
||||
|
|
@ -13,9 +13,9 @@ const (
|
|||
// Skoda native api
|
||||
var AuthParams = url.Values{
|
||||
"response_type": {"code id_token"},
|
||||
"client_id": {"f9a2359a-b776-46d9-bd0c-db1904343117@apps_vw-dilab_com"},
|
||||
"redirect_uri": {"skodaconnect://oidc.login/"},
|
||||
"scope": {"openid mbb profile"},
|
||||
"client_id": {"7f045eee-7003-4379-9968-9355ed2adb06@apps_vw-dilab_com"},
|
||||
"redirect_uri": {"myskoda://redirect/login/"},
|
||||
"scope": {"address badge birthdate cars driversLicense dealers email mileage mbb nationalIdentifier openid phone profession profile vin"},
|
||||
}
|
||||
|
||||
// TokenRefreshService parameters
|
||||
|
|
|
|||
|
|
@ -12,7 +12,9 @@ type Provider struct {
|
|||
statusG func() (StatusResponse, error)
|
||||
chargerG func() (ChargerResponse, error)
|
||||
settingsG func() (SettingsResponse, error)
|
||||
climateG func() (ClimaterResponse, error)
|
||||
action func(action, value string) error
|
||||
wakeup func() error
|
||||
}
|
||||
|
||||
// NewProvider creates a vehicle api provider
|
||||
|
|
@ -24,15 +26,18 @@ func NewProvider(api *API, vin string, cache time.Duration) *Provider {
|
|||
chargerG: provider.Cached(func() (ChargerResponse, error) {
|
||||
return api.Charger(vin)
|
||||
}, cache),
|
||||
// climateG: provider.Cached(func() (interface{}, error) {
|
||||
// return api.Climater(vin)
|
||||
// }, cache),
|
||||
climateG: provider.Cached(func() (ClimaterResponse, error) {
|
||||
return api.Climater(vin)
|
||||
}, cache),
|
||||
settingsG: provider.Cached(func() (SettingsResponse, error) {
|
||||
return api.Settings(vin)
|
||||
}, cache),
|
||||
action: func(action, value string) error {
|
||||
return api.Action(vin, action, value)
|
||||
},
|
||||
wakeup: func() error {
|
||||
return api.WakeUp(vin)
|
||||
},
|
||||
}
|
||||
return impl
|
||||
}
|
||||
|
|
@ -43,7 +48,7 @@ var _ api.Battery = (*Provider)(nil)
|
|||
func (v *Provider) Soc() (float64, error) {
|
||||
res, err := v.chargerG()
|
||||
if err == nil {
|
||||
return float64(res.Battery.StateOfChargeInPercent), nil
|
||||
return float64(res.Status.Battery.StateOfChargeInPercent), nil
|
||||
}
|
||||
|
||||
return 0, err
|
||||
|
|
@ -55,12 +60,16 @@ var _ api.ChargeState = (*Provider)(nil)
|
|||
func (v *Provider) Status() (api.ChargeStatus, error) {
|
||||
status := api.StatusA // disconnected
|
||||
|
||||
res, err := v.chargerG()
|
||||
res, err := v.climateG()
|
||||
if err == nil {
|
||||
if res.Plug.ConnectionState == "Connected" {
|
||||
if res.ChargerConnectionState == "CONNECTED" {
|
||||
status = api.StatusB
|
||||
}
|
||||
if res.Charging.State == "Charging" {
|
||||
}
|
||||
|
||||
resChrg, err := v.chargerG()
|
||||
if err == nil {
|
||||
if resChrg.Status.State == "CHARGING" {
|
||||
status = api.StatusC
|
||||
}
|
||||
}
|
||||
|
|
@ -74,14 +83,14 @@ var _ api.VehicleFinishTimer = (*Provider)(nil)
|
|||
func (v *Provider) FinishTime() (time.Time, error) {
|
||||
res, err := v.chargerG()
|
||||
if err == nil {
|
||||
crg := res.Charging
|
||||
crg := res.Status
|
||||
|
||||
// estimate not available
|
||||
if crg.State == "Error" || crg.ChargingType == "Invalid" {
|
||||
if crg.State == "Error" || crg.ChargeType == "Invalid" {
|
||||
return time.Time{}, api.ErrNotAvailable
|
||||
}
|
||||
|
||||
remaining := time.Duration(crg.RemainingToCompleteInSeconds) * time.Second
|
||||
remaining := time.Duration(crg.RemainingTimeToFullyChargedInMinutes) * time.Minute
|
||||
return time.Now().Add(remaining), err
|
||||
}
|
||||
|
||||
|
|
@ -93,7 +102,7 @@ var _ api.VehicleRange = (*Provider)(nil)
|
|||
// Range implements the api.VehicleRange interface
|
||||
func (v *Provider) Range() (rng int64, err error) {
|
||||
res, err := v.chargerG()
|
||||
return res.Battery.CruisingRangeElectricInMeters / 1e3, err
|
||||
return res.Status.Battery.RemainingCruisingRangeInMeters / 1e3, err
|
||||
}
|
||||
|
||||
var _ api.VehicleOdometer = (*Provider)(nil)
|
||||
|
|
@ -101,7 +110,7 @@ var _ api.VehicleOdometer = (*Provider)(nil)
|
|||
// Odometer implements the api.VehicleOdometer interface
|
||||
func (v *Provider) Odometer() (odo float64, err error) {
|
||||
res, err := v.statusG()
|
||||
return res.Remote.MileageInKm, err
|
||||
return res.MileageInKm, err
|
||||
}
|
||||
|
||||
// var _ api.VehicleClimater = (*Provider)(nil)
|
||||
|
|
@ -129,9 +138,9 @@ var _ api.SocLimiter = (*Provider)(nil)
|
|||
|
||||
// GetLimitSoc implements the api.SocLimiter interface
|
||||
func (v *Provider) GetLimitSoc() (int64, error) {
|
||||
res, err := v.settingsG()
|
||||
res, err := v.chargerG()
|
||||
if err == nil {
|
||||
return int64(res.TargetStateOfChargeInPercent), nil
|
||||
return int64(res.Settings.TargetStateOfChargeInPercent), nil
|
||||
}
|
||||
|
||||
return 0, err
|
||||
|
|
@ -144,3 +153,10 @@ func (v *Provider) ChargeEnable(enable bool) error {
|
|||
action := map[bool]string{true: ActionChargeStart, false: ActionChargeStop}
|
||||
return v.action(ActionCharge, action[enable])
|
||||
}
|
||||
|
||||
var _ api.Resurrector = (*Provider)(nil)
|
||||
|
||||
// WakeUp implements the api.Resurrector interface
|
||||
func (v *Provider) WakeUp() error {
|
||||
return v.wakeup()
|
||||
}
|
||||
|
|
|
|||
25
vehicle/skoda/service/tokenrefreshservice.go
Normal file
25
vehicle/skoda/service/tokenrefreshservice.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/vehicle/skoda/tokenrefreshservice"
|
||||
"github.com/evcc-io/evcc/vehicle/vag"
|
||||
"github.com/evcc-io/evcc/vehicle/vag/vwidentity"
|
||||
)
|
||||
|
||||
func TokenRefreshServiceTokenSource(log *util.Logger, data, q url.Values, user, password string) (vag.TokenSource, error) {
|
||||
q, err := vwidentity.Login(log, q, user, password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
trs := tokenrefreshservice.New(log, data)
|
||||
token, err := trs.Exchange(q)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return trs.TokenSource(token), nil
|
||||
}
|
||||
91
vehicle/skoda/tokenrefreshservice/endpoint.go
Normal file
91
vehicle/skoda/tokenrefreshservice/endpoint.go
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
package tokenrefreshservice
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/request"
|
||||
"github.com/evcc-io/evcc/util/urlvalues"
|
||||
"github.com/evcc-io/evcc/vehicle/vag"
|
||||
)
|
||||
|
||||
const (
|
||||
BaseURL = "https://mysmob.api.connect.skoda-auto.cz"
|
||||
CodeExchangeURL = BaseURL + "/api/v1/authentication/exchange-authorization-code?tokenType=CONNECT"
|
||||
RefreshTokenURL = BaseURL + "/api/v1/authentication/refresh-token?tokenType=CONNECT"
|
||||
)
|
||||
|
||||
var _ vag.TokenExchanger = (*Service)(nil)
|
||||
|
||||
type Service struct {
|
||||
*request.Helper
|
||||
data url.Values
|
||||
}
|
||||
|
||||
type skodaTokenResponse struct {
|
||||
AccessToken string `json:"accessToken"`
|
||||
RefreshToken string `json:"refreshToken"`
|
||||
IdToken string `json:"idToken"`
|
||||
}
|
||||
|
||||
func (s *skodaTokenResponse) toVagToken(v *vag.Token) {
|
||||
v.AccessToken = s.AccessToken
|
||||
v.RefreshToken = s.RefreshToken
|
||||
v.IDToken = s.IdToken
|
||||
v.Expiry = time.Now().Add(time.Minute * 60)
|
||||
}
|
||||
|
||||
func New(log *util.Logger, q url.Values) *Service {
|
||||
return &Service{
|
||||
Helper: request.NewHelper(log),
|
||||
data: q,
|
||||
}
|
||||
}
|
||||
|
||||
func (v *Service) Exchange(q url.Values) (*vag.Token, error) {
|
||||
if err := urlvalues.Require(q, "id_token", "code"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var skTok skodaTokenResponse
|
||||
|
||||
data := map[string]string{
|
||||
"code": q.Get("code"),
|
||||
"redirectUri": "myskoda://redirect/login/",
|
||||
"verifier": q.Get("code_verifier"),
|
||||
}
|
||||
|
||||
resp, err := v.Post(CodeExchangeURL, request.JSONContent, request.MarshalJSON(data))
|
||||
|
||||
if err == nil {
|
||||
defer resp.Body.Close()
|
||||
json.NewDecoder(resp.Body).Decode(&skTok)
|
||||
}
|
||||
|
||||
var res vag.Token
|
||||
skTok.toVagToken(&res)
|
||||
return &res, err
|
||||
}
|
||||
|
||||
func (v *Service) Refresh(token *vag.Token) (*vag.Token, error) {
|
||||
var skTok skodaTokenResponse
|
||||
|
||||
data := map[string]string{"token": token.RefreshToken}
|
||||
resp, err := v.Post(RefreshTokenURL, request.JSONContent, request.MarshalJSON(data))
|
||||
|
||||
if err == nil {
|
||||
defer resp.Body.Close()
|
||||
json.NewDecoder(resp.Body).Decode(&skTok)
|
||||
}
|
||||
|
||||
var res vag.Token
|
||||
skTok.toVagToken(&res)
|
||||
return &res, err
|
||||
}
|
||||
|
||||
// TokenSource creates token source. Token is refreshed automatically.
|
||||
func (v *Service) TokenSource(token *vag.Token) vag.TokenSource {
|
||||
return vag.RefreshTokenSource(token, v.Refresh)
|
||||
}
|
||||
|
|
@ -38,29 +38,24 @@ type Specification struct {
|
|||
|
||||
// StatusResponse is the /v2/vehicle-status/<vin> api
|
||||
type StatusResponse struct {
|
||||
Remote struct {
|
||||
MileageInKm float64
|
||||
}
|
||||
MileageInKm float64
|
||||
}
|
||||
|
||||
// ChargerResponse is the /v1/charging/<vin>/status api
|
||||
// ChargerResponse is the /v2/charging/<vin> api
|
||||
type ChargerResponse struct {
|
||||
Plug struct {
|
||||
ConnectionState string // Connected
|
||||
LockState string // Unlocked
|
||||
}
|
||||
Charging struct {
|
||||
State string // Error
|
||||
RemainingToCompleteInSeconds int64
|
||||
ChargingPowerInWatts float64
|
||||
ChargingRateInKilometersPerHour float64
|
||||
ChargingType string // Invalid
|
||||
ChargeMode string // MANUAL
|
||||
}
|
||||
Battery struct {
|
||||
CruisingRangeElectricInMeters int64
|
||||
StateOfChargeInPercent int
|
||||
IsVehicleInSaveLocation bool
|
||||
Status struct {
|
||||
ChargingRateInKilometersPerHour float64
|
||||
ChargePowerInKw float64
|
||||
RemainingTimeToFullyChargedInMinutes int64
|
||||
State string
|
||||
ChargeType string
|
||||
Battery struct {
|
||||
RemainingCruisingRangeInMeters int64
|
||||
StateOfChargeInPercent int
|
||||
}
|
||||
}
|
||||
Settings SettingsResponse
|
||||
}
|
||||
|
||||
// SettingsResponse is the /v1/charging/<vin>/settings api
|
||||
|
|
@ -69,3 +64,10 @@ type SettingsResponse struct {
|
|||
MaxChargeCurrentAc string `json:"maxChargeCurrentAc"`
|
||||
TargetStateOfChargeInPercent int `json:"targetStateOfChargeInPercent"`
|
||||
}
|
||||
|
||||
// ChargerResponse is the /v2/air-conditioning/<vin> api
|
||||
type ClimaterResponse struct {
|
||||
State string `json:"state"`
|
||||
ChargerConnectionState string `json:"chargerConnectionState"`
|
||||
ChargerLockState string `json:"chargerLockState"`
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue