diff --git a/internal/preferences/device.go b/internal/preferences/device.go index 9e6539d3b..33b52d1b7 100644 --- a/internal/preferences/device.go +++ b/internal/preferences/device.go @@ -10,6 +10,12 @@ import ( "fmt" ) +const ( + devicePrefPrefix = "device" + prefDeviceID = devicePrefPrefix + ".id" + prefDeviceName = devicePrefPrefix + ".name" +) + // Device contains the device-specific preferences. type Device struct { ID string `toml:"id" validate:"required,ascii"` @@ -20,23 +26,23 @@ var ErrSetDevicePreference = errors.New("could not set device preference") // SetDevicePreferences sets the device preferences. func SetDevicePreferences(device *Device) error { - if err := prefsSrc.Set("device.id", device.ID); err != nil { + if err := prefsSrc.Set(prefDeviceID, device.ID); err != nil { return fmt.Errorf("%w: %w", ErrSetDevicePreference, err) } - if err := prefsSrc.Set("device.name", device.Name); err != nil { + if err := prefsSrc.Set(prefDeviceName, device.Name); err != nil { return fmt.Errorf("%w: %w", ErrSetDevicePreference, err) } return nil } -// DeviceName retrieves the device name from the preferences. -func DeviceName() string { - return prefsSrc.String("device.name") -} - // DeviceID retrieves the device ID from the preferences. func DeviceID() string { - return prefsSrc.String("device.id") + return prefsSrc.String(prefDeviceID) +} + +// DeviceName retrieves the device name from the preferences. +func DeviceName() string { + return prefsSrc.String(prefDeviceName) } diff --git a/internal/preferences/hass.go b/internal/preferences/hass.go index 7ee95c851..2bdad6a76 100644 --- a/internal/preferences/hass.go +++ b/internal/preferences/hass.go @@ -15,6 +15,17 @@ import ( const ( WebsocketPath = "/api/websocket" WebHookPath = "/api/webhook/" + + hassPrefPrefix = "hass" + prefHassSecret = hassPrefPrefix + ".secret" + prefHassAPIURL = hassPrefPrefix + ".apiurl" + prefHassWebsocketURL = hassPrefPrefix + ".websocketurl" + prefHassWebhookID = hassPrefPrefix + ".webhook_id" + prefHassCloudhookURL = hassPrefPrefix + ".cloudhook_url" + prefHassRemoteUIURL = hassPrefPrefix + ".remote_ui_url" + regPrefPrefix = "registration" + prefHassRegToken = regPrefPrefix + ".token" + prefHassRegServer = regPrefPrefix + ".server" ) // Hass contains preferences related to connectivity to Home Assistant. @@ -36,7 +47,7 @@ var ( // //revive:disable:indent-error-flow func SetHassPreferences(hassPrefs *Hass, regPrefs *Registration) error { - if err := prefsSrc.Set("hass.secret", hassPrefs.Secret); err != nil { + if err := prefsSrc.Set(prefHassSecret, hassPrefs.Secret); err != nil { return fmt.Errorf("%w: %w", ErrSetHassPreference, err) } @@ -44,7 +55,7 @@ func SetHassPreferences(hassPrefs *Hass, regPrefs *Registration) error { if apiURL, err := generateAPIURL(hassPrefs, regPrefs); err != nil { return fmt.Errorf("%w: %w", ErrSaveHassPreferences, err) } else { - if err := prefsSrc.Set("hass.apiurl", apiURL); err != nil { + if err := prefsSrc.Set(prefHassAPIURL, apiURL); err != nil { return fmt.Errorf("%w: %w", ErrSetHassPreference, err) } } @@ -53,23 +64,23 @@ func SetHassPreferences(hassPrefs *Hass, regPrefs *Registration) error { if websocketURL, err := generateWebsocketURL(regPrefs); err != nil { return fmt.Errorf("%w: %w", ErrSaveHassPreferences, err) } else { - if err := prefsSrc.Set("hass.websocketurl", websocketURL); err != nil { + if err := prefsSrc.Set(prefHassAPIURL, websocketURL); err != nil { return fmt.Errorf("%w: %w", ErrSetHassPreference, err) } } // Set the webhookID if present. if hassPrefs.WebhookID != "" { - if err := prefsSrc.Set("hass.webhook_id", hassPrefs.WebhookID); err != nil { + if err := prefsSrc.Set(prefHassWebhookID, hassPrefs.WebhookID); err != nil { return fmt.Errorf("%w: %w", ErrSetHassPreference, err) } } - if err := prefsSrc.Set("registration.server", regPrefs.Server); err != nil { + if err := prefsSrc.Set(prefHassRegServer, regPrefs.Server); err != nil { return fmt.Errorf("%w: %w", ErrSetHassPreference, err) } - if err := prefsSrc.Set("registration.token", regPrefs.Token); err != nil { + if err := prefsSrc.Set(prefHassRegToken, regPrefs.Token); err != nil { return fmt.Errorf("%w: %w", ErrSetHassPreference, err) } @@ -79,37 +90,37 @@ func SetHassPreferences(hassPrefs *Hass, regPrefs *Registration) error { // RestAPIURL retrieves the configured Home Assistant Rest API URL from the // preferences. func RestAPIURL() string { - return prefsSrc.String("hass.apiurl") + return prefsSrc.String(prefHassAPIURL) } // RestAPIURL retrieves the configured Home Assistant websocket API URL from the // preferences. func WebsocketURL() string { - return prefsSrc.String("hass.websocketurl") + return prefsSrc.String(prefHassWebsocketURL) } // WebhookID retrieves the Go Hass Agent Webhook ID from the // preferences. func WebhookID() string { - return prefsSrc.String("hass.webhook_id") + return prefsSrc.String(prefHassWebhookID) } // Token retrieves the Go Hass Agent long-lived access token from the // preferences. func Token() string { - return prefsSrc.String("registration.token") + return prefsSrc.String(prefHassRegToken) } func generateAPIURL(hassPrefs *Hass, regPrefs *Registration) (string, error) { switch { case hassPrefs.CloudhookURL != "" && regPrefs.IgnoreHassURLs: - if err := prefsSrc.Set("hass.cloudhook_url", hassPrefs.CloudhookURL); err != nil { + if err := prefsSrc.Set(prefHassCloudhookURL, hassPrefs.CloudhookURL); err != nil { return "", fmt.Errorf("%w: %w", ErrSetHassPreference, err) } return hassPrefs.CloudhookURL, nil case hassPrefs.RemoteUIURL != "" && hassPrefs.WebhookID != "" && !regPrefs.IgnoreHassURLs: - if err := prefsSrc.Set("hass.remote_ui_url", hassPrefs.CloudhookURL); err != nil { + if err := prefsSrc.Set(prefHassRemoteUIURL, hassPrefs.CloudhookURL); err != nil { return "", fmt.Errorf("%w: %w", ErrSetHassPreference, err) } diff --git a/internal/preferences/mqtt.go b/internal/preferences/mqtt.go index 25195bc27..d16b493c6 100644 --- a/internal/preferences/mqtt.go +++ b/internal/preferences/mqtt.go @@ -13,6 +13,15 @@ import ( "github.com/knadh/koanf/v2" ) +const ( + mqttPrefPrefix = "mqtt" + prefMQTTServer = mqttPrefPrefix + ".server" + prefMQTTUser = mqttPrefPrefix + ".user" + prefMQTTPass = mqttPrefPrefix + ".password" + prefMQTTTopicPrefix = mqttPrefPrefix + ".topic_prefx" + 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 @@ -27,23 +36,23 @@ var ErrSetMQTTPreference = errors.New("could not set MQTT preference") // SetMQTTPreferences will set Go Hass Agent's MQTT preferences to the given // values. func SetMQTTPreferences(prefs *MQTT) error { - if err := prefsSrc.Set("mqtt.server", prefs.MQTTServer); err != nil { + if err := prefsSrc.Set(prefMQTTServer, prefs.MQTTServer); err != nil { return fmt.Errorf("%w: %w", ErrSetMQTTPreference, err) } - if err := prefsSrc.Set("mqtt.user", prefs.MQTTUser); err != nil { + if err := prefsSrc.Set(prefMQTTUser, prefs.MQTTUser); err != nil { return fmt.Errorf("%w: %w", ErrSetMQTTPreference, err) } - if err := prefsSrc.Set("mqtt.password", prefs.MQTTPassword); err != nil { + if err := prefsSrc.Set(prefMQTTPass, prefs.MQTTPassword); err != nil { return fmt.Errorf("%w: %w", ErrSetMQTTPreference, err) } - if err := prefsSrc.Set("mqtt.topic_prefix", prefs.MQTTTopicPrefix); err != nil { + if err := prefsSrc.Set(prefMQTTTopicPrefix, prefs.MQTTTopicPrefix); err != nil { return fmt.Errorf("%w: %w", ErrSetMQTTPreference, err) } - if err := prefsSrc.Set("mqtt.enabled", prefs.MQTTEnabled); err != nil { + if err := prefsSrc.Set(prefMQTTEnabled, prefs.MQTTEnabled); err != nil { return fmt.Errorf("%w: %w", ErrSetMQTTPreference, err) } @@ -54,7 +63,7 @@ func SetMQTTPreferences(prefs *MQTT) error { func GetMQTTPreferences() (*MQTT, error) { var mqttPrefs MQTT // Unmarshal config, overwriting defaults. - if err := prefsSrc.UnmarshalWithConf("mqtt", &mqttPrefs, koanf.UnmarshalConf{Tag: "toml"}); err != nil { + if err := prefsSrc.UnmarshalWithConf(mqttPrefPrefix, &mqttPrefs, koanf.UnmarshalConf{Tag: "toml"}); err != nil { return nil, fmt.Errorf("%w: %w", ErrLoadPreferences, err) } @@ -63,7 +72,7 @@ func GetMQTTPreferences() (*MQTT, error) { // MQTTEnabled will return whether Go Hass Agent will use MQTT. func MQTTEnabled() bool { - return prefsSrc.Bool("mqtt.enabled") + return prefsSrc.Bool(prefMQTTEnabled) } // Server returns the broker URI from the preferences. @@ -85,10 +94,6 @@ func (p *MQTT) Password() string { // TopicPrefix returns the prefix for topics on MQTT. func (p *MQTT) TopicPrefix() string { - if p.MQTTTopicPrefix == "" { - return MQTTTopicPrefix - } - return p.MQTTTopicPrefix } diff --git a/internal/preferences/prefs.go b/internal/preferences/prefs.go index 66c163a92..e70128f33 100644 --- a/internal/preferences/prefs.go +++ b/internal/preferences/prefs.go @@ -37,6 +37,8 @@ const ( DefaultServer = "http://localhost:8123" DefaultSecret = "ALongSecretString" + + prefRegistered = "registered" ) var ( @@ -191,7 +193,7 @@ func Save(ctx context.Context) error { // SetRegistered sets whether Go Hass Agent has been registered with Home // Assistant. func SetRegistered(value bool) error { - if err := prefsSrc.Set("registration", value); err != nil { + if err := prefsSrc.Set(prefRegistered, value); err != nil { return fmt.Errorf("%w: %w", ErrSetPreference, err) } @@ -200,7 +202,7 @@ func SetRegistered(value bool) error { // Registered returns the registration status of Go Hass Agent. func Registered() bool { - return prefsSrc.Bool("registered") + return prefsSrc.Bool(prefRegistered) } // checkPath checks that the given directory exists. If it doesn't it will be