Skip to content

Commit

Permalink
fix: 🐛 code cleanup missed in 7591c7a
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Dec 8, 2024
1 parent 154fa96 commit f6dca52
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 54 deletions.
5 changes: 0 additions & 5 deletions internal/agent/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ import (

var ErrUserCancelledRegistration = errors.New("user canceled registration")

type registrationPrefs interface {
AgentRegistered() bool
SaveHassPreferences(ctx context.Context, details *preferences.Hass, options *preferences.Registration) error
}

func checkRegistration(ctx context.Context, agentUI ui) error {
if preferences.Registered() && !ForceRegister(ctx) {
return nil
Expand Down
8 changes: 4 additions & 4 deletions internal/hass/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func (r *registrationRequest) RequestBody() any {
return r.Device
}

func newRegistrationRequest(device *device.Device, token string) *registrationRequest {
func newRegistrationRequest(thisDevice *device.Device, token string) *registrationRequest {
return &registrationRequest{
Device: device,
Device: thisDevice,
Token: token,
}
}
Expand All @@ -46,10 +46,10 @@ func RegisterDevice(ctx context.Context, registration *preferences.Registration)
}

registrationURL := registration.Server + RegistrationPath
device := device.NewDevice(preferences.AppIDFromContext(ctx))
thisDevice := device.NewDevice(preferences.AppIDFromContext(ctx))

// Register the device against the registration endpoint.
registrationStatus, err := api.Send[preferences.Hass](ctx, registrationURL, newRegistrationRequest(device, registration.Token))
registrationStatus, err := api.Send[preferences.Hass](ctx, registrationURL, newRegistrationRequest(thisDevice, registration.Token))
if err != nil {
return nil, fmt.Errorf("could not register device: %w", err)
}
Expand Down
36 changes: 14 additions & 22 deletions internal/preferences/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,27 @@

package preferences

import (
"errors"
"fmt"
)

type Device struct {
ID string `toml:"id" validate:"required,ascii"`
Name string `toml:"name" validate:"required,ascii"`
}

var ErrSetDevicePreference = errors.New("could not set device preference")

// SetDevicePreferences sets the device preferences.
func SetDevicePreferences(device *Device) error {
prefsSrc.Set("device.id", device.ID)
prefsSrc.Set("device.name", device.Name)
if err := prefsSrc.Set("device.id", device.ID); err != nil {
return fmt.Errorf("%w: %w", ErrSetDevicePreference, err)
}

if err := prefsSrc.Set("device.name", device.Name); err != nil {
return fmt.Errorf("%w: %w", ErrSetDevicePreference, err)
}

return nil
}
Expand All @@ -27,23 +39,3 @@ func DeviceName() string {
func DeviceID() string {
return prefsSrc.String("device.id")
}

// func (p *Preferences) GetDeviceInfo() *Device {
// return p.Device
// }

// func (p *Preferences) DeviceName() string {
// if p.Device != nil {
// return p.Device.Name
// }

// return unknownValue
// }

// func (p *Preferences) DeviceID() string {
// if p.Device != nil {
// return p.Device.ID
// }

// return unknownValue
// }
41 changes: 32 additions & 9 deletions internal/preferences/hass.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,49 @@ type Hass struct {
WebsocketURL string `toml:"websocketurl" json:"-" validate:"required,url"`
}

var ErrSaveHassPreferences = errors.New("error saving hass preferences")
var (
ErrSaveHassPreferences = errors.New("error saving hass preferences")
ErrSetHassPreference = errors.New("could not set hass preference")
)

//revive:disable:indent-error-flow
func SetHassPreferences(hassPrefs *Hass, regPrefs *Registration) error {
prefsSrc.Set("hass.secret", hassPrefs.Secret)
if err := prefsSrc.Set("hass.secret", hassPrefs.Secret); err != nil {
return fmt.Errorf("%w: %w", ErrSetHassPreference, err)
}

// Generate an API URL and set preferences as appropriate.
if apiURL, err := generateAPIURL(hassPrefs, regPrefs); err != nil {
return fmt.Errorf("%w: %w", ErrSaveHassPreferences, err)
} else {
prefsSrc.Set("hass.apiurl", apiURL)
if err := prefsSrc.Set("hass.apiurl", apiURL); err != nil {
return fmt.Errorf("%w: %w", ErrSetHassPreference, err)
}
}

// Generate a websocket URL and set preferences as appropriate.
if websocketURL, err := generateWebsocketURL(regPrefs); err != nil {
return fmt.Errorf("%w: %w", ErrSaveHassPreferences, err)
} else {
prefsSrc.Set("hass.websocketurl", websocketURL)
if err := prefsSrc.Set("hass.websocketurl", websocketURL); err != nil {
return fmt.Errorf("%w: %w", ErrSetHassPreference, err)
}
}

// Set the webhookID if present.
if hassPrefs.WebhookID != "" {
prefsSrc.Set("hass.webhook_id", hassPrefs.WebhookID)
if err := prefsSrc.Set("hass.webhook_id", hassPrefs.WebhookID); err != nil {
return fmt.Errorf("%w: %w", ErrSetHassPreference, err)
}
}

if err := prefsSrc.Set("registration.server", regPrefs.Server); err != nil {
return fmt.Errorf("%w: %w", ErrSetHassPreference, err)
}

prefsSrc.Set("registration.server", regPrefs.Server)
prefsSrc.Set("registration.token", regPrefs.Token)
if err := prefsSrc.Set("registration.token", regPrefs.Token); err != nil {
return fmt.Errorf("%w: %w", ErrSetHassPreference, err)
}

return nil
}
Expand Down Expand Up @@ -83,10 +100,16 @@ func Token() string {
func generateAPIURL(hassPrefs *Hass, regPrefs *Registration) (string, error) {
switch {
case hassPrefs.CloudhookURL != "" && regPrefs.IgnoreHassURLs:
prefsSrc.Set("hass.cloudhook_url", hassPrefs.CloudhookURL)
if err := prefsSrc.Set("hass.cloudhook_url", hassPrefs.CloudhookURL); err != nil {
return "", fmt.Errorf("%w: %w", ErrSetHassPreference, err)
}

return hassPrefs.CloudhookURL, nil
case hassPrefs.RemoteUIURL != "" && hassPrefs.WebhookID != "" && !regPrefs.IgnoreHassURLs:
prefsSrc.Set("hass.remote_ui_url", hassPrefs.CloudhookURL)
if err := prefsSrc.Set("hass.remote_ui_url", hassPrefs.CloudhookURL); err != nil {
return "", fmt.Errorf("%w: %w", ErrSetHassPreference, err)
}

return hassPrefs.RemoteUIURL + WebHookPath + hassPrefs.WebhookID, nil
default:
apiURL, err := url.Parse(regPrefs.Server)
Expand Down
27 changes: 22 additions & 5 deletions internal/preferences/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package preferences

import (
"errors"
"fmt"

mqtthass "github.com/joshuar/go-hass-anything/v12/pkg/hass"
Expand All @@ -20,12 +21,28 @@ type MQTT struct {
MQTTEnabled bool `toml:"enabled" validate:"boolean" kong:"-"`
}

var ErrSetMQTTPreference = errors.New("could not set MQTT preference")

func SetMQTTPreferences(prefs *MQTT) error {
prefsSrc.Set("mqtt.server", prefs.MQTTServer)
prefsSrc.Set("mqtt.user", prefs.MQTTUser)
prefsSrc.Set("mqtt.password", prefs.MQTTPassword)
prefsSrc.Set("mqtt.topic_prefix", prefs.MQTTTopicPrefix)
prefsSrc.Set("mqtt.enabled", prefs.MQTTEnabled)
if err := prefsSrc.Set("mqtt.server", prefs.MQTTServer); err != nil {
return fmt.Errorf("%w: %w", ErrSetMQTTPreference, err)
}

if err := prefsSrc.Set("mqtt.user", prefs.MQTTUser); err != nil {
return fmt.Errorf("%w: %w", ErrSetMQTTPreference, err)
}

if err := prefsSrc.Set("mqtt.password", prefs.MQTTPassword); err != nil {
return fmt.Errorf("%w: %w", ErrSetMQTTPreference, err)
}

if err := prefsSrc.Set("mqtt.topic_prefix", prefs.MQTTTopicPrefix); err != nil {
return fmt.Errorf("%w: %w", ErrSetMQTTPreference, err)
}

if err := prefsSrc.Set("mqtt.enabled", prefs.MQTTEnabled); err != nil {
return fmt.Errorf("%w: %w", ErrSetMQTTPreference, err)
}

return nil
}
Expand Down
29 changes: 20 additions & 9 deletions internal/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,29 @@ func Run(ctx context.Context) error {
return fmt.Errorf("cannot read old preferences: %w", err)
}

if err := preferences.Load(ctx); err != nil && !errors.Is(err, preferences.ErrLoadPreferences) {
if err = preferences.Load(ctx); err != nil && !errors.Is(err, preferences.ErrLoadPreferences) {
return fmt.Errorf("cannot initialize new preferences: %w", err)
}

// Map old preferences to new preferences.
preferences.SetRegistered(oldPrefs.Registered)
// Registered status.
if err = preferences.SetRegistered(oldPrefs.Registered); err != nil {
return fmt.Errorf("cannot set new preferences: %w", err)
}

// MQTT preferences.
if oldPrefs.MQTTEnabled {
preferences.SetMQTTPreferences(&preferences.MQTT{
if err = preferences.SetMQTTPreferences(&preferences.MQTT{
MQTTEnabled: true,
MQTTServer: oldPrefs.MQTTServer,
MQTTUser: oldPrefs.MQTTUser,
MQTTPassword: oldPrefs.MQTTPassword,
MQTTTopicPrefix: oldPrefs.MQTTTopicPrefix,
})
}); err != nil {
return fmt.Errorf("cannot set new preferences: %w", err)
}
}
preferences.SetHassPreferences(

if err = preferences.SetHassPreferences(
// Hass preferences.
&preferences.Hass{
Secret: oldPrefs.Secret,
Expand All @@ -103,12 +109,17 @@ func Run(ctx context.Context) error {
&preferences.Registration{
Server: oldPrefs.Host,
Token: oldPrefs.Token,
})
}); err != nil {
return fmt.Errorf("cannot set new preferences: %w", err)
}

// Device preferences.
preferences.SetDevicePreferences(&preferences.Device{
if err = preferences.SetDevicePreferences(&preferences.Device{
Name: oldPrefs.DeviceName,
ID: oldPrefs.DeviceID,
})
}); err != nil {
return fmt.Errorf("cannot set new preferences: %w", err)
}

err = preferences.Save(ctx)
if err != nil {
Expand Down

0 comments on commit f6dca52

Please sign in to comment.