Skip to content

Commit

Permalink
Set CPU/memory sensors to unavailable while suspended or powered off (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
slovdahl authored Sep 24, 2024
1 parent 1b78a30 commit a8654e0
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
36 changes: 36 additions & 0 deletions halinuxcompanion/sensors/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os

load_average: bool = False
allow_update: bool = True

Cpu = Sensor()
Cpu.config_name = "cpu"
Expand All @@ -25,7 +26,38 @@
Cpu.unit_of_measurement = "%"


async def on_prepare_for_sleep(self, v):
"""Handler for system sleep and wake up from sleep events.
https://www.freedesktop.org/software/systemd/man/org.freedesktop.login1.html
:param v: True if going to sleep, False if waking up from it
"""
global allow_update
if v:
allow_update = False
self.state = "unavailable"
else:
allow_update = True


async def on_prepare_for_shutdown(self, v):
"""Handler for system shutdown/reboot.
https://www.freedesktop.org/software/systemd/man/org.freedesktop.login1.html
:param v: True if shutting down, False if powering on.
"""
global allow_update
if v:
allow_update = False
self.state = "unavailable"
else:
allow_update = True


def updater(self):
if not allow_update:
return

self.state = psutil.cpu_percent()
if load_average:
data = psutil.getloadavg()
Expand All @@ -35,3 +67,7 @@ def updater(self):


Cpu.updater = MethodType(updater, Cpu)
Cpu.signals = {
"system.login_on_prepare_for_sleep": on_prepare_for_sleep,
"system.login_on_prepare_for_shutdown": on_prepare_for_shutdown,
}
37 changes: 37 additions & 0 deletions halinuxcompanion/sensors/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from halinuxcompanion.sensor import Sensor
import psutil

allow_update: bool = True

Memory = Sensor()
Memory.config_name = "memory"
Memory.attributes = {
Expand All @@ -21,7 +23,38 @@
Memory.unit_of_measurement = "%"


async def on_prepare_for_sleep(self, v):
"""Handler for system sleep and wake up from sleep events.
https://www.freedesktop.org/software/systemd/man/org.freedesktop.login1.html
:param v: True if going to sleep, False if waking up from it
"""
global allow_update
if v:
allow_update = False
self.state = "unavailable"
else:
allow_update = True


async def on_prepare_for_shutdown(self, v):
"""Handler for system shutdown/reboot.
https://www.freedesktop.org/software/systemd/man/org.freedesktop.login1.html
:param v: True if shutting down, False if powering on.
"""
global allow_update
if v:
allow_update = False
self.state = "unavailable"
else:
allow_update = True


def updater(self):
if not allow_update:
return

data = psutil.virtual_memory()
self.state = round((data.total - data.available) / data.total * 100, 1)
self.attributes["total"] = data.total / 1024
Expand All @@ -31,3 +64,7 @@ def updater(self):


Memory.updater = MethodType(updater, Memory)
Memory.signals = {
"system.login_on_prepare_for_sleep": on_prepare_for_sleep,
"system.login_on_prepare_for_shutdown": on_prepare_for_shutdown,
}

0 comments on commit a8654e0

Please sign in to comment.