Allow vehicles to use nick name from api as title (#5673)
This commit is contained in:
parent
111f375a0f
commit
95c1d387be
5 changed files with 33 additions and 20 deletions
|
|
@ -138,6 +138,7 @@ type Vehicle interface {
|
|||
Battery
|
||||
BatteryCapacity
|
||||
Title() string
|
||||
SetTitle(string)
|
||||
Icon() string
|
||||
Phases() int
|
||||
Identifiers() []string
|
||||
|
|
|
|||
|
|
@ -258,27 +258,17 @@ func (cp *ConfigProvider) configureVehicles(conf config) error {
|
|||
cc := cc
|
||||
|
||||
g.Go(func() error {
|
||||
// ensure vehicle config has title
|
||||
var ccWithTitle struct {
|
||||
Title string
|
||||
Other map[string]interface{} `mapstructure:",remain"`
|
||||
}
|
||||
|
||||
if err := util.DecodeOther(cc.Other, &ccWithTitle); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if ccWithTitle.Title == "" {
|
||||
//lint:ignore SA1019 as Title is safe on ascii
|
||||
ccWithTitle.Title = strings.Title(cc.Name)
|
||||
cc.Other["title"] = ccWithTitle.Title
|
||||
}
|
||||
|
||||
v, err := vehicle.NewFromConfig(cc.Type, cc.Other)
|
||||
if err != nil {
|
||||
log.ERROR.Printf("creating vehicle %s failed: %v", cc.Name, err)
|
||||
// wrap any created errors to prevent fatals
|
||||
v, _ = wrapper.New(ccWithTitle.Title, err)
|
||||
v = wrapper.New(err)
|
||||
}
|
||||
|
||||
// ensure vehicle config has title
|
||||
if v.Title() == "" {
|
||||
//lint:ignore SA1019 as Title is safe on ascii
|
||||
v.SetTitle(strings.Title(cc.Name))
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
|
|
|
|||
|
|
@ -374,6 +374,18 @@ func (mr *MockVehicleMockRecorder) Phases() *gomock.Call {
|
|||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Phases", reflect.TypeOf((*MockVehicle)(nil).Phases))
|
||||
}
|
||||
|
||||
// SetTitle mocks base method.
|
||||
func (m *MockVehicle) SetTitle(arg0 string) {
|
||||
m.ctrl.T.Helper()
|
||||
m.ctrl.Call(m, "SetTitle", arg0)
|
||||
}
|
||||
|
||||
// SetTitle indicates an expected call of SetTitle.
|
||||
func (mr *MockVehicleMockRecorder) SetTitle(arg0 interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetTitle", reflect.TypeOf((*MockVehicle)(nil).SetTitle), arg0)
|
||||
}
|
||||
|
||||
// Soc mocks base method.
|
||||
func (m *MockVehicle) Soc() (float64, error) {
|
||||
m.ctrl.T.Helper()
|
||||
|
|
|
|||
|
|
@ -20,6 +20,11 @@ func (v *embed) Title() string {
|
|||
return v.Title_
|
||||
}
|
||||
|
||||
// SetTitle implements the api.TitleSetter interface
|
||||
func (v *embed) SetTitle(title string) {
|
||||
v.Title_ = title
|
||||
}
|
||||
|
||||
// Icon implements the api.Vehicle interface
|
||||
func (v *embed) Icon() string {
|
||||
return v.Icon_
|
||||
|
|
|
|||
|
|
@ -14,14 +14,14 @@ type Wrapper struct {
|
|||
}
|
||||
|
||||
// New creates a new Vehicle
|
||||
func New(title string, err error) (api.Vehicle, error) {
|
||||
func New(err error) api.Vehicle {
|
||||
v := &Wrapper{
|
||||
err: fmt.Errorf("vehicle not available: %w", err),
|
||||
title: fmt.Sprintf("%s (unavailable)", title),
|
||||
title: "unavailable",
|
||||
Features_: []api.Feature{api.Offline},
|
||||
}
|
||||
|
||||
return v, nil
|
||||
return v
|
||||
}
|
||||
|
||||
var _ api.Vehicle = (*Wrapper)(nil)
|
||||
|
|
@ -31,6 +31,11 @@ func (v *Wrapper) Title() string {
|
|||
return v.title
|
||||
}
|
||||
|
||||
// SetTitle implements the api.TitleSetter interface
|
||||
func (v *Wrapper) SetTitle(title string) {
|
||||
v.title = fmt.Sprintf("%s (unavailable)", title)
|
||||
}
|
||||
|
||||
// Icon implements the api.Vehicle interface
|
||||
func (v *Wrapper) Icon() string {
|
||||
return ""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue