Skip to content

Commit

Permalink
Vehicles: show title for unavailable vehicles (#7239)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Apr 5, 2023
1 parent e605f10 commit ceb3e4d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/config.go
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
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

0 comments on commit ceb3e4d

Please sign in to comment.