diff --git a/internal/agent/agent.go b/internal/agent/agent.go index 199c0b19d..e89c0140f 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -142,9 +142,9 @@ func Run(ctx context.Context) error { } var ( - sensorControllers []WorkerController[sensor.Entity] + sensorControllers []Controller[sensor.Entity] mqttControllers []MQTTController - eventControllers []WorkerController[event.Event] + eventControllers []Controller[event.Event] ) // Setup and sort all controllers by type. for _, c := range agent.setupControllers(runCtx, prefs) { @@ -162,7 +162,14 @@ func Run(ctx context.Context) error { // Run workers for any sensor controllers. go func() { defer wg.Done() - runControllerWorkers(runCtx, prefs, sensorControllers...) + runControllers(runCtx, prefs, sensorControllers...) + }() + + wg.Add(1) + // Run workers for any event controllers. + go func() { + defer wg.Done() + runControllers(runCtx, prefs, eventControllers...) }() if len(mqttControllers) > 0 { @@ -174,13 +181,6 @@ func Run(ctx context.Context) error { }() } - wg.Add(1) - // Run workers for any event controllers. - go func() { - defer wg.Done() - runControllerWorkers(runCtx, prefs, eventControllers...) - }() - wg.Add(1) // Listen for notifications from Home Assistant. go func() { diff --git a/internal/agent/controllers.go b/internal/agent/controllers.go index f7263d0d5..b7b954b7c 100644 --- a/internal/agent/controllers.go +++ b/internal/agent/controllers.go @@ -20,7 +20,7 @@ import ( "github.com/joshuar/go-hass-agent/internal/preferences" ) -type WorkerController[T any] interface { +type Controller[T any] interface { ID() string ActiveWorkers() []string // InactiveWorkers is a list of the names of all currently inactive Workers. @@ -33,14 +33,14 @@ type WorkerController[T any] interface { // SensorController represents an object that manages one or more Workers. type SensorController interface { - WorkerController[sensor.Entity] + Controller[sensor.Entity] // States returns the list of all sensor states tracked by all workers of // this controller. States(ctx context.Context) []sensor.Entity } type EventController interface { - WorkerController[event.Event] + Controller[event.Event] } // MQTTController represents an object that is responsible for controlling the @@ -95,7 +95,7 @@ func (agent *Agent) setupControllers(ctx context.Context, prefs *preferences.Pre return controllers } -func runControllerWorkers[T any](ctx context.Context, prefs *preferences.Preferences, controllers ...WorkerController[T]) { +func runControllers[T any](ctx context.Context, prefs *preferences.Preferences, controllers ...Controller[T]) { // Start all inactive workers of all controllers. eventCh := startAllWorkers(ctx, controllers) if len(eventCh) == 0 { @@ -134,7 +134,7 @@ func runControllerWorkers[T any](ctx context.Context, prefs *preferences.Prefere } } -func startAllWorkers[T any](ctx context.Context, controllers []WorkerController[T]) []<-chan T { +func startAllWorkers[T any](ctx context.Context, controllers []Controller[T]) []<-chan T { var eventCh []<-chan T for _, controller := range controllers { @@ -161,7 +161,7 @@ func startAllWorkers[T any](ctx context.Context, controllers []WorkerController[ return eventCh } -func stopAllWorkers[T any](ctx context.Context, controllers []WorkerController[T]) { +func stopAllWorkers[T any](ctx context.Context, controllers []Controller[T]) { for _, controller := range controllers { logging.FromContext(ctx).Debug("Stopping controller", slog.String("controller", controller.ID()))