Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions charger/eebus.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ func init() {
// NewEEBusFromConfig creates an EEBus charger from generic config
func NewEEBusFromConfig(ctx context.Context, other map[string]any) (api.Charger, error) {
var cc struct {
Ski string
Ip string
Ski, Ip string
Meter bool
ChargedEnergy *bool
VasVW bool
Expand Down
3 changes: 3 additions & 0 deletions cmd/config_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/core/loadpoint"
"github.com/evcc-io/evcc/hems/hems"
"github.com/evcc-io/evcc/util/config"
"github.com/evcc-io/evcc/util/templates"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -56,6 +57,8 @@ func runConfigDelete(cmd *cobra.Command, args []string) {
deleteDevice[api.Tariff](c)
case templates.Circuit:
deleteDevice[api.Circuit](c)
case templates.Hems:
deleteDevice[hems.API](c)
case templates.Loadpoint:
deleteDevice[loadpoint.API](c)
}
Expand Down
20 changes: 12 additions & 8 deletions hems/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@ import (
"strings"

"github.com/evcc-io/evcc/core/site"
"github.com/evcc-io/evcc/hems/eebus"
"github.com/evcc-io/evcc/hems/config"
_ "github.com/evcc-io/evcc/hems/eebus"
"github.com/evcc-io/evcc/hems/hems"
"github.com/evcc-io/evcc/hems/relay"
_ "github.com/evcc-io/evcc/hems/relay"
)

// NewFromConfig creates new HEMS from config
var registry = config.Registry

// Types returns the list of types
func Types() []string {
return registry.Types()
}

// NewFromConfig creates hems from configuration
func NewFromConfig(ctx context.Context, typ string, other map[string]any, site site.API) (hems.API, error) {
switch strings.ToLower(typ) {
case "sma", "shm", "semp":
return nil, errors.New("breaking change: Sunny Home Manager integration is always on. See https://github.com/evcc-io/evcc/releases and https://docs.evcc.io/en/docs/integrations/sma-sunny-home-manager")
case "eebus":
return eebus.NewFromConfig(ctx, other, site)
case "relay":
return relay.NewFromConfig(ctx, other, site)
default:
return nil, errors.New("unknown hems: " + typ)
return config.NewFromConfig(ctx, typ, other, site)
}
}
28 changes: 28 additions & 0 deletions hems/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package config

import (
"context"
"fmt"
"strings"

"github.com/evcc-io/evcc/core/site"
"github.com/evcc-io/evcc/hems/hems"
reg "github.com/evcc-io/evcc/hems/registry"
)

var Registry = reg.New[hems.API]("hems")

// NewFromConfig creates hems from configuration
func NewFromConfig(ctx context.Context, typ string, other map[string]any, site site.API) (hems.API, error) {
factory, err := Registry.Get(strings.ToLower(typ))
if err != nil {
return nil, err
}

v, err := factory(ctx, other, site)
if err != nil {
return nil, fmt.Errorf("cannot create hems type '%s': %w", typ, err)
}

return v, nil
}
16 changes: 11 additions & 5 deletions hems/eebus/eebus.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/core/circuit"
"github.com/evcc-io/evcc/core/site"
"github.com/evcc-io/evcc/hems/config"
"github.com/evcc-io/evcc/hems/hems"
"github.com/evcc-io/evcc/hems/shared"
"github.com/evcc-io/evcc/hems/smartgrid"
"github.com/evcc-io/evcc/plugin"
Expand All @@ -18,6 +20,10 @@ import (
"github.com/samber/lo"
)

func init() {
config.Registry.AddCtx("eebus", NewFromConfig)
}

type EEBus struct {
mux sync.RWMutex
log *util.Logger
Expand Down Expand Up @@ -58,9 +64,9 @@ type Limits struct {
}

// NewFromConfig creates an EEBus HEMS from generic config
func NewFromConfig(ctx context.Context, other map[string]any, site site.API) (*EEBus, error) {
func NewFromConfig(ctx context.Context, other map[string]any, site site.API) (hems.API, error) {
cc := struct {
Ski string
Ski, Ip string
Limits `mapstructure:",squash"`
Passthrough *plugin.Config
Interval time.Duration
Expand Down Expand Up @@ -104,11 +110,11 @@ func NewFromConfig(ctx context.Context, other map[string]any, site site.API) (*E
}
site.SetCircuit(gridcontrol)

return NewEEBus(ctx, cc.Ski, cc.Limits, passthroughS, gridcontrol, cc.Interval)
return NewEEBus(ctx, cc.Ski, cc.Ip, cc.Limits, passthroughS, gridcontrol, cc.Interval)
}

// NewEEBus creates EEBus HEMS
func NewEEBus(ctx context.Context, ski string, limits Limits, passthrough func(bool) error, root api.Circuit, interval time.Duration) (*EEBus, error) {
func NewEEBus(ctx context.Context, ski, ip string, limits Limits, passthrough func(bool) error, root api.Circuit, interval time.Duration) (*EEBus, error) {
if eebus.Instance == nil {
return nil, errors.New("eebus not configured")
}
Expand All @@ -131,7 +137,7 @@ func NewEEBus(ctx context.Context, ski string, limits Limits, passthrough func(b
// otherwise a heartbeat timeout is assumed when the state machine is called for the first time
c.heartbeat.Set(struct{}{})

if err := eebus.Instance.RegisterDevice(ski, "", c); err != nil {
if err := eebus.Instance.RegisterDevice(ski, ip, c); err != nil {
return nil, err
}

Expand Down
52 changes: 52 additions & 0 deletions hems/registry/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package registry

import (
"context"
"fmt"
"maps"
"slices"

"github.com/evcc-io/evcc/core/site"
)

type (
// TODO check is this can be abstracted into a generic type parameter
factoryFunc[T any] func(context.Context, map[string]any, site.API) (T, error)

registry[T any] struct {
typ string
data map[string]factoryFunc[T]
}
)

func (r registry[T]) Add(name string, factory func(map[string]any, site.API) (T, error)) {
r.AddCtx(name, func(_ context.Context, cc map[string]any, site site.API) (T, error) {
return factory(cc, site)
})
}

func (r registry[T]) AddCtx(name string, factory factoryFunc[T]) {
if _, exists := r.data[name]; exists {
panic(fmt.Sprintf("cannot register duplicate %s type: %s", r.typ, name))
}
r.data[name] = factory
}

func (r registry[T]) Get(name string) (factoryFunc[T], error) {
factory, exists := r.data[name]
if !exists {
return nil, fmt.Errorf("invalid %s type: %s", r.typ, name)
}
return factory, nil
}

func (r registry[T]) Types() []string {
return slices.Sorted(maps.Keys(r.data))
}

func New[T any](typ string) registry[T] {
return registry[T]{
typ: typ,
data: make(map[string]factoryFunc[T]),
}
}
8 changes: 7 additions & 1 deletion hems/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ import (
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/core/circuit"
"github.com/evcc-io/evcc/core/site"
"github.com/evcc-io/evcc/hems/config"
"github.com/evcc-io/evcc/hems/hems"
"github.com/evcc-io/evcc/hems/shared"
"github.com/evcc-io/evcc/hems/smartgrid"
"github.com/evcc-io/evcc/plugin"
"github.com/evcc-io/evcc/util"
"github.com/samber/lo"
)

func init() {
config.Registry.AddCtx("relay", NewFromConfig)
}

type Relay struct {
log *util.Logger

Expand All @@ -29,7 +35,7 @@ type Relay struct {
}

// NewFromConfig creates an Relay HEMS from generic config
func NewFromConfig(ctx context.Context, other map[string]any, site site.API) (*Relay, error) {
func NewFromConfig(ctx context.Context, other map[string]any, site site.API) (hems.API, error) {
cc := struct {
MaxPower float64
Limit plugin.Config
Expand Down
22 changes: 22 additions & 0 deletions hems/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package hems

import (
"context"

"github.com/evcc-io/evcc/core/site"
"github.com/evcc-io/evcc/hems/hems"
"github.com/evcc-io/evcc/util/templates"
)

func init() {
registry.AddCtx("template", NewHemsFromTemplateConfig)
}

func NewHemsFromTemplateConfig(ctx context.Context, other map[string]any, site site.API) (hems.API, error) {
instance, err := templates.RenderInstance(templates.Hems, other)
if err != nil {
return nil, err
}

return NewFromConfig(ctx, instance.Type, instance.Other, site)
}
3 changes: 1 addition & 2 deletions meter/eebus.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ func init() {
// NewEEBusFromConfig creates an EEBus meter from generic config
func NewEEBusFromConfig(ctx context.Context, other map[string]any) (api.Meter, error) {
var cc struct {
Ski string
Ip string
Ski, Ip string
Usage *templates.Usage
Timeout_ time.Duration `mapstructure:"timeout"` // TODO deprecated
}
Expand Down
2 changes: 1 addition & 1 deletion server/eebus/test/cs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestEEBus(t *testing.T) {
gridcontrol, err := circuit.New(util.NewLogger("gridcontrol"), "gridcontrol", 0, 0, nil, time.Minute)
require.NoError(t, err)

hems, err := hems.NewEEBus(t.Context(), box.ski, eebus.Limits{}, nil, gridcontrol, time.Second)
hems, err := hems.NewEEBus(t.Context(), box.ski, "", eebus.Limits{}, nil, gridcontrol, time.Second)
require.NoError(t, err, "hems")

go hems.Run()
Expand Down
4 changes: 2 additions & 2 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ func (s *HTTPd) RegisterSystemHandler(site *core.Site, pub publisher, cache *uti
"devicestatus": {"GET", "/devices/{class:[a-z]+}/{name:[a-zA-Z0-9_.:-]+}/status", deviceStatusHandler},
"dirty": {"GET", "/dirty", getHandler(ConfigDirty)},
"evccyaml": {"GET", "/evcc.yaml", configYamlHandler(configFile)},
"newdevice": {"POST", "/devices/{class:[a-z]+}", newDeviceHandler},
"updatedevice": {"PUT", "/devices/{class:[a-z]+}/{id:[0-9.]+}", updateDeviceHandler},
"newdevice": {"POST", "/devices/{class:[a-z]+}", newDeviceHandler(site)},
"updatedevice": {"PUT", "/devices/{class:[a-z]+}/{id:[0-9.]+}", updateDeviceHandler(site)},
"deletedevice": {"DELETE", "/devices/{class:[a-z]+}/{id:[0-9.]+}", deleteDeviceHandler(site)},
"testconfig": {"POST", "/test/{class:[a-z]+}", testConfigHandler},
"testmerged": {"POST", "/test/{class:[a-z]+}/merge/{id:[0-9.]+}", testConfigHandler},
Expand Down
Loading
Loading