-
-
Notifications
You must be signed in to change notification settings - Fork 719
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Plugins: add charger provider (#18037)
- Loading branch information
Showing
4 changed files
with
118 additions
and
26 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,27 @@ | ||
package config | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/evcc-io/evcc/api" | ||
reg "github.com/evcc-io/evcc/util/registry" | ||
) | ||
|
||
var Registry = reg.New[api.Charger]("charger") | ||
|
||
// NewFromConfig creates charger from configuration | ||
func NewFromConfig(ctx context.Context, typ string, other map[string]interface{}) (api.Charger, error) { | ||
factory, err := Registry.Get(strings.ToLower(typ)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
v, err := factory(ctx, other) | ||
if err != nil { | ||
err = fmt.Errorf("cannot create charger type '%s': %w", typ, err) | ||
} | ||
|
||
return v, err | ||
} |
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,87 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/evcc-io/evcc/api" | ||
charger "github.com/evcc-io/evcc/charger/config" | ||
"github.com/evcc-io/evcc/util" | ||
"github.com/evcc-io/evcc/util/config" | ||
"github.com/spf13/cast" | ||
) | ||
|
||
type switchChargerProvider struct { | ||
charger api.Charger | ||
} | ||
|
||
func init() { | ||
registry.AddCtx("charger", NewChargerEnableFromConfig) | ||
} | ||
|
||
// NewChargerEnableFromConfig creates type conversion provider | ||
func NewChargerEnableFromConfig(ctx context.Context, other map[string]interface{}) (Provider, error) { | ||
var cc struct { | ||
Config config.Typed | ||
} | ||
|
||
if err := util.DecodeOther(other, &cc); err != nil { | ||
return nil, err | ||
} | ||
|
||
charger, err := charger.NewFromConfig(ctx, cc.Config.Type, cc.Config.Other) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
o := &switchChargerProvider{ | ||
charger: charger, | ||
} | ||
|
||
return o, nil | ||
} | ||
|
||
var _ FloatProvider = (*switchChargerProvider)(nil) | ||
|
||
func (o *switchChargerProvider) FloatGetter() (func() (float64, error), error) { | ||
return func() (float64, error) { | ||
v, err := o.charger.Enabled() | ||
return cast.ToFloat64(v), err | ||
}, nil | ||
} | ||
|
||
var _ IntProvider = (*switchChargerProvider)(nil) | ||
|
||
func (o *switchChargerProvider) IntGetter() (func() (int64, error), error) { | ||
return func() (int64, error) { | ||
v, err := o.charger.Enabled() | ||
return cast.ToInt64(v), err | ||
}, nil | ||
} | ||
|
||
var _ SetIntProvider = (*switchChargerProvider)(nil) | ||
|
||
func (o *switchChargerProvider) IntSetter(param string) (func(int64) error, error) { | ||
return func(val int64) error { | ||
b, err := cast.ToBoolE(val) | ||
if err != nil { | ||
return err | ||
} | ||
return o.charger.Enable(b) | ||
}, nil | ||
} | ||
|
||
var _ BoolProvider = (*switchChargerProvider)(nil) | ||
|
||
func (o *switchChargerProvider) BoolGetter() (func() (bool, error), error) { | ||
return func() (bool, error) { | ||
return o.charger.Enabled() | ||
}, nil | ||
} | ||
|
||
var _ SetBoolProvider = (*switchChargerProvider)(nil) | ||
|
||
func (o *switchChargerProvider) BoolSetter(param string) (func(bool) error, error) { | ||
return func(val bool) error { | ||
return o.charger.Enable(val) | ||
}, nil | ||
} |
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