Skip to content

Commit

Permalink
Plugins: add charger provider (#18037)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Jan 5, 2025
1 parent 927cd39 commit 5b21646
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 26 deletions.
18 changes: 3 additions & 15 deletions charger/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package charger

import (
"context"
"fmt"
"strings"

"github.com/evcc-io/evcc/api"
reg "github.com/evcc-io/evcc/util/registry"
"github.com/evcc-io/evcc/charger/config"
)

var registry = reg.New[api.Charger]("charger")
var registry = config.Registry

// Types returns the list of types
func Types() []string {
Expand All @@ -18,15 +16,5 @@ func Types() []string {

// 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
return config.NewFromConfig(ctx, typ, other)
}
27 changes: 27 additions & 0 deletions charger/config/config.go
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
}
87 changes: 87 additions & 0 deletions provider/charger.go
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
}
12 changes: 1 addition & 11 deletions provider/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,9 @@ func NewConstFromConfig(ctx context.Context, other map[string]interface{}) (Prov
return nil, err
}

pipe, err := pipeline.New(nil, cc.Settings)
if err != nil {
return nil, err
}

b, err := pipe.Process([]byte(cc.Value))
if err != nil {
return nil, err
}

p := &constProvider{
ctx: ctx,
str: string(b),
str: cc.Value,
set: cc.Set,
}

Expand Down

0 comments on commit 5b21646

Please sign in to comment.