Skip to content

Commit fd318bc

Browse files
committed
battery states
simplified getting battery info
1 parent bbbb50c commit fd318bc

File tree

3 files changed

+61
-42
lines changed

3 files changed

+61
-42
lines changed

src/adb_globals.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,24 @@ def bat_state_conver(choice, num):
6565

6666

6767
def match_device(name):
68-
""" Returns the device ID associated with the device name"""
68+
"""
69+
Returns the device ID associated with the device name
70+
"""
6971
return device_list[name]['ID']
7072

7173

7274
def kb2mb(num):
75+
"""
76+
Converts KB to MB
77+
"""
7378
return int(num) // 1024
7479

7580

7681

7782

7883
def out(command):
79-
"""Power Shell Func
84+
"""
85+
Power Shell Func
8086
- Checking Devices Test
8187
"""
8288
systemencoding = windll.kernel32.GetConsoleOutputCP()

src/device_states.py

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,57 @@
99
-------------------------------------------------
1010
"""
1111

12-
battery_states_final={}
13-
def battery_state_update(device, format = "Celcius"):
14-
""" Returns Dictionary with Full Battery Details """
15-
battery_states = {"temperature": "", "level": "", "voltage":"", "status":"", "health": "",
16-
"AC powered": "", "USB powered": "", "Wireless powered":"", "present": ""}
12+
def battery_state_update(device: object, format: str = "Celcius"):
13+
battery_info = {}
14+
output = (device.shell('dumpsys battery'))
1715

18-
ok=(device.shell('dumpsys battery'))
19-
buf = StringIO(ok)
20-
changes = 0
21-
for line2 in buf.readlines():
22-
line = line2.strip()
23-
if "Max charging voltage" in line:
24-
pass
25-
else:
26-
for state, value in battery_states.items():
27-
m = re.search(r'{}:(.*)'.format(state), line)
28-
if m:
29-
if value != m.group(1):
30-
changes += 1
31-
# print("changed: state={} old={} new={}".format(state, value, m.group(1)))
32-
33-
battery_states[state] = m.group(1).lstrip()
34-
35-
# Taking Battery Health/Status and Turning into "result"""
36-
if "vendor.samsung.hardware" in battery_states['health']:
37-
extract_health= (battery_states['health'].split("::")[0].replace("vendor.samsung.hardware.health@",""))
38-
battery_states['health'] = str(round(float(extract_health)))
39-
40-
# Converting Battery Status Number to String
41-
try:
42-
battery_states['status'] = bat_state_conver(choice='status',num=int(battery_states['status']))
43-
except:
44-
print("Unable to Convert Status, Check Results")
45-
46-
if changes > 0:
47-
print("---- {} changes".format(changes))
48-
pass
49-
50-
return battery_states
16+
for line in output.splitlines():
17+
if ":" in line:
18+
key, value = line.split(":", 1)
19+
battery_info[key.strip()] = value.strip()
20+
21+
battery_info['status'] = bat_state_conver(choice='status',num=int(battery_info['status']))
22+
return battery_info
23+
24+
#battery_states_final={}
25+
#def battery_state_update(device, format = "Celcius"):
26+
# """ Returns Dictionary with Full Battery Details """
27+
# battery_states = {"temperature": "", "level": "", "voltage":"", "status":"", "health": "",
28+
# "AC powered": "", "USB powered": "", "Wireless powered":"", "present": ""}
29+
#
30+
# ok=(device.shell('dumpsys battery'))
31+
# buf = StringIO(ok)
32+
# changes = 0
33+
# for line2 in buf.readlines():
34+
# line = line2.strip()
35+
# if "Max charging voltage" in line:
36+
# pass
37+
# else:
38+
# for state, value in battery_states.items():
39+
# m = re.search(r'{}:(.*)'.format(state), line)
40+
# if m:
41+
# if value != m.group(1):
42+
# changes += 1
43+
# # print("changed: state={} old={} new={}".format(state, value, m.group(1)))
44+
#
45+
# battery_states[state] = m.group(1).lstrip()
46+
#
47+
# # Taking Battery Health/Status and Turning into "result"""
48+
# if "vendor.samsung.hardware" in battery_states['health']:
49+
# extract_health= (battery_states['health'].split("::")[0].replace("vendor.samsung.hardware.health@",""))
50+
# battery_states['health'] = str(round(float(extract_health)))
51+
#
52+
# # Converting Battery Status Number to String
53+
# try:
54+
# battery_states['status'] = bat_state_conver(choice='status',num=int(battery_states['status']))
55+
# except:
56+
# print("Unable to Convert Status, Check Results")
57+
#
58+
# if changes > 0:
59+
# print("---- {} changes".format(changes))
60+
# pass
61+
#
62+
# return battery_states
5163

5264

5365

src/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ def create_states():
179179
"value": bat_state_conver(choice='health',num=int(device['health'])),
180180
'parentGroup': device['Model']
181181
},
182+
## add "AC powered", "USB powered", "Wireless powered"
183+
182184
{
183185
"id": PLUGIN_ID + f'.device.{device["Model"]}.cpu_usage',
184186
"desc": f"{device['Model']} CPU Usage",
@@ -292,9 +294,8 @@ def state_loop_update():
292294
for x in device_list:
293295
device_states.get_cpu_usge(device_list[x]['ID'])
294296
device_states.get_mem_info(device_list[x]['ID'])
295-
296297
## have not decided to loop for battery % yet.. until someone notices??
297-
# device_states.battery_state_update(device_list[x]['ID'])
298+
device_states.battery_state_update(device_list[x]['ID'])
298299
create_states()
299300
time.sleep(5)
300301

0 commit comments

Comments
 (0)