-
-
Notifications
You must be signed in to change notification settings - Fork 640
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix vehicle settings ignored when initialization fails (#12603)
- Loading branch information
Showing
3 changed files
with
61 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.