|
9 | 9 | ------------------------------------------------- |
10 | 10 | """ |
11 | 11 |
|
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')) |
17 | 15 |
|
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 |
51 | 63 |
|
52 | 64 |
|
53 | 65 |
|
|
0 commit comments