From 0dc6a95ac284f105b534a546b5e244b161c84694 Mon Sep 17 00:00:00 2001 From: alexcoder04 Date: Thu, 21 Oct 2021 13:45:40 +0200 Subject: [PATCH] [modules/sensors] auto-determine the correct thermal zone --- bumblebee_status/modules/contrib/sensors.py | 69 +++++++++++++-------- docs/modules.rst | 2 + 2 files changed, 44 insertions(+), 27 deletions(-) diff --git a/bumblebee_status/modules/contrib/sensors.py b/bumblebee_status/modules/contrib/sensors.py index 8164f00..d023d6a 100644 --- a/bumblebee_status/modules/contrib/sensors.py +++ b/bumblebee_status/modules/contrib/sensors.py @@ -18,6 +18,7 @@ contributed by `mijoharas `_ - many thanks! """ import re +import os import json import logging @@ -49,19 +50,25 @@ 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: - # try to use output of sensors -u - try: - output = util.cli.execute("sensors -u") - self.use_sensors = True - log.debug("Sensors command available") - except FileNotFoundError as e: - log.info( - "Sensors command not available, using /sys/class/thermal/thermal_zone*/" - ) - self.use_sensors = False + self.use_sensors = False # use thermal zone + return + # try to use output of sensors -u + try: + output = util.cli.execute("sensors -u") + self.use_sensors = True + log.debug("Sensors command available") + except FileNotFoundError as e: + log.info( + "Sensors command not available, using /sys/class/thermal/thermal_zone*/" + ) + self.use_sensors = False def _get_temp_from_sensors(self): if self._json == True: @@ -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: - 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 - - except IOError: - temperature = "unknown" - log.info("Can not determine temperature, please install lm-sensors") - - return temperature + return self._get_temp_from_sensors() + try: + 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: + log.info("Can not determine temperature, please install lm-sensors") + return "unknown" def get_mhz(self): mhz = None diff --git a/docs/modules.rst b/docs/modules.rst index 5b9b075..ded47dc 100644 --- a/docs/modules.rst +++ b/docs/modules.rst @@ -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. //.../), for example, path could