Skip to content

Commit

Permalink
feat(linux): add screen lock sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Jun 6, 2023
1 parent da80620 commit ffb7276
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/linux/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (d *LinuxDeviceAPI) SensorWorkers() []func(context.Context, chan interface{
workers = append(workers, LoadAvgUpdater)
workers = append(workers, DiskUsageUpdater)
workers = append(workers, TimeUpdater)
workers = append(workers, ScreenLockUpdater)
return workers
}

Expand Down
92 changes: 92 additions & 0 deletions internal/linux/screenlock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// 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"
"github.com/rs/zerolog/log"
)

const (
screensaverDBusPath = "/org/freedesktop/ScreenSaver"
screensaverDBusInterface = "org.freedesktop.ScreenSaver"
)

type screenlock struct {
locked bool
}

func (l *screenlock) Name() string {
return "Screen Lock"
}

func (l *screenlock) ID() string {
return "screen_lock"
}

func (l *screenlock) Icon() string {
if l.locked {
return "mdi:eye-lock"
} else {
return "mdi:eye-lock-open"
}
}

func (l *screenlock) SensorType() hass.SensorType {
return hass.TypeBinary
}

func (l *screenlock) DeviceClass() hass.SensorDeviceClass {
return 0
}

func (l *screenlock) StateClass() hass.SensorStateClass {
return 0
}

func (l *screenlock) State() interface{} {
return l.locked
}

func (l *screenlock) Units() string {
return ""
}

func (l *screenlock) Category() string {
return ""
}

func (l *screenlock) Attributes() interface{} {
return nil
}

func ScreenLockUpdater(ctx context.Context, update chan interface{}) {
deviceAPI, err := device.FetchAPIFromContext(ctx)
if err != nil {
log.Debug().Err(err).Caller().
Msg("Could not connect to DBus.")
return
}
dbusAPI := device.GetAPIEndpoint[*bus](deviceAPI, "session")

NewBusRequest(dbusAPI).
Path(screensaverDBusPath).
Match([]dbus.MatchOption{
dbus.WithMatchObjectPath(screensaverDBusPath),
dbus.WithMatchInterface(screensaverDBusInterface),
}).
Event("org.freedesktop.ScreenSaver.ActiveChanged").
Handler(func(s *dbus.Signal) {
lock := new(screenlock)
lock.locked = s.Body[0].(bool)
update <- lock
}).
AddWatch(ctx)
}

0 comments on commit ffb7276

Please sign in to comment.