Skip to content

Commit

Permalink
feat(linux): added laptop lid sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynis authored and joshuar committed Apr 9, 2024
1 parent 0395c16 commit 0d92428
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 25 deletions.
1 change: 1 addition & 0 deletions internal/agent/device_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func sensorWorkers() []func(context.Context) chan sensor.Details {
disk.UsageUpdater,
time.Updater,
power.ScreenLockUpdater,
power.LaptopLidUpdater,
power.PowerStateUpdater,
power.PowerProfileUpdater,
user.Updater,
Expand Down
26 changes: 10 additions & 16 deletions internal/agent/mockUI_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 87 additions & 0 deletions internal/linux/power/laptopLid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) 2024 Joshua Rich <[email protected]>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

package power

import (
"context"
"strings"

"github.com/godbus/dbus/v5"
"github.com/rs/zerolog/log"

"github.com/joshuar/go-hass-agent/internal/hass/sensor"
"github.com/joshuar/go-hass-agent/internal/linux"
"github.com/joshuar/go-hass-agent/pkg/linux/dbusx"
)

type laptopLidSensor struct {
linux.Sensor
}

func (s *laptopLidSensor) Icon() string {
state, ok := s.Value.(bool)
if !ok {
return "mdi:lock-alert"
}
if state {
return "mdi:laptop"
}
return "mdi:laptop-off"
}

func newLaptopLidEvent(v bool) *laptopLidSensor {
return &laptopLidSensor{
Sensor: linux.Sensor{
SensorTypeValue: linux.SensorLaptopLid,
IsBinary: true,
SensorSrc: linux.DataSrcDbus,
Value: v,
},
}
}

func LaptopLidUpdater(ctx context.Context) chan sensor.Details {
sensorCh := make(chan sensor.Details)
err := dbusx.NewBusRequest(ctx, dbusx.SystemBus).
Match([]dbus.MatchOption{
dbus.WithMatchObjectPath("/org/freedesktop/login1/session"),
dbus.WithMatchInterface("org.freedesktop.login1.Manager"),
}).
Handler(func(s *dbus.Signal) {
if !strings.Contains(string(s.Path), "/org/freedesktop/login1") || len(s.Body) <= 1 {
log.Trace().Str("runner", "power").Msg("Not my signal or empty signal body.")
return
}

if s.Name == dbusx.PropChangedSignal {
props, ok := s.Body[1].(map[string]dbus.Variant)
if !ok {
log.Trace().Str("runner", "power").
Str("signal", s.Name).Interface("body", s.Body).
Msg("Unexpected signal body")
return
}
if v, ok := props["LidClosed"]; ok {
sensorCh <- newLaptopLidEvent(!dbusx.VariantToValue[bool](v))
}
}

}).
AddWatch(ctx)
if err != nil {
log.Warn().Err(err).
Msg("Could not poll D-Bus for laptopLid. LaptopLid sensor will not run.")
close(sensorCh)
return sensorCh
}
log.Trace().Msg("Started laptopLid sensor.")
go func() {
defer close(sensorCh)
<-ctx.Done()
log.Trace().Msg("Stopped laptopLid sensor.")
}()
return sensorCh
}
1 change: 1 addition & 0 deletions internal/linux/sensorType.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (
SensorLoad15 // CPU load average (15 min)
SensorCPUPc // CPU Usage
SensorScreenLock // Screen Lock
SensorLaptopLid // Laptop Lid
SensorProblem // Problems
SensorKernel // Kernel Version
SensorDistribution // Distribution Name
Expand Down
19 changes: 10 additions & 9 deletions internal/linux/sensorTypeStrings.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0d92428

Please sign in to comment.