Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Rewrite bluetooth le #16592

Merged
merged 9 commits into from
Sep 14, 2018
Merged
Changes from 1 commit
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
Next Next commit
Rewrite bluetooth le
  • Loading branch information
pvizeli authored Sep 13, 2018
commit 19b0815070296e1ab217703477bf4d97414cdfe5
24 changes: 12 additions & 12 deletions homeassistant/components/device_tracker/bluetooth_le_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

_LOGGER = logging.getLogger(__name__)

REQUIREMENTS = ['gattlib==0.20150805']
REQUIREMENTS = ['pygatt==3.2.0']

BLE_PREFIX = 'BLE_'
MIN_SEEN_NEW = 5
Expand All @@ -33,8 +33,7 @@
def setup_scanner(hass, config, see, discovery_info=None):
"""Set up the Bluetooth LE Scanner."""
# pylint: disable=import-error
from gattlib import DiscoveryService

import pygatt
new_devices = {}

def see_device(address, name, new_device=False):
Expand All @@ -61,12 +60,14 @@ def discover_ble_devices():
"""Discover Bluetooth LE devices."""
_LOGGER.debug("Discovering Bluetooth LE devices")
try:
service = DiscoveryService(ble_dev_id)
devices = service.discover(duration)
adapter = pygatt.GATTToolBackend()
devs = adapter.scan()

devices = {x['address']: x['name'] for x in devs}
_LOGGER.debug("Bluetooth LE devices discovered = %s", devices)
except RuntimeError as error:
_LOGGER.error("Error during Bluetooth LE scan: %s", error)
devices = []
return None
return devices

yaml_path = hass.config.path(YAML_DEVICES)
Expand Down Expand Up @@ -101,12 +102,12 @@ def discover_ble_devices():
def update_ble(now):
"""Lookup Bluetooth LE devices and update status."""
devs = discover_ble_devices()
for mac in devs_to_track:
_LOGGER.debug("Checking %s", mac)
result = mac in devs
if not result:
# Could not lookup device name
for mac in devs_to_track:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing whitespace

if mac not in devs:
continue

if devs[mac] is None:
devs[mac] = mac
see_device(mac, devs[mac])

if track_new:
Expand All @@ -119,5 +120,4 @@ def update_ble(now):
track_point_in_utc_time(hass, update_ble, dt_util.utcnow() + interval)

update_ble(dt_util.utcnow())

return True