From 3ad0b3fcc77c724f9f56bab792c8b4a1a8f24cb5 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Wed, 27 Sep 2023 19:27:10 +0100 Subject: [PATCH] Use async subprocess (#67) --- bluetooth_devices.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/bluetooth_devices.py b/bluetooth_devices.py index e6f4b51..80d8ca5 100644 --- a/bluetooth_devices.py +++ b/bluetooth_devices.py @@ -3,6 +3,7 @@ import asyncio import socket import os +from subprocess import DEVNULL, PIPE from typing import Awaitable, Callable, Optional, TYPE_CHECKING from dasbus.connection import SystemMessageBus @@ -201,17 +202,19 @@ def add_device(self, device_object_path: str, is_host: bool) -> None: async def switch_to_master(self, device_address: str) -> None: print("switch to master called for ", device_address) - while self.is_slave(device_address): + while await self.is_slave(device_address): try: - success = os.system("sudo hcitool sr " + device_address + " MASTER") == 0 - print("hcitool ",device_address," success:",success) + proc = await asyncio.create_subprocess_exec("sudo", "hcitool", "sr", device_address, "MASTER", stdout=DEVNULL) + await proc.wait() + print("hcitool ", device_address, " success:", proc.returncode == 0) except Exception as exc: print("hcitool ",device_address," exception:",exc) await asyncio.sleep(5) - def is_slave(self, device_address: str) -> bool: - with os.popen('sudo hcitool con') as stream: - return any("SLAVE" in l and device_address in l for l in stream.readlines()) + async def is_slave(self, device_address: str) -> bool: + proc = await asyncio.create_subprocess_exec("sudo", "hcitool", "con", stdout=PIPE, stderr=DEVNULL) + stdout, stderr = await proc.communicate() + return any("SLAVE" in l and device_address in l for l in stdout.decode().split("\n")) def remove_devices(self) -> None: print("Removing all BT devices")