Rewrite bluetooth le (#16592)
* Rewrite bluetooth le * Update requirements_all.txt * Update gen_requirements_all.py * Update bluetooth_le_tracker.py * Update bluetooth_le_tracker.py * Update bluetooth_le_tracker.py * Update bluetooth_le_tracker.py * Update bluetooth_le_tracker.py * Update bluetooth_le_tracker.py
This commit is contained in:
parent
b231fa2616
commit
abe61c5529
3 changed files with 13 additions and 27 deletions
|
@ -6,35 +6,25 @@ https://home-assistant.io/components/device_tracker.bluetooth_le_tracker/
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import voluptuous as vol
|
|
||||||
from homeassistant.helpers.event import track_point_in_utc_time
|
from homeassistant.helpers.event import track_point_in_utc_time
|
||||||
from homeassistant.components.device_tracker import (
|
from homeassistant.components.device_tracker import (
|
||||||
YAML_DEVICES, CONF_TRACK_NEW, CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL,
|
YAML_DEVICES, CONF_TRACK_NEW, CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL,
|
||||||
PLATFORM_SCHEMA, load_config, SOURCE_TYPE_BLUETOOTH_LE
|
load_config, SOURCE_TYPE_BLUETOOTH_LE
|
||||||
)
|
)
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
import homeassistant.helpers.config_validation as cv
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
REQUIREMENTS = ['gattlib==0.20150805']
|
REQUIREMENTS = ['pygatt==3.2.0']
|
||||||
|
|
||||||
BLE_PREFIX = 'BLE_'
|
BLE_PREFIX = 'BLE_'
|
||||||
MIN_SEEN_NEW = 5
|
MIN_SEEN_NEW = 5
|
||||||
CONF_SCAN_DURATION = 'scan_duration'
|
|
||||||
CONF_BLUETOOTH_DEVICE = 'device_id'
|
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
||||||
vol.Optional(CONF_SCAN_DURATION, default=10): cv.positive_int,
|
|
||||||
vol.Optional(CONF_BLUETOOTH_DEVICE, default='hci0'): cv.string
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
def setup_scanner(hass, config, see, discovery_info=None):
|
def setup_scanner(hass, config, see, discovery_info=None):
|
||||||
"""Set up the Bluetooth LE Scanner."""
|
"""Set up the Bluetooth LE Scanner."""
|
||||||
# pylint: disable=import-error
|
# pylint: disable=import-error
|
||||||
from gattlib import DiscoveryService
|
import pygatt
|
||||||
|
|
||||||
new_devices = {}
|
new_devices = {}
|
||||||
|
|
||||||
def see_device(address, name, new_device=False):
|
def see_device(address, name, new_device=False):
|
||||||
|
@ -61,17 +51,17 @@ def setup_scanner(hass, config, see, discovery_info=None):
|
||||||
"""Discover Bluetooth LE devices."""
|
"""Discover Bluetooth LE devices."""
|
||||||
_LOGGER.debug("Discovering Bluetooth LE devices")
|
_LOGGER.debug("Discovering Bluetooth LE devices")
|
||||||
try:
|
try:
|
||||||
service = DiscoveryService(ble_dev_id)
|
adapter = pygatt.GATTToolBackend()
|
||||||
devices = service.discover(duration)
|
devs = adapter.scan()
|
||||||
|
|
||||||
|
devices = {x['address']: x['name'] for x in devs}
|
||||||
_LOGGER.debug("Bluetooth LE devices discovered = %s", devices)
|
_LOGGER.debug("Bluetooth LE devices discovered = %s", devices)
|
||||||
except RuntimeError as error:
|
except RuntimeError as error:
|
||||||
_LOGGER.error("Error during Bluetooth LE scan: %s", error)
|
_LOGGER.error("Error during Bluetooth LE scan: %s", error)
|
||||||
devices = []
|
return {}
|
||||||
return devices
|
return devices
|
||||||
|
|
||||||
yaml_path = hass.config.path(YAML_DEVICES)
|
yaml_path = hass.config.path(YAML_DEVICES)
|
||||||
duration = config.get(CONF_SCAN_DURATION)
|
|
||||||
ble_dev_id = config.get(CONF_BLUETOOTH_DEVICE)
|
|
||||||
devs_to_track = []
|
devs_to_track = []
|
||||||
devs_donot_track = []
|
devs_donot_track = []
|
||||||
|
|
||||||
|
@ -102,11 +92,11 @@ def setup_scanner(hass, config, see, discovery_info=None):
|
||||||
"""Lookup Bluetooth LE devices and update status."""
|
"""Lookup Bluetooth LE devices and update status."""
|
||||||
devs = discover_ble_devices()
|
devs = discover_ble_devices()
|
||||||
for mac in devs_to_track:
|
for mac in devs_to_track:
|
||||||
_LOGGER.debug("Checking %s", mac)
|
if mac not in devs:
|
||||||
result = mac in devs
|
|
||||||
if not result:
|
|
||||||
# Could not lookup device name
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
if devs[mac] is None:
|
||||||
|
devs[mac] = mac
|
||||||
see_device(mac, devs[mac])
|
see_device(mac, devs[mac])
|
||||||
|
|
||||||
if track_new:
|
if track_new:
|
||||||
|
@ -119,5 +109,4 @@ def setup_scanner(hass, config, see, discovery_info=None):
|
||||||
track_point_in_utc_time(hass, update_ble, dt_util.utcnow() + interval)
|
track_point_in_utc_time(hass, update_ble, dt_util.utcnow() + interval)
|
||||||
|
|
||||||
update_ble(dt_util.utcnow())
|
update_ble(dt_util.utcnow())
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -385,9 +385,6 @@ fritzhome==1.0.4
|
||||||
# homeassistant.components.tts.google
|
# homeassistant.components.tts.google
|
||||||
gTTS-token==1.1.1
|
gTTS-token==1.1.1
|
||||||
|
|
||||||
# homeassistant.components.device_tracker.bluetooth_le_tracker
|
|
||||||
# gattlib==0.20150805
|
|
||||||
|
|
||||||
# homeassistant.components.sensor.gearbest
|
# homeassistant.components.sensor.gearbest
|
||||||
gearbest_parser==1.0.7
|
gearbest_parser==1.0.7
|
||||||
|
|
||||||
|
@ -874,6 +871,7 @@ pyfritzhome==0.3.7
|
||||||
# homeassistant.components.ifttt
|
# homeassistant.components.ifttt
|
||||||
pyfttt==0.3
|
pyfttt==0.3
|
||||||
|
|
||||||
|
# homeassistant.components.device_tracker.bluetooth_le_tracker
|
||||||
# homeassistant.components.sensor.skybeacon
|
# homeassistant.components.sensor.skybeacon
|
||||||
pygatt==3.2.0
|
pygatt==3.2.0
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,6 @@ COMMENT_REQUIREMENTS = (
|
||||||
'bluepy',
|
'bluepy',
|
||||||
'opencv-python',
|
'opencv-python',
|
||||||
'python-lirc',
|
'python-lirc',
|
||||||
'gattlib',
|
|
||||||
'pyuserinput',
|
'pyuserinput',
|
||||||
'evdev',
|
'evdev',
|
||||||
'pycups',
|
'pycups',
|
||||||
|
|
Loading…
Add table
Reference in a new issue