Improve battery script

- try to find battery name
- mension producer and battery name
- accept missing to discharge
This commit is contained in:
2022-07-19 10:44:13 +02:00
parent cd62f62f1b
commit 2df17cb508

View File

@@ -3,14 +3,19 @@
import sys import sys
import re import re
from sys import stdout from sys import stdout
from os import path from os import path, environ
from os.path import join, isdir
from collections import namedtuple, OrderedDict from collections import namedtuple, OrderedDict
from termcolor import colored from termcolor import colored
POWER_CLASS = '/sys/class/power_supply' POWER_CLASS = '/sys/class/power_supply'
BATTERY = 'cw2015-battery' BATTERIES = (
UEVENT = path.join(POWER_CLASS, BATTERY, 'uevent') environ.get('BATTERY', 'BAT0'),
'cw2015-battery'
)
BATTERY = list(filter(lambda bat: isdir(join(POWER_CLASS, bat)), BATTERIES))[0]
UEVENT = join(POWER_CLASS, BATTERY, 'uevent')
DISCHARNGING = 'Discharging' DISCHARNGING = 'Discharging'
@@ -18,6 +23,8 @@ BatteryInfo = namedtuple(
'BatteryInfo', 'BatteryInfo',
[ [
'name', 'name',
'model',
'manufacturer',
'technology', 'technology',
'capacity', 'capacity',
'status', 'status',
@@ -50,11 +57,13 @@ def read_battery(filename):
def load_battery_info(info): def load_battery_info(info):
return BatteryInfo( return BatteryInfo(
name = info['POWER_SUPPLY_NAME'], name = info['POWER_SUPPLY_NAME'],
model = info['POWER_SUPPLY_MODEL_NAME'],
manufacturer = info['POWER_SUPPLY_MANUFACTURER'],
technology = info['POWER_SUPPLY_TECHNOLOGY'], technology = info['POWER_SUPPLY_TECHNOLOGY'],
capacity = float(info['POWER_SUPPLY_CHARGE_FULL']) / 1e6, capacity = float(info['POWER_SUPPLY_CHARGE_FULL']) / 1e6,
status = info['POWER_SUPPLY_STATUS'], status = info['POWER_SUPPLY_STATUS'],
currrent_percent = int(info['POWER_SUPPLY_CAPACITY']), currrent_percent = int(info['POWER_SUPPLY_CAPACITY']),
time_to_empty = int(info['POWER_SUPPLY_TIME_TO_EMPTY_NOW']), time_to_empty = int(info.get('POWER_SUPPLY_TIME_TO_EMPTY_NOW', 0)),
charging_current = int(float(info['POWER_SUPPLY_CURRENT_NOW']) / 1e3) charging_current = int(float(info['POWER_SUPPLY_CURRENT_NOW']) / 1e3)
) )
@@ -77,13 +86,16 @@ def format_percent(info):
def format_time_to_empty(info): def format_time_to_empty(info):
hours = info.time_to_empty // 60 if info.time_to_empty == 0:
minutes = info.time_to_empty % 60 return '??:??'
return terminal_colored(f"{hours}:{minutes:02d}", 'cyan') else:
hours = info.time_to_empty // 60
minutes = info.time_to_empty % 60
return terminal_colored(f"{hours}:{minutes:02d}", 'cyan')
def print_battery_detail(info): def print_battery_detail(info):
print(f"Battery: {info.technology} {info.name} {info.capacity} Ah") print(f"Battery: {info.model} - {info.manufacturer}: {info.technology} {info.capacity} Ah")
print("Battery level: " + format_percent(info)) print("Battery level: " + format_percent(info))
print("Battery status: " + format_status(info)) print("Battery status: " + format_status(info))
if info.status == DISCHARNGING: if info.status == DISCHARNGING: