Skip to content

Commit

Permalink
feat(agent/ui): add a configCheck function for bool config items
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Sep 20, 2023
1 parent 98d0cf2 commit 134c876
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions internal/agent/config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ const (
PrefMQTTServer = "MQTTServer"
PrefMQTTTopic = "MQTTTopic"
PrefMQTTUser = "MQTTUser"
PrefMQTTEnabled = "UseMQTT"
)
24 changes: 18 additions & 6 deletions internal/agent/ui/fyneUI.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 134c876

Please sign in to comment.