[modules/sensors] Add parameter "match_pattern" to find correct adapter

Instead of providing match_number, it is now possible to specify a
second pattern that matches directly *before* the "match" value.

fixes #308
This commit is contained in:
Tobias Witek 2018-11-08 18:56:26 +01:00
parent fcc9c39f4d
commit 2f7931753e

View file

@ -9,6 +9,7 @@ Parameters:
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_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).
""" """
@ -31,6 +32,7 @@ class Module(bumblebee.engine.Module):
self._temperature = "unknown" self._temperature = "unknown"
self._mhz = "n/a" self._mhz = "n/a"
self._match_number = int(self.parameter("match_number", "-1")) 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._pattern = re.compile(r"^\s*{}:\s*([\d.]+)$".format(self.parameter("match", "temp1_input")), re.MULTILINE)
self._json = bumblebee.util.asbool(self.parameter("json", "false")) self._json = bumblebee.util.asbool(self.parameter("json", "false"))
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd="xsensors") engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd="xsensors")
@ -61,6 +63,13 @@ class Module(bumblebee.engine.Module):
return "unknown" return "unknown"
else: else:
output = bumblebee.util.execute("sensors -u") output = bumblebee.util.execute("sensors -u")
if self._match_pattern:
temp_pattern = self.parameter("match", "temp1_input")
match = re.search(r"{}.+{}".format(self._match_pattern, temp_pattern), output.replace("\n", ""))
if match:
return int(float(match.group(1)))
else:
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]))