Skip to content

Commit

Permalink
refactor(agent): ♻️ fetch config from context rather than passing dir…
Browse files Browse the repository at this point in the history
…ectly
  • Loading branch information
joshuar committed Jan 12, 2024
1 parent fb2524c commit 00dbc00
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"vscode",
"ui",
"README",
"go"
"go",
"all"
],
"go.lintTool": "golangci-lint",
"go.lintFlags": [
Expand Down
2 changes: 1 addition & 1 deletion internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func Run(options Options) {
wg.Add(1)
go func() {
defer wg.Done()
agent.runNotificationsWorker(ctx, cfg, options)
agent.runNotificationsWorker(ctx, options)
}()
}()

Expand Down
5 changes: 2 additions & 3 deletions internal/agent/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import (
"context"
"sync"

"github.com/joshuar/go-hass-agent/internal/agent/config"
"github.com/joshuar/go-hass-agent/internal/hass/api"
"github.com/rs/zerolog/log"
)

func (agent *Agent) runNotificationsWorker(ctx context.Context, cfg config.Config, options Options) {
func (agent *Agent) runNotificationsWorker(ctx context.Context, options Options) {
if options.Headless {
log.Warn().Msg("Will not send notifications as there is no supported windowing environment.")
return
Expand Down Expand Up @@ -46,7 +45,7 @@ func (agent *Agent) runNotificationsWorker(ctx context.Context, cfg config.Confi
log.Debug().Msg("Stopping websocket.")
return
default:
api.StartWebsocket(ctx, cfg, notifyCh)
api.StartWebsocket(ctx, notifyCh)
}
}
}()
Expand Down
8 changes: 7 additions & 1 deletion internal/hass/api/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ type websocketResponse struct {
Success bool `json:"success,omitempty"`
}

func StartWebsocket(ctx context.Context, cfg config.Config, notifyCh chan [2]string) {
func StartWebsocket(ctx context.Context, notifyCh chan [2]string) {
cfg := config.FetchFromContext(ctx)
if cfg == nil {
log.Warn().Msg("Cannot retrieve config. Cannot listen for notifications.")
return
}

var websocketURL string
if err := cfg.Get(config.PrefWebsocketURL, &websocketURL); err != nil {
log.Warn().Err(err).Msg("Could not retrieve websocket URL from config.")
Expand Down

0 comments on commit 00dbc00

Please sign in to comment.