From a142f3a759f39033c2fd4d45e2a6f5799b3ed0d2 Mon Sep 17 00:00:00 2001 From: andig Date: Tue, 11 Aug 2026 18:11:44 +0200 Subject: [PATCH] Tariff: qualify db device log names with the configured title (#32747) --- cmd/setup.go | 14 ++++++++++---- cmd/setup_name_test.go | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 cmd/setup_name_test.go diff --git a/cmd/setup.go b/cmd/setup.go index 6a77f1c24..69a0ffaab 100644 --- a/cmd/setup.go +++ b/cmd/setup.go @@ -316,13 +316,19 @@ func staticInstance[T any](typ string, cc config.Named, newFromConf newFromConfF return err } -// loggerForConfig creates a logger with sensible name for (custom) configurable device -func loggerForConfig(conf *config.Config) *util.Logger { +// nameForConfig returns a sensible log name for a (custom) configurable device, +// qualifying the generic db: with the configured title where available +func nameForConfig(conf *config.Config) string { res := conf.Named().Name if t := conf.Title; t != "" && t != res { res += "-" + t } - return util.NewLogger(res) + return res +} + +// loggerForConfig creates a logger with sensible name for (custom) configurable device +func loggerForConfig(conf *config.Config) *util.Logger { + return util.NewLogger(nameForConfig(conf)) } func configurableInstance[T any](typ string, conf *config.Config, newFromConf newFromConfFunc[T], h config.Handler[T]) error { @@ -1212,7 +1218,7 @@ func configureTariffs(conf *globalconfig.Tariffs, names ...string) (*tariff.Tari var instance api.Tariff if !conf.Disable { var err error - instance, err = tariffInstance(cc.Name, config.Typed{Type: cc.Type, Other: cc.Other}) + instance, err = tariffInstance(nameForConfig(&conf), config.Typed{Type: cc.Type, Other: cc.Other}) if err != nil { return err } diff --git a/cmd/setup_name_test.go b/cmd/setup_name_test.go new file mode 100644 index 000000000..766b8f780 --- /dev/null +++ b/cmd/setup_name_test.go @@ -0,0 +1,22 @@ +package cmd + +import ( + "testing" + + "github.com/evcc-io/evcc/util/config" + "github.com/stretchr/testify/assert" +) + +func TestNameForConfig(t *testing.T) { + for _, tc := range []struct { + title string + expected string + }{ + {"", "db:8"}, + {"Solar Forecast 1. Dachseite", "db:8-Solar Forecast 1. Dachseite"}, + {"db:8", "db:8"}, // title equal to the generated name is not repeated + } { + conf := config.Config{ID: 8, Properties: config.Properties{Title: tc.title}} + assert.Equal(t, tc.expected, nameForConfig(&conf)) + } +}