Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vehicles: better naming for unavailable vehicles #7239

Merged
merged 5 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (cp *ConfigProvider) configureVehicles(conf config) error {

// wrap non-config vehicle errors to prevent fatals
log.ERROR.Printf("creating vehicle %s failed: %v", cc.Name, err)
v = wrapper.New(err)
v = wrapper.New(cc.Name, cc.Other, err)
}

// ensure vehicle config has title
Expand Down
34 changes: 29 additions & 5 deletions vehicle/wrapper/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,46 @@ package wrapper

import (
"fmt"
"strings"

"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
)

// Wrapper wraps an api.Vehicle to capture initialization errors
type Wrapper struct {
err error
title string
icon string
phases int
capacity float64
Features_ []api.Feature
}

// New creates a new Vehicle
func New(err error) api.Vehicle {
func New(name string, other map[string]interface{}, err error) api.Vehicle {
var cc struct {
Title string
Icon string
Phases int
Capacity float64
Other map[string]interface{} `mapstructure:",remain"`
}

// try to decode vehicle-specific config and look for title attribute
_ = util.DecodeOther(other, &cc)

if cc.Title == "" {
//lint:ignore SA1019 as Title is safe on ascii
cc.Title = strings.Title(name)
}

v := &Wrapper{
err: fmt.Errorf("vehicle not available: %w", err),
title: "unavailable",
title: fmt.Sprintf("%s (offline)", cc.Title),
icon: cc.Icon,
phases: cc.Phases,
capacity: cc.Capacity,
Features_: []api.Feature{api.Offline},
}

Expand All @@ -38,17 +62,17 @@ func (v *Wrapper) SetTitle(title string) {

// Icon implements the api.Vehicle interface
func (v *Wrapper) Icon() string {
return ""
return v.icon
}

// Capacity implements the api.Vehicle interface
func (v *Wrapper) Capacity() float64 {
return 0
return v.capacity
}

// Phases implements the api.Vehicle interface
func (v *Wrapper) Phases() int {
return 0
return v.phases
}

// Identifiers implements the api.Vehicle interface
Expand Down