Skip to content

Commit

Permalink
fix(preferences): 🐛 more preferences fixes after 7591c7a
Browse files Browse the repository at this point in the history
- make sure device section is set on new agent registration
- make sure a default MQTT topic is set if one is not provided is
  • Loading branch information
joshuar committed Jan 5, 2025
1 parent 66e28ec commit ee5f2d6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
7 changes: 5 additions & 2 deletions internal/agent/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package agent

import (
"context"
"errors"
"fmt"
"log/slog"

Expand Down Expand Up @@ -71,8 +72,10 @@ func setupMQTT(ctx context.Context) []MQTTWorker {
// Set up custom MQTT commands worker.
customCommandsWorker, err := commands.NewCommandsWorker(ctx, mqttDevice)
if err != nil {
logging.FromContext(ctx).Warn("Could not setup custom MQTT commands.",
slog.Any("error", err))
if !errors.Is(err, commands.ErrNoCommands) {
logging.FromContext(ctx).Warn("Could not setup custom MQTT commands.",
slog.Any("error", err))
}
} else {
workers = append(workers, customCommandsWorker)
}
Expand Down
8 changes: 8 additions & 0 deletions internal/hass/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ func RegisterDevice(ctx context.Context, registration *preferences.Registration)
registrationURL := registration.Server + RegistrationPath
thisDevice := device.NewDevice()

// Set device details in preferences.
if err := preferences.SetDevicePreferences(&preferences.Device{
Name: thisDevice.Name,
ID: thisDevice.ID,
}); err != nil {
return nil, fmt.Errorf("could not register device: %w", err)
}

// Register the device against the registration endpoint.
registrationStatus, err := api.Send[preferences.Hass](ctx, registrationURL, newRegistrationRequest(thisDevice, registration.Token))
if err != nil {
Expand Down
10 changes: 7 additions & 3 deletions internal/preferences/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ const (
prefMQTTServer = mqttPrefPrefix + ".server"
prefMQTTUser = mqttPrefPrefix + ".user"
prefMQTTPass = mqttPrefPrefix + ".password"
prefMQTTTopicPrefix = mqttPrefPrefix + ".topic_prefx"
prefMQTTTopicPrefix = mqttPrefPrefix + ".topic_prefix"
prefMQTTEnabled = mqttPrefPrefix + ".enabled"
)

// MQTT contains preferences related to MQTT functionality in Go Hass Agent.
type MQTT struct {
MQTTServer string `toml:"server,omitempty" validate:"omitempty,uri" kong:"required,help='MQTT server URI. Required.',placeholder='scheme://some.host:port'"` //nolint:lll
MQTTServer string `toml:"server,omitempty" validate:"required,uri" kong:"required,help='MQTT server URI. Required.',placeholder='scheme://some.host:port'"` //nolint:lll
MQTTUser string `toml:"user,omitempty" validate:"omitempty" kong:"optional,help='MQTT username.'"`
MQTTPassword string `toml:"password,omitempty" validate:"omitempty" kong:"optional,help='MQTT password.'"`
MQTTTopicPrefix string `toml:"topic_prefix,omitempty" validate:"omitempty,ascii" kong:"optional,help='MQTT topic prefix.'"`
MQTTTopicPrefix string `toml:"topic_prefix,omitempty" validate:"required,ascii" kong:"optional,help='MQTT topic prefix.'"`
MQTTEnabled bool `toml:"enabled" validate:"boolean" kong:"-"`
}

Expand All @@ -48,6 +48,10 @@ func SetMQTTPreferences(prefs *MQTT) error {
return fmt.Errorf("%w: %w", ErrSetMQTTPreference, err)
}

if prefs.MQTTTopicPrefix == "" {
prefs.MQTTTopicPrefix = MQTTTopicPrefix
}

if err := prefsSrc.Set(prefMQTTTopicPrefix, prefs.MQTTTopicPrefix); err != nil {
return fmt.Errorf("%w: %w", ErrSetMQTTPreference, err)
}
Expand Down
23 changes: 17 additions & 6 deletions internal/preferences/prefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"sync"

"github.com/adrg/xdg"
"github.com/davecgh/go-spew/spew"
"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
Expand Down Expand Up @@ -60,7 +59,8 @@ var defaultAgentPreferences = &preferences{
Version: AppVersion(),
Registered: false,
MQTT: &MQTT{
MQTTEnabled: false,
MQTTEnabled: false,
MQTTTopicPrefix: MQTTTopicPrefix,
},
Registration: &Registration{
Server: DefaultServer,
Expand Down Expand Up @@ -147,13 +147,24 @@ func validate() error {
return fmt.Errorf("%w: %w", ErrLoadPreferences, err)
}

// Validate current preferences.
err := validation.Validate.Struct(currentPreferences)
if err != nil {
spew.Dump(err)
// Ensure device preferences are valid.
if err := validation.Validate.Struct(currentPreferences.Device); err != nil {
return fmt.Errorf("%w: %s", ErrValidatePreferences, validation.ParseValidationErrors(err))
}

// Ensure hass preferences are valid.
if err := validation.Validate.Struct(currentPreferences.Hass); err != nil {
return fmt.Errorf("%w: %s", ErrValidatePreferences, validation.ParseValidationErrors(err))
}

if currentPreferences.IsMQTTEnabled() {
// Validate MQTT preferences are valid.
err := validation.Validate.Struct(currentPreferences.MQTT)
if err != nil {
return fmt.Errorf("%w: %s", ErrValidatePreferences, validation.ParseValidationErrors(err))
}
}

slog.Debug("Preferences are valid.")

return nil
Expand Down

0 comments on commit ee5f2d6

Please sign in to comment.