Skip to content

Commit

Permalink
fix(linux/battery): 🐛 don't add already tracked batteries
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Dec 28, 2024
1 parent 70176ce commit 76b78e4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"linux/system",
"linux/mem",
"BREAKING_CHANGES",
"linux/power"
"linux/power",
"linux/battery"
],
"go.testFlags": ["-v"],
"[markdown]": {
Expand Down
35 changes: 17 additions & 18 deletions internal/linux/battery/worker.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright (c) 2024 Joshua Rich <[email protected]>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
// Copyright 2024 Joshua Rich <[email protected]>.
// SPDX-License-Identifier: MIT

package battery

Expand Down Expand Up @@ -44,6 +42,7 @@ func (w *sensorWorker) Events(ctx context.Context) (<-chan sensor.Entity, error)
w.logger.Warn("Could not retrieve any battery details from D-Bus.", slog.Any("error", err))
}

// For all batteries, start monitoring.
for _, path := range batteries {
wg.Add(1)

Expand All @@ -58,6 +57,7 @@ func (w *sensorWorker) Events(ctx context.Context) (<-chan sensor.Entity, error)

wg.Add(1)

// Send all sensor updates from all tracked batteries to Home Assistant.
go func() {
defer wg.Done()

Expand Down Expand Up @@ -86,8 +86,19 @@ func (w *sensorWorker) getBatteries() ([]dbus.ObjectPath, error) {
}

func (w *sensorWorker) track(ctx context.Context, batteryPath dbus.ObjectPath) <-chan sensor.Entity {
w.mu.Lock()
defer w.mu.Unlock()

sensorCh := make(chan sensor.Entity)

// Ignore if the battery is already being tracked.
if _, found := w.batteryList[batteryPath]; found {
slog.Debug("Battery already monitored", slog.String("path", string(batteryPath)))
close(sensorCh)

return sensorCh
}

var wg sync.WaitGroup

battery, err := newBattery(w.bus, w.logger, batteryPath)
Expand All @@ -101,12 +112,11 @@ func (w *sensorWorker) track(ctx context.Context, batteryPath dbus.ObjectPath) <

battCtx, cancelFunc := context.WithCancel(ctx)

w.mu.Lock()
w.batteryList[batteryPath] = cancelFunc
w.mu.Unlock()

wg.Add(1)

// Get a list of sensors for this battery and send their initial state.
go func() {
defer wg.Done()

Expand All @@ -117,6 +127,7 @@ func (w *sensorWorker) track(ctx context.Context, batteryPath dbus.ObjectPath) <

wg.Add(1)

// Set up a goroutine to monitor for subsequent battery sensor changes.
go func() {
defer wg.Done()

Expand Down Expand Up @@ -158,18 +169,6 @@ func (w *sensorWorker) monitorBatteryChanges(ctx context.Context) <-chan sensor.

sensorCh := make(chan sensor.Entity)

// events, err := dbusx.NewWatch(
// dbusx.MatchPath(upowerDBusPath),
// dbusx.MatchInterface(upowerDBusDest),
// dbusx.MatchMember(deviceAddedSignal, deviceRemovedSignal),
// ).Start(ctx, w.bus)
// if err != nil {
// w.logger.Debug("Failed to create D-Bus watch for battery additions/removals.", "error", err.Error())
// close(sensorCh)

// return sensorCh
// }

go func() {
w.logger.Debug("Monitoring for battery additions/removals.")

Expand Down

0 comments on commit 76b78e4

Please sign in to comment.