[modules/sensors] update to latest API
This commit is contained in:
parent
fd92b249de
commit
f2e0d3c0ee
1 changed files with 52 additions and 49 deletions
|
@ -5,9 +5,9 @@
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
* 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
|
||||||
be: "coretemp-isa-00000/Core 0/temp1_input" (defaults to "false")
|
be: 'coretemp-isa-00000/Core 0/temp1_input' (defaults to 'false')
|
||||||
* sensors.match: (fallback) Line to match against output of 'sensors -u' (default: temp1_input)
|
* sensors.match: (fallback) Line to match against output of 'sensors -u' (default: temp1_input)
|
||||||
* sensors.match_pattern: (fallback) Line to match against before temperature is read (no default)
|
* sensors.match_pattern: (fallback) Line to match against before temperature is read (no default)
|
||||||
* sensors.match_number: (fallback) which of the matches you want (default -1: last match).
|
* sensors.match_number: (fallback) which of the matches you want (default -1: last match).
|
||||||
|
@ -18,109 +18,112 @@ import re
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import bumblebee.util
|
|
||||||
import bumblebee.input
|
|
||||||
import bumblebee.output
|
|
||||||
import bumblebee.engine
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
class Module(bumblebee.engine.Module):
|
import core.module
|
||||||
def __init__(self, engine, config):
|
import core.widget
|
||||||
super(Module, self).__init__(engine, config,
|
import core.input
|
||||||
bumblebee.output.Widget(full_text=self.temperature))
|
|
||||||
self._temperature = "unknown"
|
import util.cli
|
||||||
self._mhz = "n/a"
|
import util.format
|
||||||
self._match_number = int(self.parameter("match_number", "-1"))
|
|
||||||
self._match_pattern = self.parameter("match_pattern", None)
|
class Module(core.module.Module):
|
||||||
self._pattern = re.compile(r"^\s*{}:\s*([\d.]+)$".format(self.parameter("match", "temp1_input")), re.MULTILINE)
|
def __init__(self, config, theme):
|
||||||
self._json = bumblebee.util.asbool(self.parameter("json", "false"))
|
super().__init__(config, theme, core.widget.Widget(self.temperature))
|
||||||
self._freq = bumblebee.util.asbool(self.parameter("show_freq", "true"))
|
|
||||||
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd="xsensors")
|
self._temperature = 'unknown'
|
||||||
|
self._mhz = 'n/a'
|
||||||
|
self._match_number = int(self.parameter('match_number', '-1'))
|
||||||
|
self._match_pattern = self.parameter('match_pattern', None)
|
||||||
|
self._pattern = re.compile(r'^\s*{}:\s*([\d.]+)$'.format(self.parameter('match', 'temp1_input')), re.MULTILINE)
|
||||||
|
self._json = util.format.asbool(self.parameter('json', False))
|
||||||
|
self._freq = util.format.asbool(self.parameter('show_freq', True))
|
||||||
|
core.input.register(self, button=core.input.LEFT_MOUSE, cmd='xsensors')
|
||||||
self.determine_method()
|
self.determine_method()
|
||||||
|
|
||||||
def determine_method(self):
|
def determine_method(self):
|
||||||
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
|
self.use_sensors = False # use thermal zone
|
||||||
else:
|
else:
|
||||||
# try to use output of sensors -u
|
# try to use output of sensors -u
|
||||||
try:
|
try:
|
||||||
output = bumblebee.util.execute("sensors -u")
|
output = util.cli.execute('sensors -u')
|
||||||
self.use_sensors = True
|
self.use_sensors = True
|
||||||
log.debug("Sensors command available")
|
log.debug('Sensors command available')
|
||||||
except FileNotFoundError as e:
|
except FileNotFoundError as e:
|
||||||
log.info("Sensors command not available, using /sys/class/thermal/thermal_zone*/")
|
log.info('Sensors command not available, using /sys/class/thermal/thermal_zone*/')
|
||||||
self.use_sensors = False
|
self.use_sensors = False
|
||||||
|
|
||||||
def _get_temp_from_sensors(self):
|
def _get_temp_from_sensors(self):
|
||||||
if self._json == True:
|
if self._json == True:
|
||||||
try:
|
try:
|
||||||
output = json.loads(bumblebee.util.execute("sensors -j"))
|
output = json.loads(util.cli.execute('sensors -j'))
|
||||||
for key in self.parameter("path").split("/"):
|
for key in self.parameter('path').split('/'):
|
||||||
output = output[key]
|
output = output[key]
|
||||||
return int(float(output))
|
return int(float(output))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error("unable to read sensors: {}".format(str(e)))
|
logging.error('unable to read sensors: {}'.format(str(e)))
|
||||||
return "unknown"
|
return 'unknown'
|
||||||
else:
|
else:
|
||||||
output = bumblebee.util.execute("sensors -u")
|
output = util.cli.execute('sensors -u')
|
||||||
if self._match_pattern:
|
if self._match_pattern:
|
||||||
temp_pattern = self.parameter("match", "temp1_input")
|
temp_pattern = self.parameter('match', 'temp1_input')
|
||||||
match = re.search(r"{}.+{}:\s*([\d.]+)$".format(self._match_pattern, temp_pattern), output.replace("\n", ""))
|
match = re.search(r'{}.+{}:\s*([\d.]+)$'.format(self._match_pattern, temp_pattern), output.replace('\n', ''))
|
||||||
if match:
|
if match:
|
||||||
return int(float(match.group(1)))
|
return int(float(match.group(1)))
|
||||||
else:
|
else:
|
||||||
return "unknown"
|
return 'unknown'
|
||||||
match = self._pattern.findall(output)
|
match = self._pattern.findall(output)
|
||||||
if match:
|
if match:
|
||||||
return int(float(match[self._match_number]))
|
return int(float(match[self._match_number]))
|
||||||
return "unknown"
|
return 'unknown'
|
||||||
|
|
||||||
def get_temp(self):
|
def get_temp(self):
|
||||||
if self.use_sensors:
|
if self.use_sensors:
|
||||||
temperature = self._get_temp_from_sensors()
|
temperature = self._get_temp_from_sensors()
|
||||||
log.debug("Retrieve temperature from sensors -u")
|
log.debug('Retrieve temperature from sensors -u')
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
temperature = open(self.parameter("path", "/sys/class/thermal/thermal_zone0/temp")).read()[:2]
|
temperature = open(self.parameter('path', '/sys/class/thermal/thermal_zone0/temp')).read()[:2]
|
||||||
log.debug("retrieved temperature from /sys/class/")
|
log.debug('retrieved temperature from /sys/class/')
|
||||||
# TODO: Iterate through all thermal zones to determine the correct one and use its value
|
# 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
|
# https://unix.stackexchange.com/questions/304845/discrepancy-between-number-of-cores-and-thermal-zones-in-sys-class-thermal
|
||||||
|
|
||||||
except IOError:
|
except IOError:
|
||||||
temperature = "unknown"
|
temperature = 'unknown'
|
||||||
log.info("Can not determine temperature, please install lm-sensors")
|
log.info('Can not determine temperature, please install lm-sensors')
|
||||||
|
|
||||||
return temperature
|
return temperature
|
||||||
|
|
||||||
def get_mhz(self):
|
def get_mhz(self):
|
||||||
mhz = None
|
mhz = None
|
||||||
try:
|
try:
|
||||||
output = open("/sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq").read()
|
output = open('/sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq').read()
|
||||||
mhz = int(float(output)/1000.0)
|
mhz = int(float(output)/1000.0)
|
||||||
except:
|
except:
|
||||||
output = open("/proc/cpuinfo").read()
|
output = open('/proc/cpuinfo').read()
|
||||||
m = re.search(r"cpu MHz\s+:\s+(\d+)", output)
|
m = re.search(r'cpu MHz\s+:\s+(\d+)', output)
|
||||||
if m:
|
if m:
|
||||||
mhz = int(m.group(1))
|
mhz = int(m.group(1))
|
||||||
else:
|
else:
|
||||||
m = re.search(r"BogoMIPS\s+:\s+(\d+)", output)
|
m = re.search(r'BogoMIPS\s+:\s+(\d+)', output)
|
||||||
if m:
|
if m:
|
||||||
return "{} BogoMIPS".format(int(m.group(1)))
|
return '{} BogoMIPS'.format(int(m.group(1)))
|
||||||
if not mhz:
|
if not mhz:
|
||||||
return "n/a"
|
return 'n/a'
|
||||||
|
|
||||||
if mhz < 1000:
|
if mhz < 1000:
|
||||||
return "{} MHz".format(mhz)
|
return '{} MHz'.format(mhz)
|
||||||
else:
|
else:
|
||||||
return "{:0.01f} GHz".format(float(mhz)/1000.0)
|
return '{:0.01f} GHz'.format(float(mhz)/1000.0)
|
||||||
|
|
||||||
def temperature(self, _):
|
def temperature(self, _):
|
||||||
if self._freq:
|
if self._freq:
|
||||||
return u"{}°c @ {}".format(self._temperature, self._mhz)
|
return u'{}°c @ {}'.format(self._temperature, self._mhz)
|
||||||
else:
|
else:
|
||||||
return u"{}°c".format(self._temperature)
|
return u'{}°c'.format(self._temperature)
|
||||||
def update(self, widgets):
|
|
||||||
|
def update(self):
|
||||||
self._temperature = self.get_temp()
|
self._temperature = self.get_temp()
|
||||||
if self._freq:
|
if self._freq:
|
||||||
self._mhz = self.get_mhz()
|
self._mhz = self.get_mhz()
|
||||||
|
|
Loading…
Reference in a new issue