-
Notifications
You must be signed in to change notification settings - Fork 0
/
upower.py
123 lines (107 loc) · 5.8 KB
/
upower.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import dbus
class UPowerManager():
def __init__(self):
self.UPOWER_NAME = "org.freedesktop.UPower"
self.UPOWER_PATH = "/org/freedesktop/UPower"
self.DBUS_PROPERTIES = "org.freedesktop.DBus.Properties"
self.bus = dbus.SystemBus()
def detect_devices(self):
upower_proxy = self.bus.get_object(self.UPOWER_NAME, self.UPOWER_PATH)
upower_interface = dbus.Interface(upower_proxy, self.UPOWER_NAME)
devices = upower_interface.EnumerateDevices()
return devices
def get_device_percentage(self, battery):
battery_proxy = self.bus.get_object(self.UPOWER_NAME, battery)
battery_proxy_interface = dbus.Interface(battery_proxy, self.DBUS_PROPERTIES)
return battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "Percentage")
def get_full_device_information(self, battery):
battery_proxy = self.bus.get_object(self.UPOWER_NAME, battery)
battery_proxy_interface = dbus.Interface(battery_proxy, self.DBUS_PROPERTIES)
hasHistory = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "HasHistory")
hasStatistics = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "HasStatistics")
isPresent = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "IsPresent")
isRechargable = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "IsRechargeable")
online = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "Online")
powersupply = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "PowerSupply")
capacity = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "Capacity")
energy = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "Energy")
energyempty = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "EnergyEmpty")
energyfull = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "EnergyFull")
energyfulldesign = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "EnergyFullDesign")
energyrate = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "EnergyRate")
luminosity = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "Luminosity")
percentage = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "Percentage")
temperature = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "Temperature")
voltage = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "Voltage")
timetoempty = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "TimeToEmpty")
timetofull = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "TimeToFull")
iconname = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "IconName")
model = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "Model")
nativepath = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "NativePath")
serial = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "Serial")
vendor = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "Vendor")
state = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "State")
technology = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "Technology")
battype = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "Type")
warninglevel = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "WarningLevel")
updatetime = battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "UpdateTime")
information_table = {
'HasHistory': hasHistory,
'HasStatistics': hasStatistics,
'IsPresent': isPresent,
'IsRechargeable': isRechargable,
'Online': online,
'PowerSupply': powersupply,
'Capacity': capacity,
'Energy': energy,
'EnergyEmpty': energyempty,
'EnergyFull': energyfull,
'EnergyFullDesign': energyfulldesign,
'EnergyRate': energyrate,
'Luminosity': luminosity,
'Percentage': percentage,
'Temperature': temperature,
'Voltage': voltage,
'TimeToEmpty': timetoempty,
'TimeToFull': timetofull,
'IconName': iconname,
'Model': model,
'NativePath': nativepath,
'Serial': serial,
'Vendor': vendor,
'State': state,
'Technology': technology,
'Type': battype,
'WarningLevel': warninglevel,
'UpdateTime': updatetime
}
return information_table
def get_percent_icon(self, percent):
print(type(percent))
if (percent <= 23):
return ""
elif (percent <= 50):
return ""
elif (percent <= 90):
return ""
elif (percent <= 100):
return ""
def get_state(self, battery):
battery_proxy = self.bus.get_object(self.UPOWER_NAME, battery)
battery_proxy_interface = dbus.Interface(battery_proxy, self.DBUS_PROPERTIES)
state = int(battery_proxy_interface.Get(self.UPOWER_NAME + ".Device", "State"))
if (state == 0):
return "" # unknown
elif (state == 1):
return "{ } " # Charging
elif (state == 2):
p = self.get_device_percentage(battery)
return self.get_percent_icon(p) # discharging
elif (state == 3):
return "" # empty
elif (state == 4):
return "{ } | " # fully charged
elif (state == 5):
return "Pending charge"
elif (state == 6):
return "Pending discharge"