[modules/sensors] auto-determine the correct thermal zone

This commit is contained in:
alexcoder04 2021-10-21 13:45:40 +02:00
parent 618a22c122
commit 0dc6a95ac2
2 changed files with 44 additions and 27 deletions

View file

@ -18,6 +18,7 @@ contributed by `mijoharas <https://github.com/mijoharas>`_ - many thanks!
""" """
import re import re
import os
import json import json
import logging import logging
@ -49,9 +50,15 @@ class Module(core.module.Module):
self.determine_method() self.determine_method()
def determine_method(self): def determine_method(self):
if self.parameter("use_sensors") == "True":
self.use_sensors = True
return
if self.parameter("use_sensors") == "False":
self.use_sensors = False
return
if self.parameter("path") != None and self._json == False: if self.parameter("path") != None and self._json == False:
self.use_sensors = False # use thermal zone self.use_sensors = False # use thermal zone
else: return
# try to use output of sensors -u # try to use output of sensors -u
try: try:
output = util.cli.execute("sensors -u") output = util.cli.execute("sensors -u")
@ -92,22 +99,30 @@ class Module(core.module.Module):
def get_temp(self): def get_temp(self):
if self.use_sensors: if self.use_sensors:
temperature = self._get_temp_from_sensors()
log.debug("Retrieve temperature from sensors -u") log.debug("Retrieve temperature from sensors -u")
else: return self._get_temp_from_sensors()
try: try:
temperature = open( path = None
self.parameter("path", "/sys/class/thermal/thermal_zone0/temp") # use path provided by the user
).read().strip() if self.parameter("path") is not None:
log.debug("retrieved temperature from /sys/class/") path = self.parameter("path")
# TODO: Iterate through all thermal zones to determine the correct one and use its value # find the thermal zone that provides cpu temperature
# https://unix.stackexchange.com/questions/304845/discrepancy-between-number-of-cores-and-thermal-zones-in-sys-class-thermal for zone in os.listdir("/sys/class/thermal"):
if not zone.startswith("thermal_zone"):
continue
if open(f"/sys/class/thermal/{zone}/type").read().strip() != "x86_pkg_temp":
continue
path = f"/sys/class/thermal/{zone}/temp"
# use zone 0 as fallback
if path is None:
log.info("Can not determine temperature path, using thermal_zone0")
path = "/sys/class/thermal/thermal_zone0/temp"
log.debug(f"retrieving temperature from {path}")
# the values are t°C * 1000, so divide by 1000
return str(int(open(path).read()) / 1000)
except IOError: except IOError:
temperature = "unknown"
log.info("Can not determine temperature, please install lm-sensors") log.info("Can not determine temperature, please install lm-sensors")
return "unknown"
return temperature
def get_mhz(self): def get_mhz(self):
mhz = None mhz = None

View file

@ -1195,6 +1195,8 @@ sensors
Displays sensor temperature Displays sensor temperature
Parameters: Parameters:
* sensors.use_sensors (True/False): whether to use the 'sensors' command.
If set to 'False', the sysfs-interface at '/sys/class/thermal' is used
* sensors.path: path to temperature file (default /sys/class/thermal/thermal_zone0/temp). * sensors.path: path to temperature file (default /sys/class/thermal/thermal_zone0/temp).
* sensors.json: if set to 'true', interpret sensors.path as JSON 'path' in the output * sensors.json: if set to 'true', interpret sensors.path as JSON 'path' in the output
of 'sensors -j' (i.e. <key1>/<key2>/.../<value>), for example, path could of 'sensors -j' (i.e. <key1>/<key2>/.../<value>), for example, path could