From 9ef3da8b881246f40114754a14f67fd5dcce2c9b Mon Sep 17 00:00:00 2001 From: soenke Date: Mon, 30 Jul 2018 18:25:37 +0200 Subject: [PATCH] [modules/sensors] Prefer sensors command if available, solves #275 --- bumblebee/modules/sensors.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/bumblebee/modules/sensors.py b/bumblebee/modules/sensors.py index ed518f5..1d483d2 100644 --- a/bumblebee/modules/sensors.py +++ b/bumblebee/modules/sensors.py @@ -29,6 +29,16 @@ class Module(bumblebee.engine.Module): self._match_number = int(self.parameter("match_number", "-1")) self._pattern = re.compile(r"^\s*{}:\s*([\d.]+)$".format(self.parameter("match", "temp1_input")), re.MULTILINE) engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd="xsensors") + self.determine_method() + + def determine_method(self): + try: + output = bumblebee.util.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): output = bumblebee.util.execute("sensors -u") @@ -38,12 +48,20 @@ class Module(bumblebee.engine.Module): return "unknown" def get_temp(self): - try: - temperature = open(self.parameter("path", "/sys/class/thermal/thermal_zone0/temp")).read()[:2] - log.debug("retrieved temperature from /sys/class/") - except IOError: + if self.use_sensors: temperature = self._get_temp_from_sensors() - log.debug("retrieved temperature from 'sensors -u'") + log.debug("Retrieve temperature from sensors -u") + else: + try: + temperature = open(self.parameter("path", "/sys/class/thermal/thermal_zone0/temp")).read()[:2] + 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 def get_mhz(self):