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

Ble fix #3019

Merged
merged 7 commits into from Sep 1, 2016
Merged

Ble fix #3019

Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 17 additions & 5 deletions homeassistant/components/device_tracker/bluetooth_le_tracker.py
Expand Up @@ -2,12 +2,14 @@
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
Expand All @@ -19,6 +21,12 @@

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

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_SCAN_DURATION, default=10): vol.All(vol.Coerce(int),
Copy link
Contributor

Choose a reason for hiding this comment

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

use cv.positive_integer

Copy link
Author

Choose a reason for hiding this comment

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

There is no "postive_integer" method in Home assistants config validation module or do I miss something?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry I was thinking the positive_timedelta - my bad.

Copy link
Member

Choose a reason for hiding this comment

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

There's cv.positive_int.

vol.Range(min=1))
})


def setup_scanner(hass, config, see):
Expand All @@ -27,6 +35,7 @@ def setup_scanner(hass, config, see):
from gattlib import DiscoveryService

new_devices = {}
duration = 10
Copy link
Member

Choose a reason for hiding this comment

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

Do you need this line?
Voluptuous should default it to 10.

Copy link
Author

Choose a reason for hiding this comment

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

You're right, this can be removed.

Copy link
Author

Choose a reason for hiding this comment

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

It has been removed now.


def see_device(address, name, new_device=False):
"""Mark a device as seen."""
Expand All @@ -51,12 +60,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 +75,13 @@ def discover_ble_devices():
# to 0
for device in load_config(yaml_path, hass, 0, 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 +108,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