Skip to content

Commit

Permalink
Fix for BLE device tracker (#3019)
Browse files Browse the repository at this point in the history
* Bug fix tracked devices
* Added scan_duration configuration parameter
  • Loading branch information
Open Home Automation authored and kellerza committed Sep 1, 2016
1 parent d2dfe04 commit 83f1272
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions homeassistant/components/device_tracker/bluetooth_le_tracker.py
Expand Up @@ -2,23 +2,31 @@
import logging
from datetime import timedelta

import voluptuous as vol
from homeassistant.helpers.event import track_point_in_utc_time
from homeassistant.components.device_tracker import (
YAML_DEVICES,
CONF_TRACK_NEW,
CONF_SCAN_INTERVAL,
DEFAULT_SCAN_INTERVAL,
PLATFORM_SCHEMA,
load_config,
)
import homeassistant.util as util
import homeassistant.util.dt as dt_util
import homeassistant.helpers.config_validation as cv

_LOGGER = logging.getLogger(__name__)

REQUIREMENTS = ['gattlib==0.20150805']

BLE_PREFIX = 'BLE_'
MIN_SEEN_NEW = 5
CONF_SCAN_DURATION = "scan_duration"

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_SCAN_DURATION, default=10): cv.positive_int
})


def setup_scanner(hass, config, see):
Expand Down Expand Up @@ -51,12 +59,13 @@ def discover_ble_devices():
"""Discover Bluetooth LE devices."""
_LOGGER.debug("Discovering Bluetooth LE devices")
service = DiscoveryService()
devices = service.discover(10)
devices = service.discover(duration)
_LOGGER.debug("Bluetooth LE devices discovered = %s", devices)

return devices

yaml_path = hass.config.path(YAML_DEVICES)
duration = config.get(CONF_SCAN_DURATION)
devs_to_track = []
devs_donot_track = []

Expand All @@ -65,11 +74,13 @@ def discover_ble_devices():
# to 0
for device in load_config(yaml_path, hass, 0):
# check if device is a valid bluetooth device
if device.mac and device.mac[:3].upper() == BLE_PREFIX:
if device.mac and device.mac[:4].upper() == BLE_PREFIX:
if device.track:
devs_to_track.append(device.mac[3:])
_LOGGER.debug("Adding %s to BLE tracker", device.mac)
devs_to_track.append(device.mac[4:])
else:
devs_donot_track.append(device.mac[3:])
_LOGGER.debug("Adding %s to BLE do not track", device.mac)
devs_donot_track.append(device.mac[4:])

# if track new devices is true discover new devices
# on every scan.
Expand All @@ -96,7 +107,7 @@ def update_ble(now):
if track_new:
for address in devs:
if address not in devs_to_track and \
address not in devs_donot_track:
address not in devs_donot_track:
_LOGGER.info("Discovered Bluetooth LE device %s", address)
see_device(address, devs[address], new_device=True)

Expand Down

0 comments on commit 83f1272

Please sign in to comment.