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
Ble fix #3019
Changes from 2 commits
6bcef27
bce6686
f594049
35f8b9c
c102c77
af2647a
404fee9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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), | ||
vol.Range(min=1)) | ||
}) | ||
|
||
|
||
def setup_scanner(hass, config, see): | ||
|
@@ -27,6 +35,7 @@ def setup_scanner(hass, config, see): | |
from gattlib import DiscoveryService | ||
|
||
new_devices = {} | ||
duration = 10 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need this line? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, this can be removed. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.""" | ||
|
@@ -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 = [] | ||
|
||
|
@@ -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. | ||
|
@@ -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) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use
cv.positive_integer
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.