Repository: evcc-io/evcc
Pull request #13804 (2024-05-07 19:45:26)

Custom vehicle: lower case parameter identical to plugin
Refs https://github.com/evcc-io/evcc/discussions/13798#discussioncomment-9346296

/cc @Adminius


Patch:
diff --git a/main.go b/main.go
index 60f47f19..49005972 100644
--- a/main.go
+++ b/main.go
@@ -12,10 +12,10 @@ import (
 )
 
 var (
-	// go:embed dist
+	//go:embed dist
 	web embed.FS
 
-	// go:embed i18n/*.toml
+	//go:embed i18n/*.toml
 	i18n embed.FS
 )
 
diff --git a/templates/definition/vehicle/teslalogger.yaml b/templates/definition/vehicle/teslalogger.yaml
index f1e65b89..691f0615 100644
--- a/templates/definition/vehicle/teslalogger.yaml
+++ b/templates/definition/vehicle/teslalogger.yaml
@@ -78,7 +78,7 @@ render: |
     uri: {{ .url }}:{{ .port }}/command/{{ .id }}/wake_up
   chargeEnable:
     source: http
-    uri: {{ .url }}:{{ .port }}/command/{{ .id }}/charge_start_stop?${enable}
+    uri: {{ .url }}:{{ .port }}/command/{{ .id }}/charge_start_stop?${chargeenable}
   maxcurrent:
     source: http
     uri: {{ .url }}:{{ .port }}/command/{{ .id }}/set_charging_amps?${maxcurrent}
diff --git a/util/format.go b/util/format.go
index a92dfa90..8f1cb5dc 100644
--- a/util/format.go
+++ b/util/format.go
@@ -11,7 +11,7 @@ import (
 	"github.com/42atomys/sprout"
 )
 
-var re = regexp.MustCompile(`\${(\w+)(:([a-zA-Z0-9%.]+))?}`)
+var re = regexp.MustCompile(`(?i)\${(\w+)(:([a-zA-Z0-9%.]+))?}`)
 
 // Truish returns true if value is truish (true/1/on)
 func Truish(s string) bool {
@@ -73,7 +73,7 @@ func ReplaceFormatted(s string, kv map[string]interface{}) (string, error) {
 		match, key, format := m[0], m[1], m[3]
 
 		// find key and replacement value
-		val, ok := kv[key]
+		val, ok := kv[strings.ToLower(key)]
 		if !ok {
 			wanted = append(wanted, key)
 			format = "%s"
diff --git a/vehicle/vehicle.go b/vehicle/vehicle.go
index 6c56fd40..4f7a2d85 100644
--- a/vehicle/vehicle.go
+++ b/vehicle/vehicle.go
@@ -107,7 +107,7 @@ func NewConfigurableFromConfig(other map[string]interface{}) (api.Vehicle, error
 	// decorate maxCurrent
 	var maxCurrent func(int64) error
 	if cc.MaxCurrent != nil {
-		maxCurrent, err = provider.NewIntSetterFromConfig("maxCurrent", *cc.MaxCurrent)
+		maxCurrent, err = provider.NewIntSetterFromConfig("maxcurrent", *cc.MaxCurrent)
 		if err != nil {
 			return nil, fmt.Errorf("maxCurrent: %w", err)
 		}
@@ -153,7 +153,7 @@ func NewConfigurableFromConfig(other map[string]interface{}) (api.Vehicle, error
 	// decorate chargeEnable
 	var chargeEnable func(bool) error
 	if cc.ChargeEnable != nil {
-		chargeEnable, err = provider.NewBoolSetterFromConfig("chargeEnable", *cc.ChargeEnable)
+		chargeEnable, err = provider.NewBoolSetterFromConfig("chargeenable", *cc.ChargeEnable)
 		if err != nil {
 			return nil, fmt.Errorf("chargeEnable: %w", err)
 		}


Test patch:
diff --git a/util/format_test.go b/util/format_test.go
index 7cc23bcd..da21fdcd 100644
--- a/util/format_test.go
+++ b/util/format_test.go
@@ -4,6 +4,9 @@ import (
 	"math"
 	"testing"
 	"time"
+
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
 )
 
 func TestTruish(t *testing.T) {
@@ -21,10 +24,7 @@ func TestTruish(t *testing.T) {
 	}
 
 	for _, c := range cases {
-		b := Truish(c.k)
-		if b != c.v {
-			t.Errorf("expected %v got %v", c.v, b)
-		}
+		assert.Equal(t, c.v, Truish(c.k))
 	}
 }
 
@@ -36,6 +36,7 @@ func TestReplace(t *testing.T) {
 	}{
 		// regex tests
 		{"foo", true, "${foo}", "true"},
+		{"foo", true, "${Foo}", "true"},
 		{"foo", "1", "abc${foo}${foo}", "abc11"},
 		{"foo", math.Pi, "${foo:%.2f}", "3.14"},
 		{"foo", math.Pi, "${foo:%.0f}%", "3%"},
@@ -47,9 +48,8 @@ func TestReplace(t *testing.T) {
 			c.k: c.v,
 		})
 
-		if s != c.expected || err != nil {
-			t.Error(s, err)
-		}
+		require.NoError(t, err)
+		assert.Equal(t, c.expected, s)
 	}
 }