From 0d18d6703f6fd00766131c05d34950f928c3c833 Mon Sep 17 00:00:00 2001 From: andig Date: Wed, 23 Oct 2024 19:31:53 +0200 Subject: [PATCH] Mqtt: set will only when running as service (#16812) --- cmd/root.go | 4 ++++ cmd/setup.go | 8 +++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 4d96e9491a..82d74917cc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -43,6 +43,8 @@ var ( ignoreMqtt = []string{"log", "auth", "releaseNotes"} // excessive size may crash certain brokers viper *vpr.Viper + + runAsService bool ) // rootCmd represents the base command when called without any subcommands @@ -111,6 +113,8 @@ func Execute() { } func runRoot(cmd *cobra.Command, args []string) { + runAsService = true + // load config and re-configure logging after reading config file var err error if cfgErr := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed); errors.As(cfgErr, &vpr.ConfigFileNotFoundError{}) { diff --git a/cmd/setup.go b/cmd/setup.go index 7642c905d4..0827489e6f 100644 --- a/cmd/setup.go +++ b/cmd/setup.go @@ -465,9 +465,7 @@ func configureEnvironment(cmd *cobra.Command, conf *globalconfig.All) (err error } // setup persistence - if err == nil { - err = wrapErrorWithClass(ClassDatabase, configureDatabase(conf.Database)) - } + err = wrapErrorWithClass(ClassDatabase, configureDatabase(conf.Database)) // setup translations if err == nil { @@ -605,6 +603,10 @@ func configureMqtt(conf *globalconfig.Mqtt) error { log := util.NewLogger("mqtt") instance, err := mqtt.RegisteredClient(log, conf.Broker, conf.User, conf.Password, conf.ClientID, 1, conf.Insecure, conf.CaCert, conf.ClientCert, conf.ClientKey, func(options *paho.ClientOptions) { + if !runAsService { + return + } + topic := fmt.Sprintf("%s/status", strings.Trim(conf.Topic, "/")) options.SetWill(topic, "offline", 1, true)