From 7b65aab7b6473c2377c19af7e1206241f2765954 Mon Sep 17 00:00:00 2001 From: Joshua Rich Date: Mon, 4 Nov 2024 15:00:01 +1000 Subject: [PATCH] feat(linux/system): :sparkles: add preferences to optionally disable hwmon sensors --- .vscode/settings.json | 3 ++- internal/linux/system/common.go | 12 ++++++++++++ internal/linux/system/hwmon.go | 26 ++++++++++++++++++++++++-- 3 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 internal/linux/system/common.go diff --git a/.vscode/settings.json b/.vscode/settings.json index cd458f5c0..ad21c8103 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -30,7 +30,8 @@ "linux/net", "linux/media", "agent/sensor", - "linux/cpu" + "linux/cpu", + "linux/system" ], "go.testFlags": ["-v"], "[markdown]": { diff --git a/internal/linux/system/common.go b/internal/linux/system/common.go new file mode 100644 index 000000000..3dc35512e --- /dev/null +++ b/internal/linux/system/common.go @@ -0,0 +1,12 @@ +// Copyright 2024 Joshua Rich . +// SPDX-License-Identifier: MIT + +package system + +const ( + preferencesID = "system_sensors" +) + +type WorkerPrefs struct { + DisableHWMon bool `toml:"disable_hwmon" comment:"Set to true to disable hwmon sensors."` +} diff --git a/internal/linux/system/hwmon.go b/internal/linux/system/hwmon.go index 1a0c581c5..6966e28d2 100644 --- a/internal/linux/system/hwmon.go +++ b/internal/linux/system/hwmon.go @@ -14,6 +14,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" "github.com/joshuar/go-hass-agent/pkg/linux/hwmon" ) @@ -93,9 +94,30 @@ func (w *hwMonWorker) Sensors(_ context.Context) ([]sensor.Entity, error) { return sensors, nil } -func NewHWMonWorker(_ context.Context) (*linux.PollingSensorWorker, error) { +func (w *hwMonWorker) PreferencesID() string { + return preferencesID +} + +func (w *hwMonWorker) DefaultPreferences() WorkerPrefs { + return WorkerPrefs{} +} + +func NewHWMonWorker(ctx context.Context) (*linux.PollingSensorWorker, error) { worker := linux.NewPollingSensorWorker(hwmonWorkerID, hwMonInterval, hwMonJitter) - worker.PollingSensorType = &hwMonWorker{} + + hwMonWorker := &hwMonWorker{} + + prefs, err := preferences.LoadWorkerPreferences(ctx, hwMonWorker) + if err != nil { + return worker, fmt.Errorf("could not load preferences: %w", err) + } + + // If disabled, don't use. + if prefs.DisableHWMon { + return worker, nil + } + + worker.PollingSensorType = hwMonWorker return worker, nil }