diff --git a/internal/agent/config/constants.go b/internal/agent/config/constants.go index 7a72aab2b..b4affffa5 100644 --- a/internal/agent/config/constants.go +++ b/internal/agent/config/constants.go @@ -21,4 +21,5 @@ const ( PrefMQTTServer = "MQTTServer" PrefMQTTTopic = "MQTTTopic" PrefMQTTUser = "MQTTUser" + PrefMQTTEnabled = "UseMQTT" ) diff --git a/internal/agent/ui/fyneUI.go b/internal/agent/ui/fyneUI.go index 4f9571423..3ade2fc27 100644 --- a/internal/agent/ui/fyneUI.go +++ b/internal/agent/ui/fyneUI.go @@ -296,10 +296,10 @@ func (ui *fyneUI) agentSettingsWindow(agent Agent, t *translations.Translator) f func (ui *fyneUI) serverConfigItems(ctx context.Context, agent Agent, t *translations.Translator) []*widget.FormItem { allServers := hass.FindServers(ctx) - tokenEntry := configEntry(config.PrefToken, "ASecretLongLivedToken", agent) + tokenEntry := configEntry(agent, config.PrefToken, "ASecretLongLivedToken") tokenEntry.Validator = validation.NewRegexp("[A-Za-z0-9_\\.]+", "Invalid token format") - serverEntry := configEntry(config.PrefHost, allServers[0], agent) + serverEntry := configEntry(agent, config.PrefHost, allServers[0]) serverEntry.Validator = httpValidator() serverEntry.Disable() @@ -332,14 +332,14 @@ func (ui *fyneUI) serverConfigItems(ctx context.Context, agent Agent, t *transla // mqttConfigItems generates a list of for item widgets for configuring the // agent to use an MQTT for pub/sub functionality func (ui *fyneUI) mqttConfigItems(agent Agent, t *translations.Translator) []*widget.FormItem { - serverEntry := configEntry(config.PrefMQTTServer, "localhost:1883", agent) + serverEntry := configEntry(agent, config.PrefMQTTServer, "localhost:1883") serverEntry.Validator = hostPortValidator() serverEntry.Disable() - topicEntry := configEntry(config.PrefMQTTTopic, "homeassistant", agent) + topicEntry := configEntry(agent, config.PrefMQTTTopic, "homeassistant") topicEntry.Disable() - mqttEnabled := widget.NewCheck("", func(b bool) { + mqttEnabled := configCheck(agent, config.PrefMQTTEnabled, func(b bool) { switch b { case true: serverEntry.Enable() @@ -368,7 +368,7 @@ func (ui *fyneUI) mqttConfigItems(agent Agent, t *translations.Translator) []*wi // configEntry creates a form entry widget that is tied to the given config // value of the given agent. When the value of the entry widget changes, the // corresponding config value will be updated. -func configEntry(name, placeholder string, agent Agent) *widget.Entry { +func configEntry(agent Agent, name, placeholder string) *widget.Entry { entry := widget.NewEntry() entry.OnChanged = func(s string) { if err := agent.SetConfig(name, s); err != nil { @@ -382,6 +382,18 @@ func configEntry(name, placeholder string, agent Agent) *widget.Entry { return entry } +// configCheck creates a form checkbox widget that is tied to the given config +// value of the given agent. When the value of the entry widget changes, the +// corresponding config value will be updated. +func configCheck(agent Agent, name string, checkFn func(bool)) *widget.Check { + entry := widget.NewCheck("", checkFn) + if err := agent.GetConfig(name, &entry.Checked); err != nil { + log.Warn().Err(err).Msgf("Could not get value of config entry %s. Using placeholder.", name) + entry.SetChecked(false) + } + return entry +} + func longestString(a []string) string { var l string if len(a) > 0 {