This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

@@ -3,10 +3,14 @@ import logging
from homeassistant.helpers.event import track_point_in_utc_time
from homeassistant.components.device_tracker.legacy import (
YAML_DEVICES, async_load_config
YAML_DEVICES,
async_load_config,
)
from homeassistant.components.device_tracker.const import (
CONF_TRACK_NEW, CONF_SCAN_INTERVAL, SCAN_INTERVAL, SOURCE_TYPE_BLUETOOTH_LE
CONF_TRACK_NEW,
CONF_SCAN_INTERVAL,
SCAN_INTERVAL,
SOURCE_TYPE_BLUETOOTH_LE,
)
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
import homeassistant.util.dt as dt_util
@@ -14,9 +18,9 @@ from homeassistant.util.async_ import run_coroutine_threadsafe
_LOGGER = logging.getLogger(__name__)
DATA_BLE = 'BLE'
DATA_BLE_ADAPTER = 'ADAPTER'
BLE_PREFIX = 'BLE_'
DATA_BLE = "BLE"
DATA_BLE_ADAPTER = "ADAPTER"
BLE_PREFIX = "BLE_"
MIN_SEEN_NEW = 5
@@ -24,6 +28,7 @@ def setup_scanner(hass, config, see, discovery_info=None):
"""Set up the Bluetooth LE Scanner."""
# pylint: disable=import-error
import pygatt
new_devices = {}
hass.data.setdefault(DATA_BLE, {DATA_BLE_ADAPTER: None})
@@ -41,8 +46,7 @@ def setup_scanner(hass, config, see, discovery_info=None):
"""Mark a device as seen."""
if new_device:
if address in new_devices:
_LOGGER.debug(
"Seen %s %s times", address, new_devices[address])
_LOGGER.debug("Seen %s %s times", address, new_devices[address])
new_devices[address] += 1
if new_devices[address] >= MIN_SEEN_NEW:
_LOGGER.debug("Adding %s to tracked devices", address)
@@ -57,8 +61,11 @@ def setup_scanner(hass, config, see, discovery_info=None):
if name is not None:
name = name.strip("\x00")
see(mac=BLE_PREFIX + address, host_name=name,
source_type=SOURCE_TYPE_BLUETOOTH_LE)
see(
mac=BLE_PREFIX + address,
host_name=name,
source_type=SOURCE_TYPE_BLUETOOTH_LE,
)
def discover_ble_devices():
"""Discover Bluetooth LE devices."""
@@ -68,7 +75,7 @@ def setup_scanner(hass, config, see, discovery_info=None):
hass.data[DATA_BLE][DATA_BLE_ADAPTER] = adapter
devs = adapter.scan()
devices = {x['address']: x['name'] for x in devs}
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)
@@ -83,8 +90,7 @@ def setup_scanner(hass, config, see, discovery_info=None):
# We just need the devices so set consider_home and home range
# to 0
for device in run_coroutine_threadsafe(
async_load_config(yaml_path, hass, 0),
hass.loop
async_load_config(yaml_path, hass, 0), hass.loop
).result():
# check if device is a valid bluetooth device
if device.mac and device.mac[:4].upper() == BLE_PREFIX:
@@ -118,8 +124,7 @@ def setup_scanner(hass, config, see, discovery_info=None):
if track_new:
for address in devs:
if address not in devs_to_track and \
address not in devs_donot_track:
if address not in devs_to_track and address not in devs_donot_track:
_LOGGER.info("Discovered Bluetooth LE device %s", address)
see_device(address, devs[address], new_device=True)