-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linux): add sensors for tracking shutdown/suspend state via D-Bus
- Loading branch information
Showing
4 changed files
with
95 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright (c) 2023 Joshua Rich <[email protected]> | ||
// | ||
// This software is released under the MIT License. | ||
// https://opensource.org/licenses/MIT | ||
|
||
package linux | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/godbus/dbus/v5" | ||
"github.com/joshuar/go-hass-agent/internal/device" | ||
"github.com/joshuar/go-hass-agent/internal/hass/sensor" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type powerMgmtSensor struct { | ||
linuxSensor | ||
} | ||
|
||
func (s *powerMgmtSensor) SensorType() sensor.SensorType { | ||
return sensor.TypeBinary | ||
} | ||
|
||
func (s *powerMgmtSensor) Icon() string { | ||
if isTrue, ok := s.value.(bool); ok && isTrue { | ||
switch s.sensorType { | ||
case isSuspended: | ||
return "mdi:power-sleep" | ||
case isShutdown: | ||
return "mdi:power-off" | ||
} | ||
} | ||
return "mdi:power-on" | ||
} | ||
|
||
func PowerMgmtUpdater(ctx context.Context, tracker device.SensorTracker) { | ||
r := NewBusRequest(ctx, SystemBus). | ||
Path("/org/freedesktop/login1"). | ||
Match([]dbus.MatchOption{ | ||
dbus.WithMatchInterface("org.freedesktop.login1.Manager"), | ||
}) | ||
|
||
err := NewBusRequest(ctx, SystemBus). | ||
Path("/org/freedesktop/login1"). | ||
Match([]dbus.MatchOption{ | ||
dbus.WithMatchInterface("org.freedesktop.login1.Manager"), | ||
}).Event("org.freedesktop.login1.Manager.PrepareForSleep"). | ||
Handler(func(s *dbus.Signal) { | ||
if s.Name != "org.freedesktop.login1.Manager.PrepareForSleep" { | ||
return | ||
} | ||
if err := tracker.UpdateSensors(ctx, newPowerMgmtSensor(isSuspended, s.Body[0].(bool))); err != nil { | ||
log.Error().Err(err).Msg("Could not update suspended sensor.") | ||
} | ||
}). | ||
AddWatch(ctx) | ||
if err != nil { | ||
log.Warn().Err(err). | ||
Msg("Failed to create user D-Bus watch. Will not track suspend state.") | ||
} | ||
err = r.Event("org.freedesktop.login1.Manager.PrepareForShutdown"). | ||
Handler(func(s *dbus.Signal) { | ||
if s.Name != "org.freedesktop.login1.Manager.PrepareForShutdown" { | ||
return | ||
} | ||
if err = tracker.UpdateSensors(ctx, newPowerMgmtSensor(isShutdown, s.Body[0].(bool))); err != nil { | ||
log.Error().Err(err).Msg("Could not update powered off sensor.") | ||
} | ||
}). | ||
AddWatch(ctx) | ||
if err != nil { | ||
log.Warn().Err(err). | ||
Msg("Failed to create user D-Bus watch. Will not track powered off state.") | ||
} | ||
} | ||
|
||
func newPowerMgmtSensor(t sensorType, v interface{}) *powerMgmtSensor { | ||
return &powerMgmtSensor{ | ||
linuxSensor: linuxSensor{ | ||
value: v, | ||
sensorType: t, | ||
source: srcDbus, | ||
diagnostic: true, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.