From 2a283546a00d0e9c83653a92fbdac74343071ad6 Mon Sep 17 00:00:00 2001 From: Abdallah Abdelazim Date: Wed, 22 Jun 2022 21:20:37 +0200 Subject: [PATCH 1/5] Add python shebang line --- main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/main.py b/main.py index a6b2e4f..eedf494 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +#!/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 2e09948af153dfbf7d26d4445d8d8552c376ff07 Mon Sep 17 00:00:00 2001 From: Abdallah Abdelazim Date: Wed, 22 Jun 2022 21:22:24 +0200 Subject: [PATCH 2/5] Fix deprecation warning on Python newer than v3.6 --- main.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index eedf494..fe15c07 100644 --- a/main.py +++ b/main.py @@ -6,6 +6,8 @@ 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 @@ -55,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 From 2446422d783e7d5c5d6f7b72d327db455032ec0a Mon Sep 17 00:00:00 2001 From: Abdallah Abdelazim Date: Wed, 22 Jun 2022 21:24:39 +0200 Subject: [PATCH 3/5] Support breaking of loop with CTRL-C with no exceptions --- main.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/main.py b/main.py index fe15c07..e79d566 100644 --- a/main.py +++ b/main.py @@ -134,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__': From 2d31eda7a8f9ab501d4b7fdde68bbff2c2e461be Mon Sep 17 00:00:00 2001 From: Abdallah Abdelazim Date: Wed, 22 Jun 2022 21:31:01 +0200 Subject: [PATCH 4/5] Add execute permission to --- main.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 main.py diff --git a/main.py b/main.py old mode 100644 new mode 100755 From 6d37599a23ed221760a18691791138713418fbb1 Mon Sep 17 00:00:00 2001 From: Abdallah Abdelazim Date: Wed, 22 Jun 2022 21:51:33 +0200 Subject: [PATCH 5/5] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a7a2a95..ac9ae02 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 ```