-
-
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): added laptop lid sensor
- Loading branch information
Showing
5 changed files
with
109 additions
and
25 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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) 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 | ||
} |
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.