Merge pull request #825 from alexcoder04/main

[modules/sensors] auto-determine the correct thermal zone
This commit is contained in:
tobi-wan-kenobi 2021-10-21 14:07:42 +02:00 committed by GitHub
commit 9106ec9c8f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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 os
import json
import logging
@ -49,9 +50,15 @@ class Module(core.module.Module):
self.determine_method()
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:
self.use_sensors = False # use thermal zone
else:
return
# try to use output of sensors -u
try:
output = util.cli.execute("sensors -u")
@ -92,22 +99,30 @@ class Module(core.module.Module):
def get_temp(self):
if self.use_sensors:
temperature = self._get_temp_from_sensors()
log.debug("Retrieve temperature from sensors -u")
else:
return self._get_temp_from_sensors()
try:
temperature = open(
self.parameter("path", "/sys/class/thermal/thermal_zone0/temp")
).read().strip()
log.debug("retrieved temperature from /sys/class/")
# TODO: Iterate through all thermal zones to determine the correct one and use its value
# https://unix.stackexchange.com/questions/304845/discrepancy-between-number-of-cores-and-thermal-zones-in-sys-class-thermal
path = None
# use path provided by the user
if self.parameter("path") is not None:
path = self.parameter("path")
# find the thermal zone that provides cpu temperature
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:
temperature = "unknown"
log.info("Can not determine temperature, please install lm-sensors")
return temperature
return "unknown"
def get_mhz(self):
mhz = None

View file

@ -1195,6 +1195,8 @@ sensors
Displays sensor temperature
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.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