From a31226924050bd880c4cb4b1437e9923dbd27458 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sat, 22 Jul 2017 11:44:21 +0200 Subject: [PATCH] [modules/sensors] Add fallback for temperature retrieval If /sys/class/ data cannot be read, fall back to "sensors -u", by popular demand :P see #145 --- README.md | 1 + bumblebee/modules/sensors.py | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index edb6930..eef5063 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,7 @@ Modules and commandline utilities are only required for modules, the core itself * bluez / blueman (for module 'bluetooth') * dbus-send (for module 'bluetooth') * nvidia-smi (for module 'nvidiagpu') +* sensors (for module 'sensors', as fallback) # Examples Here are some screenshots for all themes that currently exist: diff --git a/bumblebee/modules/sensors.py b/bumblebee/modules/sensors.py index b2e68fb..7011ae2 100644 --- a/bumblebee/modules/sensors.py +++ b/bumblebee/modules/sensors.py @@ -5,27 +5,45 @@ Parameters: * sensors.path: path to temperature file (default /sys/class/thermal/thermal_zone0/temp). + * sensors.match: (fallback) Line to match against output of 'sensors -u' (default: temp1_input) + * sensors.match_number: (fallback) which of the matches you want (default -1: last match). + """ import re +import logging +import bumblebee.util import bumblebee.input import bumblebee.output import bumblebee.engine +log = logging.getLogger(__name__) + class Module(bumblebee.engine.Module): def __init__(self, engine, config): super(Module, self).__init__(engine, config, bumblebee.output.Widget(full_text=self.temperature)) self._temperature = "unknown" self._mhz = "n/a" + 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") + def _get_temp_from_sensors(self): + output = bumblebee.util.execute("sensors -u") + match = self._pattern.findall(output) + if match: + return int(float(match[self._match_number])) + 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: - temperature = "unknown" + temperature = self._get_temp_from_sensors() + log.debug("retrieved temperature from 'sensors -u'") return temperature def get_mhz( self ):