Added levels settings to the battery module (#654)

* Added levels settings to the battery module

Specify text for custom levels of the battery charge

* Round up charge the same way it is displayed

percentage value used to determine text from levels setting should be
round up the same way as when it is displayed on the screen
This commit is contained in:
Егор 2018-12-03 14:41:02 +03:00 committed by enkore
parent 7e8da7686c
commit fde7b6bb78

View File

@ -1,3 +1,4 @@
import bisect
import configparser
import os
import re
@ -188,6 +189,15 @@ class BatteryChecker(IntervalModule):
("base_path", "Override the default base path for searching for batteries"),
("battery_prefix", "Override the default battery prefix"),
("status", "A dictionary mapping ('DPL', 'DIS', 'CHR', 'FULL') to alternative names"),
("levels",
"A dictionary mapping of charge levels to corresponding names. Let the keys be a < b < c < d. "
"Then the following intervals correspond to each value:\n"
" 0 -> status['DPL']\n"
" |0 < x <= a| -> levels[a]\n"
" |a < x <= b| -> levels[b]\n"
" |b < x <= c| -> levels[c]\n"
" |c < x <= d| -> levels[d]\n"
" |d < x <= 100| -> status['FULL']"),
("color", "The text color"),
("full_color", "The full color"),
("charging_color", "The charging color"),
@ -207,6 +217,7 @@ class BatteryChecker(IntervalModule):
"DIS": "DIS",
"FULL": "FULL",
}
levels = None
not_present_text = "Battery {battery_ident} not present"
alert = False
@ -362,7 +373,14 @@ class BatteryChecker(IntervalModule):
self.notification.update(title=title,
body=body)
fdict["status"] = self.status[fdict["status"]]
if self.levels and fdict['status'] == 'DIS':
self.levels.setdefault(0, self.status.get('DPL', 'DPL'))
self.levels.setdefault(100, self.status.get('FULL', 'FULL'))
keys = sorted(self.levels.keys())
index = bisect.bisect_left(keys, int(fdict['percentage']))
fdict["status"] = self.levels[keys[index]]
else:
fdict["status"] = self.status[fdict["status"]]
self.data = fdict
self.output = {