Add Tasmota timezone handling and dashboard updates
This commit is contained in:
+44
-12
@@ -39,12 +39,13 @@ type InfluxConfig struct {
|
||||
}
|
||||
|
||||
type AppConfig struct {
|
||||
BatchSize int `json:"batch_size"`
|
||||
BufferSize int `json:"buffer_size"`
|
||||
FlushInterval DurationValue `json:"flush_interval"`
|
||||
FlushTimeout DurationValue `json:"flush_timeout"`
|
||||
LogLevel string `json:"log_level"`
|
||||
HealthAddress string `json:"health_address"`
|
||||
BatchSize int `json:"batch_size"`
|
||||
BufferSize int `json:"buffer_size"`
|
||||
FlushInterval DurationValue `json:"flush_interval"`
|
||||
FlushTimeout DurationValue `json:"flush_timeout"`
|
||||
LogLevel string `json:"log_level"`
|
||||
HealthAddress string `json:"health_address"`
|
||||
TasmotaTimeZone string `json:"tasmota_time_zone"`
|
||||
}
|
||||
|
||||
type DurationValue struct {
|
||||
@@ -124,6 +125,9 @@ func (cfg Config) Validate() error {
|
||||
if cfg.App.FlushTimeout.Duration <= 0 {
|
||||
return errors.New("app flush_timeout must be greater than zero")
|
||||
}
|
||||
if _, err := loadConfiguredLocation(cfg.App.TasmotaTimeZone); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -145,12 +149,13 @@ func defaultConfig() Config {
|
||||
Precision: "ns",
|
||||
},
|
||||
App: AppConfig{
|
||||
BatchSize: 200,
|
||||
BufferSize: 1000,
|
||||
FlushInterval: DurationValue{Duration: 10 * time.Second},
|
||||
FlushTimeout: DurationValue{Duration: 10 * time.Second},
|
||||
LogLevel: "info",
|
||||
HealthAddress: ":8080",
|
||||
BatchSize: 200,
|
||||
BufferSize: 1000,
|
||||
FlushInterval: DurationValue{Duration: 10 * time.Second},
|
||||
FlushTimeout: DurationValue{Duration: 10 * time.Second},
|
||||
LogLevel: "info",
|
||||
HealthAddress: ":8080",
|
||||
TasmotaTimeZone: "UTC",
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -166,6 +171,7 @@ func applyEnvOverrides(cfg *Config) error {
|
||||
setString(&cfg.Influx.Precision, envPrefix+"INFLUX_PRECISION")
|
||||
setString(&cfg.App.LogLevel, envPrefix+"APP_LOG_LEVEL")
|
||||
setString(&cfg.App.HealthAddress, envPrefix+"APP_HEALTH_ADDRESS")
|
||||
setString(&cfg.App.TasmotaTimeZone, envPrefix+"APP_TASMOTA_TIME_ZONE")
|
||||
|
||||
if raw, ok := os.LookupEnv(envPrefix + "DEVICE_ALIASES"); ok {
|
||||
if strings.TrimSpace(raw) == "" {
|
||||
@@ -276,3 +282,29 @@ func normalizeDeviceKey(value string) string {
|
||||
normalized = strings.Trim(normalized, "_")
|
||||
return normalized
|
||||
}
|
||||
|
||||
func (cfg AppConfig) TasmotaLocation() *time.Location {
|
||||
location, err := loadConfiguredLocation(cfg.TasmotaTimeZone)
|
||||
if err != nil {
|
||||
return time.UTC
|
||||
}
|
||||
|
||||
return location
|
||||
}
|
||||
|
||||
func loadConfiguredLocation(name string) (*time.Location, error) {
|
||||
trimmed := strings.TrimSpace(name)
|
||||
if trimmed == "" || strings.EqualFold(trimmed, "utc") {
|
||||
return time.UTC, nil
|
||||
}
|
||||
if strings.EqualFold(trimmed, "local") {
|
||||
return time.Local, nil
|
||||
}
|
||||
|
||||
location, err := time.LoadLocation(trimmed)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("app tasmota_time_zone %q is invalid: %w", trimmed, err)
|
||||
}
|
||||
|
||||
return location, nil
|
||||
}
|
||||
|
||||
@@ -7,8 +7,6 @@ import (
|
||||
)
|
||||
|
||||
func TestLoadNormalizesDeviceAliases(t *testing.T) {
|
||||
t.Setenv("MQTT_SCRUBBER_DEVICE_ALIASES", "")
|
||||
|
||||
configPath := filepath.Join(t.TempDir(), "config.json")
|
||||
contents := `{
|
||||
"mqtt": {
|
||||
@@ -105,3 +103,76 @@ func TestLoadOverridesDeviceAliasesFromEnv(t *testing.T) {
|
||||
t.Fatalf("unexpected desk alias: got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadSupportsTasmotaTimeZone(t *testing.T) {
|
||||
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",
|
||||
"tasmota_time_zone": "Europe/Prague"
|
||||
}
|
||||
}`
|
||||
|
||||
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.App.TasmotaLocation().String(); got != "Europe/Prague" {
|
||||
t.Fatalf("unexpected tasmota timezone: got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadRejectsInvalidTasmotaTimeZone(t *testing.T) {
|
||||
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",
|
||||
"tasmota_time_zone": "Not/AZone"
|
||||
}
|
||||
}`
|
||||
|
||||
if err := os.WriteFile(configPath, []byte(contents), 0o644); err != nil {
|
||||
t.Fatalf("write config file: %v", err)
|
||||
}
|
||||
|
||||
if _, err := Load(configPath); err == nil {
|
||||
t.Fatal("expected Load to reject invalid tasmota_time_zone")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user