Toyota Connected: Toyota, Lexus and Subaru as three plugins
One connector, registered once per brand with the brand fixed, so the
panel, the Web App and the Phone App each list Toyota, Lexus and Subaru
separately: own card, own account, own enable switch, own
/api/integrations/{brand} routes and vehicle provider. The brand picker
is gone.
Settings saved before the split under pluginSettings.toyota with brand
L or S move onto their brand's key on read, and are written back on the
next save. Cars linked as provider "toyota" read through their make's
brand.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9d8636ee73
commit
914aeeee23
21 files changed
+837
-471
No files matched your search
@@ -177,7 +177,8 @@ Restart the server. The plugin appears in the panel's **Plugins** card,
|
||||
**disabled** by default.
|
||||
|
||||
> DriverVault ships four built-in connectors today — `toyota` (Toyota Connected /
|
||||
> MyToyota, read-only vehicle data), `anker-solix` (Anker Solix V1 EV charger),
|
||||
> MyToyota, read-only vehicle data; registered three times, as `toyota`, `lexus`
|
||||
> and `subaru`, so each brand shows up as a plugin of its own), `anker-solix` (Anker Solix V1 EV charger),
|
||||
> `greencell` (Greencell HabuDen EV charger, read over the owner's MQTT broker
|
||||
> rather than a cloud API) and `apprise` (notifications, through an apprise-api
|
||||
> gateway the operator runs) — all blank-imported from `builtin/builtin.go`. The
|
||||
|
||||
@@ -24,6 +24,9 @@ func TestEveryBuiltinIsRegistered(t *testing.T) {
|
||||
"apprise": plugins.CategoryNotifications,
|
||||
"greencell": plugins.CategoryChargers,
|
||||
"toyota": plugins.CategoryVehicles,
|
||||
// One connector, three registrations: see toyota.Brands.
|
||||
"lexus": plugins.CategoryVehicles,
|
||||
"subaru": plugins.CategoryVehicles,
|
||||
}
|
||||
for name, category := range want {
|
||||
v, ok := seen[name]
|
||||
|
||||
@@ -138,8 +138,41 @@ const (
|
||||
// never fires with a token that expires mid-flight.
|
||||
const tokenExpiryMargin = 30 * time.Second
|
||||
|
||||
// Brand is one of the three makes this connector serves. The apps show each as
|
||||
// a plugin of its own — its own panel entry, its own settings card, its own
|
||||
// credentials — because an owner thinks of "my Lexus account", not of the
|
||||
// Toyota Connected Europe backend the three happen to share. The code is one
|
||||
// connector; only the login realm and a few headers differ (see authFor).
|
||||
type Brand struct {
|
||||
Name string // plugin name, also the key under pluginSettings: "toyota"
|
||||
Code string // the one-letter code the API uses on the wire: "T"
|
||||
Make string // "Toyota"
|
||||
App string // the app the owner knows the account by: "MyToyota"
|
||||
Provider string // the panel's provider line
|
||||
}
|
||||
|
||||
// Brands are the registered makes, in menu order.
|
||||
var Brands = []Brand{
|
||||
{Name: "toyota", Code: "T", Make: "Toyota", App: "MyToyota", Provider: "Toyota Connected Europe"},
|
||||
{Name: "lexus", Code: "L", Make: "Lexus", App: "Lexus Link+", Provider: "Lexus (Toyota Connected Europe)"},
|
||||
{Name: "subaru", Code: "S", Make: "Subaru", App: "MySubaru", Provider: "Subaru (Toyota Connected Europe)"},
|
||||
}
|
||||
|
||||
// brandByCode finds a brand by its wire code; Toyota for anything unknown.
|
||||
func brandByCode(code string) Brand {
|
||||
for _, b := range Brands {
|
||||
if b.Code == code {
|
||||
return b
|
||||
}
|
||||
}
|
||||
return Brands[0]
|
||||
}
|
||||
|
||||
func init() {
|
||||
plugins.Register("toyota", func() plugins.Plugin { return &Plugin{} })
|
||||
for _, b := range Brands {
|
||||
code := b.Code
|
||||
plugins.Register(b.Name, func() plugins.Plugin { return &Plugin{fixedBrand: code} })
|
||||
}
|
||||
}
|
||||
|
||||
// tokenInfo holds the currently-held OAuth tokens and the derived account UUID.
|
||||
@@ -154,8 +187,11 @@ type tokenInfo struct {
|
||||
type Plugin struct {
|
||||
username string
|
||||
password string
|
||||
brand string // "T" (Toyota), "L" (Lexus) or "S" (Subaru)
|
||||
auth brandAuth // the OAuth client for brand, fixed at Init
|
||||
// fixedBrand is the brand this registration serves, set by its factory. A
|
||||
// bare &Plugin{} (tests) has none and takes the brand from config instead.
|
||||
fixedBrand string
|
||||
brand string // "T" (Toyota), "L" (Lexus) or "S" (Subaru)
|
||||
auth brandAuth // the OAuth client for brand, fixed at Init
|
||||
|
||||
mu sync.Mutex // guards tok and the login flow
|
||||
tok *tokenInfo
|
||||
@@ -169,9 +205,10 @@ type Plugin struct {
|
||||
|
||||
// Descriptor returns the plugin's static metadata for the admin panel.
|
||||
func (p *Plugin) Descriptor() plugins.Descriptor {
|
||||
b := brandByCode(p.fixedBrand)
|
||||
return plugins.Descriptor{
|
||||
Name: "toyota",
|
||||
Provider: "Toyota Connected Europe",
|
||||
Name: b.Name,
|
||||
Provider: b.Provider,
|
||||
Version: "0.4.0",
|
||||
Kind: plugins.KindBuiltin,
|
||||
Category: plugins.CategoryVehicles,
|
||||
@@ -190,21 +227,15 @@ func (p *Plugin) Descriptor() plugins.Descriptor {
|
||||
},
|
||||
ConfigFields: []plugins.ConfigField{
|
||||
// Credentials are intentionally NOT required at the global (panel) layer:
|
||||
// this connector runs under each user's own MyToyota account, supplied in
|
||||
// the Web App's per-user integration settings. A superadmin/org admin may
|
||||
// this connector runs under each user's own account, supplied in the
|
||||
// Web App's per-user integration settings. A superadmin/org admin may
|
||||
// still set a shared (e.g. fleet) account here that users inherit. See the
|
||||
// cascade in internal/api/integrations.go.
|
||||
{Key: "username", Label: "MyToyota email", Type: "text",
|
||||
Help: "The email address for your MyToyota (Toyota Connected Europe) account."},
|
||||
{Key: "password", Label: "MyToyota password", Type: "password", Secret: true,
|
||||
Help: "Your MyToyota account password. Stored locally, sent only to Toyota's login endpoint."},
|
||||
{Key: "brand", Label: "Brand", Type: "select", Default: "T",
|
||||
Help: "Vehicle brand tied to the account. Subaru accounts (Solterra) log in through their own realm on the same service.",
|
||||
Options: []plugins.SelectOption{
|
||||
{Value: "T", Label: "Toyota"},
|
||||
{Value: "L", Label: "Lexus"},
|
||||
{Value: "S", Label: "Subaru"},
|
||||
}},
|
||||
// cascade in internal/api/integrations.go. There is no brand field: each
|
||||
// brand is its own registration.
|
||||
{Key: "username", Label: b.App + " email", Type: "text",
|
||||
Help: "The email address for your " + b.App + " account."},
|
||||
{Key: "password", Label: b.App + " password", Type: "password", Secret: true,
|
||||
Help: "Your " + b.App + " account password. Stored locally, sent only to the " + b.Make + " login endpoint."},
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -217,7 +248,10 @@ func (p *Plugin) Init(_ context.Context, config map[string]string) error {
|
||||
|
||||
p.username = strings.TrimSpace(config["username"])
|
||||
p.password = config["password"]
|
||||
p.brand = strings.TrimSpace(config["brand"])
|
||||
p.brand = p.fixedBrand
|
||||
if p.brand == "" {
|
||||
p.brand = strings.TrimSpace(config["brand"])
|
||||
}
|
||||
if p.brand == "" {
|
||||
p.brand = "T"
|
||||
}
|
||||
|
||||
@@ -22,34 +22,58 @@ func TestDescriptor(t *testing.T) {
|
||||
if len(d.Capabilities) == 0 {
|
||||
t.Fatal("expected capabilities")
|
||||
}
|
||||
// The credential + brand fields must be advertised. They are intentionally
|
||||
// NOT required at the global layer — credentials come from the per-user
|
||||
// cascade — and the password field must be marked secret.
|
||||
// The credential fields must be advertised. They are intentionally NOT
|
||||
// required at the global layer — credentials come from the per-user cascade —
|
||||
// and the password field must be marked secret. There is no brand field: each
|
||||
// brand is a registration of its own.
|
||||
fields := map[string]plugins.ConfigField{}
|
||||
for _, f := range d.ConfigFields {
|
||||
fields[f.Key] = f
|
||||
}
|
||||
for _, k := range []string{"username", "password", "brand"} {
|
||||
for _, k := range []string{"username", "password"} {
|
||||
if _, ok := fields[k]; !ok {
|
||||
t.Errorf("config field %q should be present", k)
|
||||
}
|
||||
}
|
||||
if _, ok := fields["brand"]; ok {
|
||||
t.Error("the brand is fixed per registration and must not be a config field")
|
||||
}
|
||||
if fields["username"].Required || fields["password"].Required {
|
||||
t.Error("credentials must not be required at the global layer (per-user cascade)")
|
||||
}
|
||||
if !fields["password"].Secret {
|
||||
t.Error("password field must be marked secret")
|
||||
}
|
||||
// The three brands that share the backend must all be offered: a value the
|
||||
// select does not list cannot be chosen from the panel.
|
||||
brands := map[string]bool{}
|
||||
for _, o := range fields["brand"].Options {
|
||||
brands[o.Value] = true
|
||||
}
|
||||
|
||||
// TestBrandRegistrations pins the three faces of the one connector: each brand
|
||||
// is its own plugin name with its brand fixed, so the panel and the apps list
|
||||
// Toyota, Lexus and Subaru separately, and a config cannot move one onto
|
||||
// another's login.
|
||||
func TestBrandRegistrations(t *testing.T) {
|
||||
want := map[string]string{"toyota": "T", "lexus": "L", "subaru": "S"}
|
||||
if len(Brands) != len(want) {
|
||||
t.Fatalf("Brands = %d entries, want %d", len(Brands), len(want))
|
||||
}
|
||||
for _, b := range []string{"T", "L", "S"} {
|
||||
if !brands[b] {
|
||||
t.Errorf("brand option %q should be offered", b)
|
||||
for _, b := range Brands {
|
||||
code, ok := want[b.Name]
|
||||
if !ok || code != b.Code {
|
||||
t.Errorf("brand %q has code %q, want %q", b.Name, b.Code, code)
|
||||
}
|
||||
p := &Plugin{fixedBrand: b.Code}
|
||||
if d := p.Descriptor(); d.Name != b.Name || d.Category != plugins.CategoryVehicles {
|
||||
t.Errorf("brand %q: descriptor name %q category %q", b.Name, d.Name, d.Category)
|
||||
}
|
||||
// A stray brand in config (a pre-split setting) must not win.
|
||||
_ = p.Init(context.Background(), map[string]string{"username": "u", "password": "p", "brand": "T"})
|
||||
if p.brand != b.Code {
|
||||
t.Errorf("brand %q: Init with brand=T gave %q", b.Name, p.brand)
|
||||
}
|
||||
}
|
||||
s := &Plugin{fixedBrand: "S"}
|
||||
_ = s.Init(context.Background(), map[string]string{"username": "u", "password": "p"})
|
||||
if s.auth.clientID != subaruAuth.clientID {
|
||||
t.Errorf("subaru registration logs in as %q", s.auth.clientID)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user