From e82297530d439c82f0d303d90142bb89e54f6144 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Thu, 11 Oct 2018 15:49:10 -0400 Subject: [PATCH] Add average CPU temp to i3status --- i3wrapper.py | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/i3wrapper.py b/i3wrapper.py index c0fc7d3..4b9c0cf 100755 --- a/i3wrapper.py +++ b/i3wrapper.py @@ -1,6 +1,7 @@ #!/usr/bin/env python -import sys import json +import subprocess +import sys _BITS = [] @@ -44,6 +45,43 @@ def _backlight(dev_dir='/sys/class/backlight/intel_backlight'): color = '#5599ff' return '☼:{}%'.format(pct), color +@_bit(7) +def _cputemp(): + sensors_job = subprocess.run( + ['sensors', '-j'], + check=True, capture_output=True, text=True + ) + parsed = json.loads(sensors_job.stdout) + found_core_temps = [] + + for key, value in parsed.items(): + if 'coretemp' not in key: + continue + if not hasattr(value, 'items'): + continue + + for subkey, subvalue in value.items(): + if 'Core' not in subkey: + continue + if not hasattr(subvalue, 'items'): + continue + + for subsubkey, subsubvalue in subvalue.items(): + if not subsubkey.endswith('_input'): + continue + found_core_temps.append(subsubvalue) + + avg_temp = float(sum(found_core_temps)) / len(found_core_temps) + color = None + + if avg_temp > 75: + color = '#ff0000' + if avg_temp >= 50: + color = '#ffaa00' + if avg_temp <= 25: + color = '#5599ff' + return 'T:{}°C'.format(avg_temp), color + def _print_line(message): sys.stdout.write(message + '\n')