Skip to content

Commit

Permalink
fix(agent): 🐛 support passing registration parameters via command-lin…
Browse files Browse the repository at this point in the history
…e when running in graphical mode

- if `--server` and `--token` are specified on the command-line, use those values and don't display the registration details window when running `go-hass-agent register` in graphical mode
  • Loading branch information
joshuar committed Aug 4, 2024
1 parent 2e46627 commit 4e35159
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
4 changes: 2 additions & 2 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,6 @@ func (agent *Agent) Run(ctx context.Context, trk SensorTracker, reg Registry) er
}

func (agent *Agent) Register(ctx context.Context, trk SensorTracker) {
defer agent.Stop()

var wg sync.WaitGroup

wg.Add(1)
Expand All @@ -252,6 +250,8 @@ func (agent *Agent) Register(ctx context.Context, trk SensorTracker) {
if err := agent.checkRegistration(ctx, trk); err != nil {
agent.logger.Log(ctx, logging.LevelFatal, "Error checking registration status", "error", err.Error())
}

close(agent.done)
}()

agent.ui.Run(ctx, agent, agent.done)
Expand Down
2 changes: 1 addition & 1 deletion internal/agent/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (agent *Agent) checkRegistration(ctx context.Context, trk SensorTracker) er

// If the agent is not running headless, ask the user for registration
// details.
if !agent.headless {
if !agent.headless && agent.prefs.Registration.IsDefault() {
userInputDone := make(chan struct{})
agent.ui.DisplayRegistrationWindow(ctx, agent.prefs, userInputDone)
<-userInputDone
Expand Down
13 changes: 8 additions & 5 deletions internal/preferences/prefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const (
LogFile = "go-hass-agent.log"
preferencesFile = "preferences.toml"
defaultFilePerms = 0o600

defaultServer = "http://localhost:8123"
defaultSecret = "ALongSecretString"
)

var (
Expand Down Expand Up @@ -155,14 +158,14 @@ func DefaultPreferences(file string) *Preferences {
Version: AppVersion,
Registered: false,
Registration: &Registration{
Server: "http://localhost:8123",
Token: "ASecretLongLivedToken",
Server: defaultServer,
Token: defaultSecret,
},
Hass: &Hass{
IgnoreHassURLs: false,
WebhookID: "ALongString",
RestAPIURL: "https://localhost:8123",
WebsocketURL: "https://localhost:8123",
WebhookID: defaultSecret,
RestAPIURL: defaultServer,
WebsocketURL: defaultServer,
},
MQTT: &MQTT{MQTTEnabled: false},
Device: device,
Expand Down
4 changes: 4 additions & 0 deletions internal/preferences/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ func (p *Registration) Validate() error {

return nil
}

func (p *Registration) IsDefault() bool {
return p.Server == defaultServer && p.Token == defaultSecret
}

0 comments on commit 4e35159

Please sign in to comment.