From 71a49696dc7fff379a78e039099411842428f269 Mon Sep 17 00:00:00 2001
From: Joshua Rich <joshua.rich@gmail.com>
Date: Sat, 14 Dec 2024 14:50:08 +1000
Subject: [PATCH] feat: :sparkles: allow disabling app sensors

---
 internal/linux/apps/apps.go | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/internal/linux/apps/apps.go b/internal/linux/apps/apps.go
index d404f4dd6..b012c8cb8 100644
--- a/internal/linux/apps/apps.go
+++ b/internal/linux/apps/apps.go
@@ -16,6 +16,7 @@ import (
 
 	"github.com/joshuar/go-hass-agent/internal/hass/sensor"
 	"github.com/joshuar/go-hass-agent/internal/linux"
+	"github.com/joshuar/go-hass-agent/internal/preferences"
 	"github.com/joshuar/go-hass-agent/pkg/linux/dbusx"
 )
 
@@ -30,6 +31,16 @@ const (
 
 var ErrNoApps = errors.New("no running apps")
 
+type WorkerPrefs preferences.CommonWorkerPrefs
+
+func (w *sensorWorker) PreferencesID() string {
+	return workerID
+}
+
+func (w *sensorWorker) DefaultPreferences() WorkerPrefs {
+	return WorkerPrefs{}
+}
+
 type sensorWorker struct {
 	getAppStates     func() (map[string]dbus.Variant, error)
 	triggerCh        chan dbusx.Trigger
@@ -117,6 +128,7 @@ func NewAppWorker(ctx context.Context) (*linux.EventSensorWorker, error) {
 		return worker, linux.ErrNoDesktopPortal
 	}
 
+	// Connect to the D-Bus session bus. Bail if we can't.
 	bus, ok := linux.CtxGetSessionBus(ctx)
 	if !ok {
 		return worker, linux.ErrNoSessionBus
@@ -131,7 +143,7 @@ func NewAppWorker(ctx context.Context) (*linux.EventSensorWorker, error) {
 		return worker, fmt.Errorf("could not watch D-Bus for app state events: %w", err)
 	}
 
-	worker.EventSensorType = &sensorWorker{
+	appsWorker := &sensorWorker{
 		triggerCh: triggerCh,
 		getAppStates: func() (map[string]dbus.Variant, error) {
 			apps, err := dbusx.GetData[map[string]dbus.Variant](bus, appStateDBusPath, portalDest, appStateDBusMethod)
@@ -147,5 +159,16 @@ func NewAppWorker(ctx context.Context) (*linux.EventSensorWorker, error) {
 		},
 	}
 
+	prefs, err := preferences.LoadWorker(ctx, appsWorker)
+	if err != nil {
+		return worker, fmt.Errorf("could not load preferences: %w", err)
+	}
+
+	if prefs.Disabled {
+		return worker, nil
+	}
+
+	worker.EventSensorType = appsWorker
+
 	return worker, nil
 }