Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix blocking code #20

Merged
merged 1 commit into from
Dec 1, 2018
Merged
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
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