Skip to content

Commit

Permalink
feat(components/preferences)!: restructure sensor/control preferences
Browse files Browse the repository at this point in the history
- sensors and controls preferences are now split under [sensors] and [controls] in the preferences file
- preferences are grouped under [sensors]/[controls] by the worker that produces them, in user-friendly sub-groups

BREAKING CHANGE: Sensors and (MQTT) controls preferences have been restructured in the preferences file. Users who have customised any sensor/control preferences will need to manually migrate the changes to the new structure.
  • Loading branch information
joshuar committed Feb 1, 2025
1 parent 101b2a8 commit 5376ce1
Show file tree
Hide file tree
Showing 47 changed files with 156 additions and 87 deletions.
2 changes: 1 addition & 1 deletion internal/agent/agentsensor/connection_latency.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type ConnectionLatencySensorWorker struct {
}

func (w *ConnectionLatencySensorWorker) PreferencesID() string {
return "connection_latency_sensor"
return preferences.SensorsPrefPrefix + "agent" + preferences.PathDelim + "connection_latency"
}

func (w *ConnectionLatencySensorWorker) DefaultPreferences() preferences.CommonWorkerPrefs {
Expand Down
2 changes: 1 addition & 1 deletion internal/agent/agentsensor/external_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type ExternalIPWorker struct {
}

func (w *ExternalIPWorker) PreferencesID() string {
return "external_ip_sensor"
return preferences.SensorsPrefPrefix + "agent" + preferences.PathDelim + "external_ip"
}

func (w *ExternalIPWorker) DefaultPreferences() preferences.CommonWorkerPrefs {
Expand Down
2 changes: 1 addition & 1 deletion internal/agent/agentsensor/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type VersionWorker struct {
}

func (w *VersionWorker) PreferencesID() string {
return "version_sensor"
return preferences.SensorsPrefPrefix + "agent" + preferences.PathDelim + "version"
}

func (w *VersionWorker) DefaultPreferences() preferences.CommonWorkerPrefs {
Expand Down
3 changes: 1 addition & 2 deletions internal/agent/workers_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/joshuar/go-hass-agent/internal/hass/event"
"github.com/joshuar/go-hass-agent/internal/hass/sensor"
"github.com/joshuar/go-hass-agent/internal/linux"
"github.com/joshuar/go-hass-agent/internal/linux/apps"
"github.com/joshuar/go-hass-agent/internal/linux/battery"
"github.com/joshuar/go-hass-agent/internal/linux/cpu"
"github.com/joshuar/go-hass-agent/internal/linux/desktop"
Expand All @@ -27,13 +26,13 @@ import (

// sensorEventWorkersInitFuncs are all of the sensor workers that generate sensors on events.
var sensorEventWorkersInitFuncs = []func(ctx context.Context) (*linux.EventSensorWorker, error){
apps.NewAppWorker,
battery.NewBatteryWorker,
net.NewConnectionWorker,
net.NewAddressWorker,
power.NewProfileWorker,
power.NewStateWorker,
power.NewScreenLockWorker,
desktop.NewAppWorker,
desktop.NewDesktopWorker,
}

Expand Down
3 changes: 2 additions & 1 deletion internal/components/preferences/preferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
defaultMQTTTopicPrefix = "homeassistant"
defaultMQTTServer = "tcp://localhost:1883"
DefaultAppID = "go-hass-agent"
PathDelim = "."
)

// preferences defines all preferences for Go Hass Agent.
Expand All @@ -58,7 +59,7 @@ type preferences struct {

var (
// Package level internal variables.
prefsSrc = koanf.New(".")
prefsSrc = koanf.New(PathDelim)
mu = sync.Mutex{}
prefsFile string
logger *slog.Logger
Expand Down
7 changes: 4 additions & 3 deletions internal/components/preferences/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
)

const (
workerPrefsPrefix = "worker"
SensorsPrefPrefix = "sensors" + PathDelim
ControlsPrefPrefix = "controls" + PathDelim
)

// CommonWorkerPrefs contains worker preferences that all workers can/should
Expand Down Expand Up @@ -52,7 +53,7 @@ func LoadWorker[T any](worker Worker[T]) (*T, error) {
// Get the default preferences.
defaultPrefs = worker.DefaultPreferences()
// Set the key to the worker preferences in the preferences store.
prefsKey := workerPrefsPrefix + "." + worker.PreferencesID()
prefsKey := worker.PreferencesID()
// Try to retrieve any existing preferences. Use those if possible.
foundPrefs := prefsSrc.Get(prefsKey)
if foundPrefs != nil {
Expand Down Expand Up @@ -108,7 +109,7 @@ func SaveWorker[T any](worker Worker[T], prefs T) error {
return fmt.Errorf("%w: %w", ErrSaveWorkerPrefs, err)
}
// Merge the worker preferences into the preferences file.
if err := prefsSrc.Set(workerPrefsPrefix+"."+worker.PreferencesID(), prefsMaps); err != nil {
if err := prefsSrc.Set(worker.PreferencesID(), prefsMaps); err != nil {
return fmt.Errorf("%w: %w", ErrSaveWorkerPrefs, err)
}
// Save the preferences.
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/battery/preferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package battery
import "github.com/joshuar/go-hass-agent/internal/components/preferences"

const (
preferencesID = "battery_sensors"
preferencesID = preferences.SensorsPrefPrefix + "batteries"
)

type WorkerPrefs struct {
Expand Down
4 changes: 2 additions & 2 deletions internal/linux/cpu/freqWorker.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
cpuFreqUpdateJitter = time.Second

cpuFreqWorkerID = "cpu_freq_sensors"
cpuFreqPreferencesID = "freq"
cpuFreqPreferencesID = prefPrefix + "frequencies"
)

type freqWorker struct{}
Expand All @@ -39,7 +39,7 @@ func (w *freqWorker) Sensors(_ context.Context) ([]sensor.Entity, error) {
}

func (w *freqWorker) PreferencesID() string {
return basePreferencesID + "." + cpuFreqPreferencesID
return cpuFreqPreferencesID
}

func (w *freqWorker) DefaultPreferences() FreqWorkerPrefs {
Expand Down
4 changes: 2 additions & 2 deletions internal/linux/cpu/loadAvgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (
loadAvgsTotal = 3

loadAvgsWorkerID = "cpu_loadavg_sensors"
loadAvgsPreferencesID = "loadavg"
loadAvgsPreferencesID = prefPrefix + "load_averages"
)

var ErrParseLoadAvgs = errors.New("could not parse load averages")
Expand Down Expand Up @@ -67,7 +67,7 @@ func (w *loadAvgsWorker) Sensors(_ context.Context) ([]sensor.Entity, error) {
}

func (w *loadAvgsWorker) PreferencesID() string {
return basePreferencesID + "." + loadAvgsPreferencesID
return loadAvgsPreferencesID
}

func (w *loadAvgsWorker) DefaultPreferences() preferences.CommonWorkerPrefs {
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/cpu/preferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package cpu
import "github.com/joshuar/go-hass-agent/internal/components/preferences"

const (
basePreferencesID = "cpu_sensors"
prefPrefix = preferences.SensorsPrefPrefix + "cpu" + preferences.PathDelim
)

// FreqWorkerPrefs are the preferences for the CPU frequency worker.
Expand Down
4 changes: 2 additions & 2 deletions internal/linux/cpu/usageWorker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
cpuUsageUpdateJitter = 500 * time.Millisecond

cpuUsageWorkerID = "cpu_usage_sensors"
cpuUsagePreferencesID = "usage"
cpuUsagePreferencesID = prefPrefix + "usage"
)

type usageWorker struct {
Expand All @@ -47,7 +47,7 @@ func (w *usageWorker) Sensors(_ context.Context) ([]sensor.Entity, error) {
}

func (w *usageWorker) PreferencesID() string {
return basePreferencesID + "." + cpuUsagePreferencesID
return cpuUsagePreferencesID
}

func (w *usageWorker) DefaultPreferences() UsagePrefs {
Expand Down
12 changes: 5 additions & 7 deletions internal/linux/apps/apps.go → internal/linux/desktop/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// https://opensource.org/licenses/MIT

//revive:disable:unused-receiver
package apps
package desktop

import (
"context"
Expand All @@ -27,7 +27,7 @@ const (
appStateDBusInterface = "org.freedesktop.impl.portal.Background"
appStateDBusEvent = "org.freedesktop.impl.portal.Background.RunningApplicationsChanged"

workerID = "app_sensors"
appWorkerID = "app_sensors"

activeAppsIcon = "mdi:application"
activeAppsName = "Active App"
Expand All @@ -41,10 +41,8 @@ const (

var ErrNoApps = errors.New("no running apps")

type WorkerPrefs preferences.CommonWorkerPrefs

func (w *sensorWorker) PreferencesID() string {
return workerID
return prefPrefix + "app"
}

func (w *sensorWorker) DefaultPreferences() WorkerPrefs {
Expand All @@ -60,7 +58,7 @@ type sensorWorker struct {

func (w *sensorWorker) Events(ctx context.Context) (<-chan sensor.Entity, error) {
sensorCh := make(chan sensor.Entity)
logger := slog.Default().With(slog.String("worker", workerID))
logger := slog.Default().With(slog.String("worker", desktopWorkerID))

sendSensors := func(ctx context.Context, sensorCh chan sensor.Entity) {
appSensors, err := w.Sensors(ctx)
Expand Down Expand Up @@ -154,7 +152,7 @@ func (w *sensorWorker) Sensors(_ context.Context) ([]sensor.Entity, error) {
}

func NewAppWorker(ctx context.Context) (*linux.EventSensorWorker, error) {
worker := linux.NewEventSensorWorker(workerID)
worker := linux.NewEventSensorWorker(appWorkerID)

// If we cannot find a portal interface, we cannot monitor the active app.
portalDest, ok := linux.CtxGetDesktopPortal(ctx)
Expand Down
9 changes: 5 additions & 4 deletions internal/linux/desktop/desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const (
colorSchemeProp = "color-scheme"
accentColorProp = "accent-color"

workerID = "desktop_settings_sensors"
desktopWorkerID = "desktop_settings_sensors"
desktopWorkerPrefID = prefPrefix + "preferences"
)

var ErrUnknownProp = errors.New("unknown desktop property")
Expand All @@ -43,7 +44,7 @@ type settingsWorker struct {
}

func (w *settingsWorker) PreferencesID() string {
return preferencesID
return desktopWorkerPrefID
}

func (w *settingsWorker) DefaultPreferences() WorkerPrefs {
Expand All @@ -53,7 +54,7 @@ func (w *settingsWorker) DefaultPreferences() WorkerPrefs {
//nolint:cyclop,gocognit
func (w *settingsWorker) Events(ctx context.Context) (<-chan sensor.Entity, error) {
sensorCh := make(chan sensor.Entity)
logger := logging.FromContext(ctx).With(slog.String("worker", workerID))
logger := logging.FromContext(ctx).With(slog.String("worker", desktopWorkerID))

go func() {
defer close(sensorCh)
Expand Down Expand Up @@ -160,7 +161,7 @@ func (w *settingsWorker) Sensors(ctx context.Context) ([]sensor.Entity, error) {
func NewDesktopWorker(ctx context.Context) (*linux.EventSensorWorker, error) {
var err error

worker := linux.NewEventSensorWorker(workerID)
worker := linux.NewEventSensorWorker(desktopWorkerID)

_, ok := linux.CtxGetDesktopPortal(ctx)
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/desktop/preferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package desktop
import "github.com/joshuar/go-hass-agent/internal/components/preferences"

const (
preferencesID = "desktop_sensors"
prefPrefix = preferences.SensorsPrefPrefix + "desktop" + preferences.PathDelim
)

type WorkerPrefs struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/disk/ioWorker.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (w *ioWorker) Sensors(ctx context.Context) ([]sensor.Entity, error) {
}

func (w *ioWorker) PreferencesID() string {
return basePreferencesID + "." + ioWorkerPreferencesID
return ioWorkerPreferencesID
}

func (w *ioWorker) DefaultPreferences() WorkerPrefs {
Expand Down
6 changes: 3 additions & 3 deletions internal/linux/disk/preferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ package disk
import "github.com/joshuar/go-hass-agent/internal/components/preferences"

const (
basePreferencesID = "disk_sensors"
ioWorkerPreferencesID = "io_sensors"
usageWorkerPreferencesID = "usage_sensors"
prefPrefix = preferences.SensorsPrefPrefix + "disk" + preferences.PathDelim
ioWorkerPreferencesID = prefPrefix + "rates"
usageWorkerPreferencesID = prefPrefix + "usage"
)

type WorkerPrefs struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/disk/usageWorker.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (w *usageWorker) Sensors(ctx context.Context) ([]sensor.Entity, error) {
}

func (w *usageWorker) PreferencesID() string {
return basePreferencesID + "." + usageWorkerPreferencesID
return usageWorkerPreferencesID
}

func (w *usageWorker) DefaultPreferences() WorkerPrefs {
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/location/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
locationUpdatedSignal = clientInterface + ".LocationUpdated"

workerID = "location_worker"
preferencesID = "location"
preferencesID = preferences.SensorsPrefPrefix + "location"
)

type locationWorker struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/media/camera.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (
defaultHeight uint32 = 640
defaultWidth uint32 = 480

cameraPreferencesID = "camera_controls"
cameraPreferencesID = preferences.ControlsPrefPrefix + "media" + preferences.PathDelim + "video"
)

var defaultPreferredFmts = []string{"Motion-JPEG"}
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/media/mpris.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (
mprisDBusPath = "/org/mpris/MediaPlayer2"
mprisDBusNamespace = "org.mpris.MediaPlayer2.Player"

mprisPreferencesID = "mpris"
mprisPreferencesID = preferences.SensorsPrefPrefix + "media" + preferences.PathDelim + "mpris"
)

type mprisMonitor struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/media/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const (
maxVolpc = 100
volStepPc = 1

audioControlPreferencesID = "audio_controls"
audioControlPreferencesID = preferences.ControlsPrefPrefix + "media" + preferences.PathDelim + "audio"
)

// audioControl is a struct containing the data for providing audio state
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/mem/memUsage.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (
memoryUsageSensorPcUnits = "%"

memUsageWorkerID = "memory_usage_sensors"
memUsagePreferencesID = memUsageWorkerID
memUsagePreferencesID = prefPrefix + "usage"
)

// Lists of the memory statistics we want to track as sensors. See /proc/meminfo
Expand Down
2 changes: 1 addition & 1 deletion internal/linux/mem/oomEvents.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

const (
oomEventsWorkerID = "oom_events"
oomEventsPreferencesID = oomEventsWorkerID
oomEventsPreferencesID = prefPrefix + "oom_events"
oomDBusPath = "/org/freedesktop/systemd1/unit"
unitPathPrefix = "/org/freedesktop/systemd1/unit"
oomEventName = "oom_event"
Expand Down
4 changes: 4 additions & 0 deletions internal/linux/mem/preferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ package mem

import "github.com/joshuar/go-hass-agent/internal/components/preferences"

const (
prefPrefix = preferences.SensorsPrefPrefix + "memory" + preferences.PathDelim
)

type WorkerPreferences struct {
preferences.CommonWorkerPrefs
UpdateInterval string `toml:"update_interval" comment:"Time between updates of sensors."`
Expand Down
4 changes: 2 additions & 2 deletions internal/linux/net/dbusConnectionState.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
activeConnectionsProp = "ActivatingConnection"

netConnWorkerID = "network_connection_sensors"
netConnPrefID = prefPrefix + "connections"
)

type ConnectionsWorker struct {
Expand Down Expand Up @@ -134,13 +135,12 @@ func (w *ConnectionsWorker) Events(ctx context.Context) (<-chan sensor.Entity, e
}

func (w *ConnectionsWorker) PreferencesID() string {
return preferencesID
return netConnPrefID
}

func (w *ConnectionsWorker) DefaultPreferences() WorkerPrefs {
return WorkerPrefs{
IgnoredDevices: defaultIgnoredDevices,
UpdateInterval: rateInterval.String(),
}
}

Expand Down
Loading

0 comments on commit 5376ce1

Please sign in to comment.