2020-04-26 16:41:50 +02:00
|
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Displays sensor temperature
|
|
|
|
|
|
|
|
Parameters:
|
2021-10-21 14:43:15 +02:00
|
|
|
* sensors.use_sensors: whether to use the sensors command
|
2020-04-26 16:41:50 +02:00
|
|
|
* sensors.path: path to temperature file (default /sys/class/thermal/thermal_zone0/temp).
|
2020-04-26 16:45:19 +02:00
|
|
|
* sensors.json: if set to 'true', interpret sensors.path as JSON 'path' in the output
|
2020-05-05 20:33:27 +02:00
|
|
|
of 'sensors -j' (i.e. <key1>/<key2>/.../<value>), for example, path could
|
|
|
|
be: 'coretemp-isa-00000/Core 0/temp1_input' (defaults to 'false')
|
2020-04-26 16:41:50 +02:00
|
|
|
* 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_number: (fallback) which of the matches you want (default -1: last match).
|
|
|
|
* sensors.show_freq: whether to show CPU frequency. (default: true)
|
2020-05-08 20:58:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
contributed by `mijoharas <https://github.com/mijoharas>`_ - many thanks!
|
2020-04-26 16:41:50 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import re
|
2021-10-21 13:45:40 +02:00
|
|
|
import os
|
2020-04-26 16:41:50 +02:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2020-04-26 16:45:19 +02:00
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
import core.input
|
|
|
|
|
|
|
|
import util.cli
|
|
|
|
import util.format
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-26 16:45:19 +02:00
|
|
|
class Module(core.module.Module):
|
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.temperature))
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
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")
|
2021-10-21 14:43:15 +02:00
|
|
|
self.use_sensors = self.determine_method()
|
2020-04-26 16:41:50 +02:00
|
|
|
|
|
|
|
def determine_method(self):
|
2021-10-21 14:43:15 +02:00
|
|
|
if util.format.asbool(self.parameter("use_sensors")) == True:
|
|
|
|
return True
|
|
|
|
if util.format.asbool(self.parameter("use_sensors")) == False:
|
|
|
|
return False
|
2020-05-03 11:15:52 +02:00
|
|
|
if self.parameter("path") != None and self._json == False:
|
2021-10-21 14:43:15 +02:00
|
|
|
return False
|
2021-10-21 13:45:40 +02:00
|
|
|
# try to use output of sensors -u
|
|
|
|
try:
|
2021-10-21 14:43:15 +02:00
|
|
|
_ = util.cli.execute("sensors -u")
|
2021-10-21 13:45:40 +02:00
|
|
|
log.debug("Sensors command available")
|
2021-10-21 14:43:15 +02:00
|
|
|
return True
|
2021-10-21 13:45:40 +02:00
|
|
|
except FileNotFoundError as e:
|
|
|
|
log.info(
|
|
|
|
"Sensors command not available, using /sys/class/thermal/thermal_zone*/"
|
|
|
|
)
|
2021-10-21 14:43:15 +02:00
|
|
|
return False
|
2020-04-26 16:41:50 +02:00
|
|
|
|
|
|
|
def _get_temp_from_sensors(self):
|
|
|
|
if self._json == True:
|
|
|
|
try:
|
2020-05-03 11:15:52 +02:00
|
|
|
output = json.loads(util.cli.execute("sensors -j"))
|
|
|
|
for key in self.parameter("path").split("/"):
|
2020-04-26 16:41:50 +02:00
|
|
|
output = output[key]
|
|
|
|
return int(float(output))
|
|
|
|
except Exception as e:
|
2020-05-03 11:15:52 +02:00
|
|
|
logging.error("unable to read sensors: {}".format(str(e)))
|
|
|
|
return "unknown"
|
2020-04-26 16:41:50 +02:00
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
output = util.cli.execute("sensors -u")
|
2020-04-26 16:41:50 +02:00
|
|
|
if self._match_pattern:
|
2020-05-03 11:15:52 +02:00
|
|
|
temp_pattern = self.parameter("match", "temp1_input")
|
|
|
|
match = re.search(
|
|
|
|
r"{}.+{}:\s*([\d.]+)$".format(self._match_pattern, temp_pattern),
|
|
|
|
output.replace("\n", ""),
|
|
|
|
)
|
2020-04-26 16:41:50 +02:00
|
|
|
if match:
|
|
|
|
return int(float(match.group(1)))
|
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
return "unknown"
|
2020-04-26 16:41:50 +02:00
|
|
|
match = self._pattern.findall(output)
|
|
|
|
if match:
|
|
|
|
return int(float(match[self._match_number]))
|
2020-05-03 11:15:52 +02:00
|
|
|
return "unknown"
|
2020-04-26 16:41:50 +02:00
|
|
|
|
|
|
|
def get_temp(self):
|
|
|
|
if self.use_sensors:
|
2020-05-03 11:15:52 +02:00
|
|
|
log.debug("Retrieve temperature from sensors -u")
|
2021-10-21 13:45:40 +02:00
|
|
|
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
|
2021-10-21 14:43:15 +02:00
|
|
|
else:
|
|
|
|
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"
|
2021-10-21 13:45:40 +02:00
|
|
|
# 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"
|
2020-04-26 16:41:50 +02:00
|
|
|
|
|
|
|
def get_mhz(self):
|
|
|
|
mhz = None
|
|
|
|
try:
|
2020-05-03 11:15:52 +02:00
|
|
|
output = open(
|
|
|
|
"/sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq"
|
|
|
|
).read()
|
|
|
|
mhz = int(float(output) / 1000.0)
|
2020-04-26 16:41:50 +02:00
|
|
|
except:
|
2020-05-03 11:15:52 +02:00
|
|
|
output = open("/proc/cpuinfo").read()
|
|
|
|
m = re.search(r"cpu MHz\s+:\s+(\d+)", output)
|
2020-04-26 16:41:50 +02:00
|
|
|
if m:
|
|
|
|
mhz = int(m.group(1))
|
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
m = re.search(r"BogoMIPS\s+:\s+(\d+)", output)
|
2020-04-26 16:41:50 +02:00
|
|
|
if m:
|
2020-05-03 11:15:52 +02:00
|
|
|
return "{} BogoMIPS".format(int(m.group(1)))
|
2020-04-26 16:41:50 +02:00
|
|
|
if not mhz:
|
2020-05-03 11:15:52 +02:00
|
|
|
return "n/a"
|
2020-04-26 16:41:50 +02:00
|
|
|
|
|
|
|
if mhz < 1000:
|
2020-05-03 11:15:52 +02:00
|
|
|
return "{} MHz".format(mhz)
|
2020-04-26 16:41:50 +02:00
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
return "{:0.01f} GHz".format(float(mhz) / 1000.0)
|
2020-04-26 16:41:50 +02:00
|
|
|
|
|
|
|
def temperature(self, _):
|
|
|
|
if self._freq:
|
2020-05-03 11:15:52 +02:00
|
|
|
return "{}°c @ {}".format(self._temperature, self._mhz)
|
2020-04-26 16:41:50 +02:00
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
return "{}°c".format(self._temperature)
|
2020-04-26 16:45:19 +02:00
|
|
|
|
|
|
|
def update(self):
|
2020-04-26 16:41:50 +02:00
|
|
|
self._temperature = self.get_temp()
|
|
|
|
if self._freq:
|
|
|
|
self._mhz = self.get_mhz()
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-26 16:41:50 +02:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|