Tariff: qualify db device log names with the configured title (#32747)

This commit is contained in:
andig 2026-08-11 18:11:44 +02:00 • committed by GitHub
parent 3189e011f2
commit a142f3a759
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 4 deletions

View file

@ -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:<id> 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
}

22
cmd/setup_name_test.go Normal file
View file

@ -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))
}
}