1
0
Fork 0
ryzowanie/config/i3blocks/blocklets/battery

111 lines
2.7 KiB
Text
Raw Permalink Normal View History

2024-06-11 21:55:28 +02:00
#!/usr/bin/env python
# Works with upower 0.99.7
import time
import subprocess
glyphs = {
'supply' : {
'on' : '',
'off' : ''#''
},
'status' : ["", "", "", "", ""],
'time' : ""
}
flags = {
'supply' : False,
'charge' : False,
'full' : False,
'discharging' : False,
'charging' : False
}
def getIco(percent):
if percent > 90:
color = '#FFFFFF'
ico = glyphs['status'][4]
elif percent > 70:
color = '#FFFFFF'
ico = glyphs['status'][3]
elif percent > 40:
color = '#FFFFFF'
ico = glyphs['status'][2]
elif percent > 15:
color = '#FFAAAA'
ico = glyphs['status'][1]
else:
color = '#A00000'
ico = glyphs['status'][0]
s = '<span foreground="'+color+'">'+ico+'</span>'
return s
# Get information about power devices with following command:
# upower -e
# Get information from upower about power supply
ac_device = '/org/freedesktop/UPower/devices/line_power_AC'
info_ac = subprocess.run(['upower', '-i', ac_device], stdout=subprocess.PIPE)
info_ac = info_ac.stdout.decode('UTF-8')
info_ac = info_ac.split('\n')
## Process AC information
for l in info_ac:
foo = l.split()
if not foo:
continue
if foo[0] == 'online:':
if foo[1] == 'yes':
flags['supply'] = True
# Get information from upower about BAT1
ac_device = '/org/freedesktop/UPower/devices/battery_BAT1'
info_BAT1 = subprocess.run(['upower', '-i', ac_device], stdout=subprocess.PIPE)
info_BAT1 = info_BAT1.stdout.decode('UTF-8')
info_BAT1 = info_BAT1.split('\n')
## Process BAT1 information
for l in info_BAT1:
foo = l.split()
if not foo:
continue
if foo[0] == 'state:':
if foo[1] == 'fully-charged':
flags['full'] = True
elif foo[1] == 'discharging':
flags['discharging'] = True
elif foo[0] == 'percentage:':
percentage = foo[1]
elif foo[0] == 'time':
remaining = foo[3]
if foo[4] == 'minutes':
remaining += 'm'
elif foo[4] == 'hours':
remaining += 'h'
if flags['supply']:
currentTime = int(time.time())
supply_ico = glyphs['supply']['on']
battery_ico = glyphs['status'][currentTime % 5]
else:
supply_ico = glyphs['supply']['off']
p = int(percentage[:-1])
battery_ico = getIco(p)
if flags['full']:
b = '<span foreground="#00CF00">' + glyphs['status'][4] + '</span>'
print(b +' '+supply_ico)
#print(b +' '+percentage+' '+supply_ico)
else:
print(battery_ico)
#print(battery_ico+' '+percentage+'('+glyphs['time']+remaining+')'+supply_ico)