Skip to content

Commit

Permalink
Mqtt: set will only when running as service (evcc-io#16812)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Oct 23, 2024
1 parent 40c3d67 commit 0d18d67
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{}) {
Expand Down
8 changes: 5 additions & 3 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 0d18d67

Please sign in to comment.