Co-authored-by: andig <cpuidle@gmail.com> Co-authored-by: andig <andi@evcc.io> Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com> Co-authored-by: Michael Geers <michael@geers.tv>
28 lines
638 B
Go
28 lines
638 B
Go
package messenger
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/evcc-io/evcc/api"
|
|
"github.com/evcc-io/evcc/util"
|
|
reg "github.com/evcc-io/evcc/util/registry"
|
|
)
|
|
|
|
var registry = reg.New[api.Messenger]("messenger")
|
|
|
|
// NewFromConfig creates messenger from configuration
|
|
func NewFromConfig(ctx context.Context, typ string, other map[string]any) (api.Messenger, 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 messenger type '%s': %w", util.TypeWithTemplateName(typ, other), err)
|
|
}
|
|
|
|
return v, err
|
|
}
|