Skip to content

Commit

Permalink
Fix vehicle settings ignored when initialization fails (#12603)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Mar 2, 2024
1 parent 2f29659 commit be997b1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 108 deletions.
3 changes: 1 addition & 2 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (
"github.com/evcc-io/evcc/util/sponsor"
"github.com/evcc-io/evcc/util/templates"
"github.com/evcc-io/evcc/vehicle"
"github.com/evcc-io/evcc/vehicle/wrapper"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/libp2p/zeroconf/v2"
Expand Down Expand Up @@ -285,7 +284,7 @@ func vehicleInstance(cc config.Named) (api.Vehicle, error) {

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

// ensure vehicle config has title
Expand Down
60 changes: 60 additions & 0 deletions vehicle/wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package vehicle

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 {
embed
err error
}

// NewWrapper creates an offline Vehicle wrapper
func NewWrapper(name string, other map[string]interface{}, err error) api.Vehicle {
var cc struct {
embed `mapstructure:",squash"`
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{
embed: cc.embed,
err: fmt.Errorf("vehicle not available: %w", err),
}

v.Features_ = append(v.Features_, api.Offline)
v.SetTitle(cc.Title_)

return v
}

// Error returns the initialization error
func (v *Wrapper) Error() string {
return v.err.Error()
}

var _ api.Vehicle = (*Wrapper)(nil)

// SetTitle implements the api.TitleSetter interface
func (v *Wrapper) SetTitle(title string) {
v.Title_ = fmt.Sprintf("%s (unavailable)", title)
}

var _ api.Battery = (*Wrapper)(nil)

// Soc implements the api.Battery interface
func (v *Wrapper) Soc() (float64, error) {
return 0, v.err
}
106 changes: 0 additions & 106 deletions vehicle/wrapper/wrapper.go

This file was deleted.

0 comments on commit be997b1

Please sign in to comment.