From 81f30fab7acdf7fa34b00d1544186299ffa6585d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Sat, 1 Dec 2018 18:27:18 +0100 Subject: [PATCH] Fix blocking code (#20) --- examples/bluetooth_scan_network.py | 2 ++ examples/bluetooth_scan_unit.py | 2 ++ googledevices/api/bluetooth.py | 4 +--- googledevices/cli/commands.py | 5 ++++- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/examples/bluetooth_scan_network.py b/examples/bluetooth_scan_network.py index 1a70648..5b9af60 100644 --- a/examples/bluetooth_scan_network.py +++ b/examples/bluetooth_scan_network.py @@ -1,6 +1,7 @@ """Example usage of googledevices.""" import asyncio import json +import time import aiohttp from googledevices.api.bluetooth import Bluetooth from googledevices.api.device_info import DeviceInfo @@ -31,6 +32,7 @@ async def bluetooth_scan(): async with aiohttp.ClientSession() as session: googledevices = Bluetooth(LOOP, session, host['host']) await googledevices.scan_for_devices_multi_run() + time.sleep(5) await googledevices.get_scan_result() for device in googledevices.devices: mac = device['mac_address'] diff --git a/examples/bluetooth_scan_unit.py b/examples/bluetooth_scan_unit.py index 3e16b6c..af90233 100644 --- a/examples/bluetooth_scan_unit.py +++ b/examples/bluetooth_scan_unit.py @@ -1,5 +1,6 @@ """Example usage of googledevices.""" import asyncio +import time import aiohttp from googledevices.api.bluetooth import Bluetooth @@ -11,6 +12,7 @@ async def bluetooth_scan(): async with aiohttp.ClientSession() as session: googledevices = Bluetooth(LOOP, session, IPADDRESS) await googledevices.scan_for_devices() # Start device scan + time.sleep(5) await googledevices.get_scan_result() # Returns the result print("Device info:", googledevices.devices) diff --git a/googledevices/api/bluetooth.py b/googledevices/api/bluetooth.py index 9529a4d..400cad3 100644 --- a/googledevices/api/bluetooth.py +++ b/googledevices/api/bluetooth.py @@ -8,7 +8,6 @@ import logging import json import socket -import time import aiohttp import async_timeout @@ -58,7 +57,7 @@ async def set_discovery_enabled(self): _LOGGER.error('Error connecting to %s - %s', self._ipaddress, error) - async def scan_for_devices(self, sleep=5): + async def scan_for_devices(self): """Scan for bluetooth devices.""" endpoint = '/setup/bluetooth/scan' data = {"enable": True, "clear_results": True, "timeout": 5} @@ -74,7 +73,6 @@ async def scan_for_devices(self, sleep=5): aiohttp.ClientError, socket.gaierror) as error: _LOGGER.error('Error connecting to %s - %s', self._ipaddress, error) - time.sleep(sleep) async def get_scan_result(self): """Scan for bluetooth devices.""" diff --git a/googledevices/cli/commands.py b/googledevices/cli/commands.py index d3abe4b..bcc7559 100644 --- a/googledevices/cli/commands.py +++ b/googledevices/cli/commands.py @@ -1,6 +1,7 @@ """CLI commands.""" import asyncio import json +import time import click import aiohttp @@ -37,6 +38,7 @@ async def bluetooth_scan(): async with aiohttp.ClientSession() as session: googledevices = Bluetooth(LOOP, session, ip_address) await googledevices.scan_for_devices() + time.sleep(5) await googledevices.get_scan_result() print(json.dumps(googledevices.devices, indent=4, sort_keys=True)) LOOP.run_until_complete(bluetooth_scan()) @@ -63,7 +65,7 @@ async def bluetooth_scan(): googledevices = NetworkScan(LOOP, session) result = await googledevices.scan_for_units(ipscope) for host in result: - if host['assistant']: + if host['bluetooth']: async with aiohttp.ClientSession() as session: googledevices = DeviceInfo(LOOP, session, host['host']) await googledevices.get_device_info() @@ -71,6 +73,7 @@ async def bluetooth_scan(): async with aiohttp.ClientSession() as session: googledevices = Bluetooth(LOOP, session, host['host']) await googledevices.scan_for_devices_multi_run() + time.sleep(5) await googledevices.get_scan_result() for device in googledevices.devices: mac = device['mac_address']