Skip to content

Commit

Permalink
feat: ✨ allow disabling app sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Dec 14, 2024
1 parent cc18b67 commit 71a4969
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion internal/linux/apps/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
}

0 comments on commit 71a4969

Please sign in to comment.