Templates: simplify handling of linked templates

This commit is contained in:
andig 2022-10-02 19:20:00 +02:00
parent 8a655efce8
commit 3af110652f

View file

@ -5,6 +5,7 @@ import (
"fmt"
"github.com/evcc-io/evcc/util/templates"
"golang.org/x/exp/slices"
)
// configureDeviceGuidedSetup lets the user choose a device that is set to support guided setup
@ -95,20 +96,12 @@ func (c *CmdConfigure) configureDeviceGuidedSetup() {
// configureLinkedTypes lets the user configure devices that are marked as being linked to a guided device
// e.g. SMA Inverters, Energy Meter with SMA Home Manager
func (c *CmdConfigure) configureLinkedTypes(templateItem templates.Template) {
linkedTemplates := templateItem.Linked
added := make([]string, 0)
deviceOfTemplateAdded := make(map[string]bool)
if linkedTemplates == nil {
return
}
for _, linkedTemplate := range linkedTemplates {
if linkedTemplate.ExcludeTemplate != "" {
// don't process this linked template if a referenced exclude template was added
if deviceOfTemplateAdded[linkedTemplate.ExcludeTemplate] {
continue
}
for _, linkedTemplate := range templateItem.Linked {
// don't process this linked template if a referenced exclude template was added
if slices.Contains(added, linkedTemplate.ExcludeTemplate) {
continue
}
linkedTemplateItem, err := templates.ByName(templates.Meter, linkedTemplate.Template)
@ -116,13 +109,14 @@ func (c *CmdConfigure) configureLinkedTypes(templateItem templates.Template) {
fmt.Println("Error: " + err.Error())
return
}
if len(linkedTemplateItem.Params) == 0 || linkedTemplate.Usage == "" {
break
}
linkedTemplateItem.SetCombinedTitle()
category := DeviceCategory(linkedTemplate.Usage)
localizeMap := localizeMap{
"Linked": linkedTemplateItem.Title(),
"Article": DeviceCategories[category].article,
@ -136,8 +130,8 @@ func (c *CmdConfigure) configureLinkedTypes(templateItem templates.Template) {
}
for {
if added := c.configureLinkedTemplate(linkedTemplateItem, category); added {
deviceOfTemplateAdded[linkedTemplate.Template] = true
if c.configureLinkedTemplate(linkedTemplateItem, category) {
added = append(added, linkedTemplate.Template)
}
if !linkedTemplate.Multiple {