Skip to content

Fixes & enhancements: fix deprecation warning on Python versions newer than v3.6 & support breaking of loop with CTRL-C with no exceptions #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# **AirStatus for Linux**
#### Check your AirPods battery level on Linux

#### What is it?
### What is it?
This is a Python 3.6 script, forked from [faglo/AirStatus](https://github.com/faglo/AirStatus) that allows you to check AirPods battery level from your terminal, as JSON output.

### Usage
Expand All @@ -12,6 +12,8 @@ python3 main.py [output_file]

Output will be stored in `output_file` if specified.

This script requires `bleak` package. Install it with: `pip3 install bleak`.

#### Example output

```
Expand Down
45 changes: 27 additions & 18 deletions main.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#!/usr/bin/env python3
from bleak import discover
from asyncio import new_event_loop, set_event_loop, get_event_loop
from time import sleep, time_ns
from binascii import hexlify
from json import dumps
from sys import argv
from datetime import datetime
import sys
import asyncio

# Configure update duration (update after n seconds)
UPDATE_DURATION = 1
Expand Down Expand Up @@ -54,11 +57,14 @@ async def get_device():

# Same as get_device() but it's standalone method instead of async
def get_data_hex():
new_loop = new_event_loop()
set_event_loop(new_loop)
loop = get_event_loop()
a = loop.run_until_complete(get_device())
loop.close()
if sys.version_info < (3, 7):
new_loop = new_event_loop()
set_event_loop(new_loop)
loop = get_event_loop()
a = loop.run_until_complete(get_device())
loop.close()
else:
a = asyncio.run(get_device())
return a


Expand Down Expand Up @@ -128,19 +134,22 @@ def is_flipped(raw):
def run():
output_file = argv[-1]

while True:
data = get_data()

if data["status"] == 1:
json_data = dumps(data)
if len(argv) > 1:
f = open(output_file, "a")
f.write(json_data+"\n")
f.close()
else:
print(json_data)

sleep(UPDATE_DURATION)
try:
while True:
data = get_data()

if data["status"] == 1:
json_data = dumps(data)
if len(argv) > 1:
f = open(output_file, "a")
f.write(json_data+"\n")
f.close()
else:
print(json_data)

sleep(UPDATE_DURATION)
except KeyboardInterrupt:
pass


if __name__ == '__main__':
Expand Down