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 re
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 termcolor import colored
POWER_CLASS = '/sys/class/power_supply'
BATTERY = 'cw2015-battery'
UEVENT = path.join(POWER_CLASS, BATTERY, 'uevent')
BATTERIES = (
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'
@@ -18,6 +23,8 @@ BatteryInfo = namedtuple(
'BatteryInfo',
[
'name',
'model',
'manufacturer',
'technology',
'capacity',
'status',
@@ -50,11 +57,13 @@ def read_battery(filename):
def load_battery_info(info):
return BatteryInfo(
name = info['POWER_SUPPLY_NAME'],
model = info['POWER_SUPPLY_MODEL_NAME'],
manufacturer = info['POWER_SUPPLY_MANUFACTURER'],
technology = info['POWER_SUPPLY_TECHNOLOGY'],
capacity = float(info['POWER_SUPPLY_CHARGE_FULL']) / 1e6,
status = info['POWER_SUPPLY_STATUS'],
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)
)
@@ -77,13 +86,16 @@ def format_percent(info):
def format_time_to_empty(info):
hours = info.time_to_empty // 60
minutes = info.time_to_empty % 60
return terminal_colored(f"{hours}:{minutes:02d}", 'cyan')
if info.time_to_empty == 0:
return '??:??'
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):
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 status: " + format_status(info))
if info.status == DISCHARNGING: