Skip to content

Commit

Permalink
feat(linux/system): ✨ add preferences to optionally disable hwmon sen…
Browse files Browse the repository at this point in the history
…sors
  • Loading branch information
joshuar committed Nov 4, 2024
1 parent ecc5cc6 commit 7b65aab
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"linux/net",
"linux/media",
"agent/sensor",
"linux/cpu"
"linux/cpu",
"linux/system"
],
"go.testFlags": ["-v"],
"[markdown]": {
Expand Down
12 changes: 12 additions & 0 deletions internal/linux/system/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2024 Joshua Rich <[email protected]>.
// 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."`
}
26 changes: 24 additions & 2 deletions internal/linux/system/hwmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

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

0 comments on commit 7b65aab

Please sign in to comment.