[modules/sensors2] incorporate latest changes

This commit is contained in:
tobi-wan-kenobi 2020-04-19 09:43:44 +02:00
parent 0a6f914232
commit 968df75945

View file

@ -1,6 +1,6 @@
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
'''Displays sensor temperature and CPU frequency """Displays sensor temperature and CPU frequency
Parameters: Parameters:
@ -10,13 +10,18 @@ Parameters:
* sensors2.showfan: Enable or disable fan display (default: true) * sensors2.showfan: Enable or disable fan display (default: true)
* sensors2.showother: Enable or display 'other' sensor readings (default: false) * sensors2.showother: Enable or display 'other' sensor readings (default: false)
* sensors2.showname: Enable or disable show of sensor name (default: false) * sensors2.showname: Enable or disable show of sensor name (default: false)
''' * sensors2.chip_include: Comma-separated list of chip to include (defaults to '' will include all by default, example: 'coretemp,bat')
* sensors2.chip_exclude:Comma separated list of chip to exclude (defaults to '' will exlude none by default)
* sensors2.field_include: Comma separated list of chip to include (defaults to '' will include all by default, example: 'temp,fan')
* sensors2.field_exclude: Comma separated list of chip to exclude (defaults to '' will exclude none by default)
* sensors2.chip_field_exclude: Comma separated list of chip field to exclude (defaults to '' will exclude none by default, example: 'coretemp-isa-0000.temp1,coretemp-isa-0000.fan1')
* sensors2.chip_field_include: Comma-separated list of adaper field to include (defaults to '' will include all by default)
"""
import re import re
import core.module import core.module
import core.widget import core.widget
import core.input
import util.cli import util.cli
import util.format import util.format
@ -27,9 +32,11 @@ class Module(core.module.Module):
self.__chip = self.parameter('chip', '') self.__chip = self.parameter('chip', '')
self.__data = {} self.__data = {}
self.__update()
self.widgets(self.__create_widgets())
def update(self): def update(self):
self.widgets(self.__create_widgets())
self.__update() self.__update()
for widget in self.widgets(): for widget in self.widgets():
self.__update_widget(widget) self.__update_widget(widget)
@ -37,7 +44,7 @@ class Module(core.module.Module):
def state(self, widget): def state(self, widget):
widget_type = widget.get('type', '') widget_type = widget.get('type', '')
try: try:
data = self._data[widget.get('adapter')][widget.get('package')][widget.get('field')] data = self.__data[widget.get('adapter')][widget.get('package')][widget.get('field')]
if 'crit' in data and float(data['input']) > float(data['crit']): if 'crit' in data and float(data['input']) > float(data['crit']):
return ['critical', widget_type] return ['critical', widget_type]
if 'max' in data and float(data['input']) > float(data['max']): if 'max' in data and float(data['input']) > float(data['max']):
@ -51,6 +58,12 @@ class Module(core.module.Module):
show_temp = util.format.asbool(self.parameter('showtemp', True)) show_temp = util.format.asbool(self.parameter('showtemp', True))
show_fan = util.format.asbool(self.parameter('showfan', True)) show_fan = util.format.asbool(self.parameter('showfan', True))
show_other = util.format.asbool(self.parameter('showother', False)) show_other = util.format.asbool(self.parameter('showother', False))
include_chip = tuple(filter(len, util.format.aslist(self.parameter('chip_include', ''))))
exclude_chip = tuple(filter(len, util.format.aslist(self.parameter('chip_exclude', ''))))
include_field = tuple(filter(len, util.format.aslist(self.parameter('field_include', ''))))
exclude_field = tuple(filter(len, util.format.aslist(self.parameter('field_exclude', ''))))
include_chip_field = tuple(filter(len, util.format.aslist(self.parameter('chip_field_include', ''))))
exclude_chip_field = tuple(filter(len, util.format.aslist(self.parameter('chip_field_exclude', ''))))
if util.format.asbool(self.parameter('showcpu', True)): if util.format.asbool(self.parameter('showcpu', True)):
widget = core.widget.Widget(full_text=self.__cpu) widget = core.widget.Widget(full_text=self.__cpu)
@ -58,15 +71,50 @@ class Module(core.module.Module):
widgets.append(widget) widgets.append(widget)
for adapter in self.__data: for adapter in self.__data:
if include_chip or exclude_chip:
if include_chip:
if all([chip not in adapter for chip in include_chip]):
continue
else:
if any([chip in adapter for chip in exclude_chip]):
continue
if include_chip_field:
try:
if all([i.split('.')[0] not in adapter for i in include_chip_field]):
continue
except:
pass
for package in self.__data[adapter]: for package in self.__data[adapter]:
if util.format.asbool(self.parameter('showname', False)): if util.format.asbool(self.parameter('showname', False)):
widget = core.widget.Widget(full_text=package) widget = core.widget.Widget(full_text=package)
widget.set('data', self._data[adapter][package]) widget.set('data', self.__data[adapter][package])
widget.set('package', package) widget.set('package', package)
widget.set('field', '') widget.set('field', '')
widget.set('adapter', adapter) widget.set('adapter', adapter)
widgets.append(widget) widgets.append(widget)
for field in self.__data[adapter][package]: for field in self.__data[adapter][package]:
if include_field or exclude_field:
if include_field:
if all([included not in field for included in include_field]):
continue
else:
if any([excluded in field for excluded in exclude_field]):
continue
try:
if include_chip_field or exclude_chip_field:
if include_chip_field:
if all([i.split('.')[1] not in field for i in include_chip_field if i.split('.')[0] in adapter]):
continue
else:
if any([i.split('.')[1] in field for i in exclude_chip_field if i.split('.')[0] in adapter]):
continue
except:
pass
widget = core.widget.Widget() widget = core.widget.Widget()
widget.set('package', package) widget.set('package', package)
widget.set('field', field) widget.set('field', field)
@ -97,7 +145,7 @@ class Module(core.module.Module):
widget.full_text(u'{:0.0f}'.format(data['input'])) widget.full_text(u'{:0.0f}'.format(data['input']))
def __update(self): def __update(self):
output = util.cli.execute('sensors -u {}'.format(self.__chip)) output = util.cli.execute('sensors -u {}'.format(self.__chip), ignore_errors=True)
self.__data = self.__parse(output) self.__data = self.__parse(output)
def __parse(self, data): def __parse(self, data):
@ -109,8 +157,8 @@ class Module(core.module.Module):
if 'Adapter' in line: if 'Adapter' in line:
# new adapter # new adapter
line = line.replace('Adapter: ', '') line = line.replace('Adapter: ', '')
adapter = '{} {}'.format(chip, line) output[chip + ' ' + line] = {}
output[adapter] = {} adapter = chip + ' ' + line
chip = line #default - line before adapter is always the chip chip = line #default - line before adapter is always the chip
if not adapter: continue if not adapter: continue
key, value = (line.split(':') + ['', ''])[:2] key, value = (line.split(':') + ['', ''])[:2]