Add configurable device aliases

This commit is contained in:
2026-03-14 22:42:27 +01:00
parent b52ad9caee
commit 110388c363
8 changed files with 259 additions and 17 deletions
+52 -3
View File
@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"time"
@@ -12,10 +13,13 @@ import (
const envPrefix = "MQTT_SCRUBBER_"
var invalidDeviceCharacters = regexp.MustCompile(`[^a-z0-9_]+`)
type Config struct {
MQTT MQTTConfig `json:"mqtt"`
Influx InfluxConfig `json:"influx"`
App AppConfig `json:"app"`
MQTT MQTTConfig `json:"mqtt"`
Influx InfluxConfig `json:"influx"`
App AppConfig `json:"app"`
DeviceAliases map[string]string `json:"device_aliases"`
}
type MQTTConfig struct {
@@ -80,6 +84,8 @@ func Load(path string) (Config, error) {
return Config{}, err
}
cfg.DeviceAliases = normalizeDeviceAliases(cfg.DeviceAliases)
if err := cfg.Validate(); err != nil {
return Config{}, err
}
@@ -161,6 +167,18 @@ func applyEnvOverrides(cfg *Config) error {
setString(&cfg.App.LogLevel, envPrefix+"APP_LOG_LEVEL")
setString(&cfg.App.HealthAddress, envPrefix+"APP_HEALTH_ADDRESS")
if raw, ok := os.LookupEnv(envPrefix + "DEVICE_ALIASES"); ok {
if strings.TrimSpace(raw) == "" {
cfg.DeviceAliases = nil
} else {
aliases := make(map[string]string)
if err := json.Unmarshal([]byte(raw), &aliases); err != nil {
return fmt.Errorf("parse %sDEVICE_ALIASES: %w", envPrefix, err)
}
cfg.DeviceAliases = aliases
}
}
if raw, ok := os.LookupEnv(envPrefix + "MQTT_TOPICS"); ok {
cfg.MQTT.Topics = splitAndTrim(raw)
}
@@ -227,3 +245,34 @@ func splitAndTrim(value string) []string {
return result
}
func normalizeDeviceAliases(aliases map[string]string) map[string]string {
if len(aliases) == 0 {
return nil
}
normalized := make(map[string]string, len(aliases))
for device, alias := range aliases {
cleanDevice := normalizeDeviceKey(device)
cleanAlias := strings.TrimSpace(alias)
if cleanDevice == "" || cleanAlias == "" {
continue
}
normalized[cleanDevice] = cleanAlias
}
if len(normalized) == 0 {
return nil
}
return normalized
}
func normalizeDeviceKey(value string) string {
normalized := strings.ToLower(strings.TrimSpace(value))
normalized = strings.ReplaceAll(normalized, "-", "_")
normalized = strings.ReplaceAll(normalized, " ", "_")
normalized = invalidDeviceCharacters.ReplaceAllString(normalized, "_")
normalized = strings.Trim(normalized, "_")
return normalized
}
+107
View File
@@ -0,0 +1,107 @@
package config
import (
"os"
"path/filepath"
"testing"
)
func TestLoadNormalizesDeviceAliases(t *testing.T) {
t.Setenv("MQTT_SCRUBBER_DEVICE_ALIASES", "")
configPath := filepath.Join(t.TempDir(), "config.json")
contents := `{
"mqtt": {
"broker": "tcp://127.0.0.1:1883",
"client_id": "mqqt-scrubber",
"topics": ["tele/+/STATE"],
"qos": 0
},
"influx": {
"url": "http://127.0.0.1:8181",
"database": "home",
"precision": "ns"
},
"app": {
"batch_size": 200,
"buffer_size": 1000,
"flush_interval": "10s",
"flush_timeout": "10s",
"log_level": "info",
"health_address": ":8080"
},
"device_aliases": {
"Kitchen-Plug": "Kitchen Plug",
" Patio Sensor ": "Patio Sensor",
"unused": " "
}
}`
if err := os.WriteFile(configPath, []byte(contents), 0o644); err != nil {
t.Fatalf("write config file: %v", err)
}
cfg, err := Load(configPath)
if err != nil {
t.Fatalf("Load returned error: %v", err)
}
if got := cfg.DeviceAliases["kitchen_plug"]; got != "Kitchen Plug" {
t.Fatalf("unexpected kitchen alias: got %q", got)
}
if got := cfg.DeviceAliases["patio_sensor"]; got != "Patio Sensor" {
t.Fatalf("unexpected patio alias: got %q", got)
}
if _, exists := cfg.DeviceAliases["unused"]; exists {
t.Fatal("expected blank aliases to be discarded")
}
}
func TestLoadOverridesDeviceAliasesFromEnv(t *testing.T) {
t.Setenv("MQTT_SCRUBBER_DEVICE_ALIASES", `{"Desk-Plug":"Desk Plug"}`)
configPath := filepath.Join(t.TempDir(), "config.json")
contents := `{
"mqtt": {
"broker": "tcp://127.0.0.1:1883",
"client_id": "mqqt-scrubber",
"topics": ["tele/+/STATE"],
"qos": 0
},
"influx": {
"url": "http://127.0.0.1:8181",
"database": "home",
"precision": "ns"
},
"app": {
"batch_size": 200,
"buffer_size": 1000,
"flush_interval": "10s",
"flush_timeout": "10s",
"log_level": "info",
"health_address": ":8080"
},
"device_aliases": {
"Kitchen-Plug": "Kitchen Plug"
}
}`
if err := os.WriteFile(configPath, []byte(contents), 0o644); err != nil {
t.Fatalf("write config file: %v", err)
}
cfg, err := Load(configPath)
if err != nil {
t.Fatalf("Load returned error: %v", err)
}
if len(cfg.DeviceAliases) != 1 {
t.Fatalf("expected env aliases to replace file aliases, got %d entries", len(cfg.DeviceAliases))
}
if got := cfg.DeviceAliases["desk_plug"]; got != "Desk Plug" {
t.Fatalf("unexpected desk alias: got %q", got)
}
}