Skip to content

Commit

Permalink
Config UI: initialize circuits from settings db (#15073)
Browse files Browse the repository at this point in the history
  • Loading branch information
naltatis authored Jul 27, 2024
1 parent 24e448e commit 581f70e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 55 deletions.
1 change: 0 additions & 1 deletion assets/js/components/Config/CircuitsModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
docs="/docs/features/loadmanagement"
:defaultYaml="defaultYaml"
endpoint="/config/circuits"
size="md"
@changed="$emit('changed')"
/>
</template>
Expand Down
12 changes: 6 additions & 6 deletions assets/js/components/Config/defaultYaml/circuits.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# - name: main # unique name, used as reference, e.g. as parent in other circuits
# title: Main Circuit # used in the UI
# maxCurrent: 63 # 63A main circuit breaker (optional)
# maxPower: 30000 # 30kW (optional)
# maxcurrent: 63 # 63A main circuit breaker (optional)
# maxpower: 30000 # 30kW (optional)
# meter: grid # associated meter to monitor the power consumption (optional)
# parent: # no parent, this is the root circuit
# - name: garage # unique name, used as reference, e.g. to associate loadpoints
# title: Garage # used in the UI
# maxCurrent: 24 # allow individual phase use up to 24A
# maxPower: 11000 # limit total power to 11kW
# maxcurrent: 24 # allow individual phase use up to 24A
# maxpower: 11000 # limit total power to 11kW
# meter: garage # dedicated meter for the garage
# parent: main # parent to the main circuit
# - name: carport # unique name, used as reference, e.g. to associate loadpoints
# title: Carport # used in the UI
# maxCurrent: 32 # 32A circuit breaker
# maxPower: # no limit, only check current
# maxcurrent: 32 # 32A circuit breaker
# maxpower: # no limit, only check current
# meter: # no meter, using data from loadpoints
# parent: main # parent to the main circuit
57 changes: 9 additions & 48 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,15 @@ If you know what you're doing, you can run evcc ignoring the service database wi
return err
}

func configureCircuits(static []config.Named, names ...string) error {
children := slices.Clone(static)
func configureCircuits(conf []config.Named, names ...string) error {
// migrate settings
if settings.Exists(keys.Circuits) {
if err := settings.Yaml(keys.Circuits, new([]map[string]any), &conf); err != nil {
return err
}
}

children := slices.Clone(conf)

// TODO: check for circular references
NEXT:
Expand Down Expand Up @@ -176,52 +183,6 @@ NEXT:
return fmt.Errorf("circuit is missing parent: %s", children[0].Name)
}

// append devices from database
configurable, err := config.ConfigurationsByClass(templates.Circuit)
if err != nil {
return err
}

children2 := slices.Clone(configurable)

NEXT2:
for i, conf := range children2 {
cc := conf.Named()

if len(names) > 0 && !slices.Contains(names, cc.Name) {
return nil
}

if parent := cast.ToString(cc.Property("parent")); parent != "" {
if _, err := config.Circuits().ByName(parent); err != nil {
continue
}
}

log := util.NewLogger("circuit-" + cc.Name)
instance, err := core.NewCircuitFromConfig(log, cc.Other)
if err != nil {
return fmt.Errorf("cannot create circuit '%s': %w", cc.Name, err)
}

// ensure config has title
if instance.GetTitle() == "" {
//lint:ignore SA1019 as Title is safe on ascii
instance.SetTitle(strings.Title(cc.Name))
}

if err := config.Circuits().Add(config.NewConfigurableDevice(conf, instance)); err != nil {
return err
}

children2 = slices.Delete(children2, i, i+1)
goto NEXT2
}

if len(children2) > 0 {
return fmt.Errorf("missing parent circuit: %s", children2[0].Named().Name)
}

var rootFound bool
for _, dev := range config.Circuits().Devices() {
c := dev.Instance()
Expand Down

0 comments on commit 581f70e

Please sign in to comment.