Skip to content

Commit

Permalink
Fix blocking code (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored and eliseomartelli committed Dec 1, 2018
1 parent cce9c3f commit 81f30fa
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
2 changes: 2 additions & 0 deletions examples/bluetooth_scan_network.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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']
Expand Down
2 changes: 2 additions & 0 deletions examples/bluetooth_scan_unit.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Example usage of googledevices."""
import asyncio
import time
import aiohttp
from googledevices.api.bluetooth import Bluetooth

Expand All @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions googledevices/api/bluetooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import logging
import json
import socket
import time

import aiohttp
import async_timeout
Expand Down Expand Up @@ -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}
Expand All @@ -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."""
Expand Down
5 changes: 4 additions & 1 deletion googledevices/cli/commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""CLI commands."""
import asyncio
import json
import time
import click
import aiohttp

Expand Down Expand Up @@ -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())
Expand All @@ -63,14 +65,15 @@ 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()
ghname = googledevices.device_info.get('name')
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']
Expand Down

0 comments on commit 81f30fa

Please # to comment.