[modules/sensors] use util.format.asbool() + auto-check only if no path is specified

This commit is contained in:
alexcoder04 2021-10-21 14:43:15 +02:00
parent 0dc6a95ac2
commit 6b31cdb698
2 changed files with 20 additions and 20 deletions

View file

@ -4,6 +4,7 @@
"""Displays sensor temperature """Displays sensor temperature
Parameters: Parameters:
* sensors.use_sensors: whether to use the sensors command
* 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
@ -47,28 +48,25 @@ class Module(core.module.Module):
self._json = util.format.asbool(self.parameter("json", False)) self._json = util.format.asbool(self.parameter("json", False))
self._freq = util.format.asbool(self.parameter("show_freq", True)) self._freq = util.format.asbool(self.parameter("show_freq", True))
core.input.register(self, button=core.input.LEFT_MOUSE, cmd="xsensors") core.input.register(self, button=core.input.LEFT_MOUSE, cmd="xsensors")
self.determine_method() self.use_sensors = self.determine_method()
def determine_method(self): def determine_method(self):
if self.parameter("use_sensors") == "True": if util.format.asbool(self.parameter("use_sensors")) == True:
self.use_sensors = True return True
return if util.format.asbool(self.parameter("use_sensors")) == False:
if self.parameter("use_sensors") == "False": return 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 return False
return
# try to use output of sensors -u # try to use output of sensors -u
try: try:
output = util.cli.execute("sensors -u") _ = util.cli.execute("sensors -u")
self.use_sensors = True
log.debug("Sensors command available") log.debug("Sensors command available")
return True
except FileNotFoundError as e: except FileNotFoundError as e:
log.info( log.info(
"Sensors command not available, using /sys/class/thermal/thermal_zone*/" "Sensors command not available, using /sys/class/thermal/thermal_zone*/"
) )
self.use_sensors = False return False
def _get_temp_from_sensors(self): def _get_temp_from_sensors(self):
if self._json == True: if self._json == True:
@ -107,12 +105,13 @@ class Module(core.module.Module):
if self.parameter("path") is not None: if self.parameter("path") is not None:
path = self.parameter("path") path = self.parameter("path")
# find the thermal zone that provides cpu temperature # find the thermal zone that provides cpu temperature
for zone in os.listdir("/sys/class/thermal"): else:
if not zone.startswith("thermal_zone"): for zone in os.listdir("/sys/class/thermal"):
continue if not zone.startswith("thermal_zone"):
if open(f"/sys/class/thermal/{zone}/type").read().strip() != "x86_pkg_temp": continue
continue if open(f"/sys/class/thermal/{zone}/type").read().strip() != "x86_pkg_temp":
path = f"/sys/class/thermal/{zone}/temp" continue
path = f"/sys/class/thermal/{zone}/temp"
# use zone 0 as fallback # use zone 0 as fallback
if path is None: if path is None:
log.info("Can not determine temperature path, using thermal_zone0") log.info("Can not determine temperature path, using thermal_zone0")

View file

@ -1195,8 +1195,9 @@ sensors
Displays sensor temperature Displays sensor temperature
Parameters: Parameters:
* sensors.use_sensors (True/False): whether to use the 'sensors' command. * sensors.use_sensors: whether to use the 'sensors' command.
If set to 'False', the sysfs-interface at '/sys/class/thermal' is used If set to 'false', the sysfs-interface at '/sys/class/thermal' is used.
If not set, 'sensors' will be used if available.
* 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