diff --git a/.vscode/settings.json b/.vscode/settings.json index 023759522..cd458f5c0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -29,7 +29,8 @@ "README", "linux/net", "linux/media", - "agent/sensor" + "agent/sensor", + "linux/cpu" ], "go.testFlags": ["-v"], "[markdown]": { diff --git a/internal/linux/cpu/common.go b/internal/linux/cpu/common.go new file mode 100644 index 000000000..2b2403498 --- /dev/null +++ b/internal/linux/cpu/common.go @@ -0,0 +1,15 @@ +// Copyright 2024 Joshua Rich . +// SPDX-License-Identifier: MIT + +package cpu + +import "github.com/joshuar/go-hass-agent/internal/preferences" + +const ( + preferencesID = "cpu_sensors" +) + +type WorkerPrefs struct { + DisableCPUFreq bool `toml:"disable_cpufreq" comment:"Set to true to disable CPU frequency sensors."` + preferences.CommonWorkerPrefs +} diff --git a/internal/linux/cpu/usageWorker.go b/internal/linux/cpu/usageWorker.go index 4b011933e..cdf376d63 100644 --- a/internal/linux/cpu/usageWorker.go +++ b/internal/linux/cpu/usageWorker.go @@ -1,8 +1,7 @@ -// Copyright (c) 2024 Joshua Rich -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT +// Copyright 2024 Joshua Rich . +// SPDX-License-Identifier: MIT +//revive:disable:unused-receiver package cpu import ( @@ -19,6 +18,7 @@ import ( "github.com/joshuar/go-hass-agent/internal/hass/sensor" "github.com/joshuar/go-hass-agent/internal/hass/sensor/types" "github.com/joshuar/go-hass-agent/internal/linux" + "github.com/joshuar/go-hass-agent/internal/preferences" ) const ( @@ -39,6 +39,7 @@ type usageWorker struct { linux.PollingSensorWorker clktck int64 delta time.Duration + prefs WorkerPrefs } func (w *usageWorker) UpdateDelta(delta time.Duration) { @@ -49,7 +50,17 @@ func (w *usageWorker) Sensors(_ context.Context) ([]sensor.Entity, error) { return w.getUsageStats() } +func (w *usageWorker) PreferencesID() string { + return preferencesID +} + +func (w *usageWorker) DefaultPreferences() WorkerPrefs { + return WorkerPrefs{} +} + func NewUsageWorker(ctx context.Context) (*linux.PollingSensorWorker, error) { + var err error + worker := linux.NewPollingSensorWorker(usageWorkerID, usageUpdateInterval, usageUpdateJitter) clktck, found := linux.CtxGetClkTck(ctx) @@ -62,7 +73,7 @@ func NewUsageWorker(ctx context.Context) (*linux.PollingSensorWorker, error) { return worker, fmt.Errorf("%w: no boottime value", linux.ErrInvalidCtx) } - worker.PollingSensorType = &usageWorker{ + cpuUsageWorker := &usageWorker{ path: filepath.Join(linux.ProcFSRoot, "stat"), boottime: boottime, clktck: clktck, @@ -72,6 +83,18 @@ func NewUsageWorker(ctx context.Context) (*linux.PollingSensorWorker, error) { }, } + cpuUsageWorker.prefs, err = preferences.LoadWorkerPreferences(ctx, cpuUsageWorker) + if err != nil { + return worker, fmt.Errorf("could not load preferences: %w", err) + } + + // If disabled, don't use the addressWorker. + if cpuUsageWorker.prefs.Disabled { + return worker, nil + } + + worker.PollingSensorType = cpuUsageWorker + return worker, nil } @@ -105,7 +128,9 @@ func (w *usageWorker) getUsageStats() ([]sensor.Entity, error) { sensors = append(sensors, newUsageSensor(w.clktck, cols, types.CategoryDefault)) case strings.Contains(cols[0], "cpu"): sensors = append(sensors, newUsageSensor(w.clktck, cols, types.CategoryDiagnostic)) - sensors = append(sensors, newCPUFreqSensor(cols[0])) + if !w.prefs.DisableCPUFreq { + sensors = append(sensors, newCPUFreqSensor(cols[0])) + } case cols[0] == "ctxt": if _, found := w.rateSensors["ctxt"]; found { w.rateSensors["ctxt"].update(w.delta, cols[1])