Added support for requesting RSSI values from Bluetooth devices (#12458)
* Added support for requesting RSSI values from Bluetooth devices * Moved Bluetooth RSSI code to separate library and imported it * Cleaned up tuple issues * Changed concatination of mac addresses * Changed string formatting to use new style * Ran gen_requirements_all.py
This commit is contained in:
parent
7b3d17bae4
commit
7c99567b65
2 changed files with 25 additions and 10 deletions
|
@ -17,12 +17,15 @@ import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
REQUIREMENTS = ['pybluez==0.22']
|
REQUIREMENTS = ['pybluez==0.22', 'bt_proximity==0.1.2']
|
||||||
|
|
||||||
BT_PREFIX = 'BT_'
|
BT_PREFIX = 'BT_'
|
||||||
|
|
||||||
|
CONF_REQUEST_RSSI = 'request_rssi'
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Optional(CONF_TRACK_NEW): cv.boolean
|
vol.Optional(CONF_TRACK_NEW): cv.boolean,
|
||||||
|
vol.Optional(CONF_REQUEST_RSSI): cv.boolean
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
@ -30,11 +33,15 @@ def setup_scanner(hass, config, see, discovery_info=None):
|
||||||
"""Set up the Bluetooth Scanner."""
|
"""Set up the Bluetooth Scanner."""
|
||||||
# pylint: disable=import-error
|
# pylint: disable=import-error
|
||||||
import bluetooth
|
import bluetooth
|
||||||
|
from bt_proximity import BluetoothRSSI
|
||||||
|
|
||||||
def see_device(device):
|
def see_device(mac, name, rssi=None):
|
||||||
"""Mark a device as seen."""
|
"""Mark a device as seen."""
|
||||||
see(mac=BT_PREFIX + device[0], host_name=device[1],
|
attributes = {}
|
||||||
source_type=SOURCE_TYPE_BLUETOOTH)
|
if rssi is not None:
|
||||||
|
attributes['rssi'] = rssi
|
||||||
|
see(mac="{}_{}".format(BT_PREFIX, mac), host_name=name,
|
||||||
|
attributes=attributes, source_type=SOURCE_TYPE_BLUETOOTH)
|
||||||
|
|
||||||
def discover_devices():
|
def discover_devices():
|
||||||
"""Discover Bluetooth devices."""
|
"""Discover Bluetooth devices."""
|
||||||
|
@ -64,27 +71,32 @@ def setup_scanner(hass, config, see, discovery_info=None):
|
||||||
if track_new:
|
if track_new:
|
||||||
for dev in discover_devices():
|
for dev in discover_devices():
|
||||||
if dev[0] not in devs_to_track and \
|
if dev[0] not in devs_to_track and \
|
||||||
dev[0] not in devs_donot_track:
|
dev[0] not in devs_donot_track:
|
||||||
devs_to_track.append(dev[0])
|
devs_to_track.append(dev[0])
|
||||||
see_device(dev)
|
see_device(dev[0], dev[1])
|
||||||
|
|
||||||
interval = config.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
|
interval = config.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
|
||||||
|
|
||||||
|
request_rssi = config.get(CONF_REQUEST_RSSI, False)
|
||||||
|
|
||||||
def update_bluetooth(now):
|
def update_bluetooth(now):
|
||||||
"""Lookup Bluetooth device and update status."""
|
"""Lookup Bluetooth device and update status."""
|
||||||
try:
|
try:
|
||||||
if track_new:
|
if track_new:
|
||||||
for dev in discover_devices():
|
for dev in discover_devices():
|
||||||
if dev[0] not in devs_to_track and \
|
if dev[0] not in devs_to_track and \
|
||||||
dev[0] not in devs_donot_track:
|
dev[0] not in devs_donot_track:
|
||||||
devs_to_track.append(dev[0])
|
devs_to_track.append(dev[0])
|
||||||
for mac in devs_to_track:
|
for mac in devs_to_track:
|
||||||
_LOGGER.debug("Scanning %s", mac)
|
_LOGGER.debug("Scanning %s", mac)
|
||||||
result = bluetooth.lookup_name(mac, timeout=5)
|
result = bluetooth.lookup_name(mac, timeout=5)
|
||||||
if not result:
|
rssi = None
|
||||||
|
if request_rssi:
|
||||||
|
rssi = BluetoothRSSI(mac).request_rssi()
|
||||||
|
if result is None:
|
||||||
# Could not lookup device name
|
# Could not lookup device name
|
||||||
continue
|
continue
|
||||||
see_device((mac, result))
|
see_device(mac, result, rssi)
|
||||||
except bluetooth.BluetoothError:
|
except bluetooth.BluetoothError:
|
||||||
_LOGGER.exception("Error looking up Bluetooth device")
|
_LOGGER.exception("Error looking up Bluetooth device")
|
||||||
track_point_in_utc_time(
|
track_point_in_utc_time(
|
||||||
|
|
|
@ -172,6 +172,9 @@ boto3==1.4.7
|
||||||
# homeassistant.scripts.credstash
|
# homeassistant.scripts.credstash
|
||||||
botocore==1.7.34
|
botocore==1.7.34
|
||||||
|
|
||||||
|
# homeassistant.components.device_tracker.bluetooth_tracker
|
||||||
|
bt_proximity==0.1.2
|
||||||
|
|
||||||
# homeassistant.components.sensor.buienradar
|
# homeassistant.components.sensor.buienradar
|
||||||
# homeassistant.components.weather.buienradar
|
# homeassistant.components.weather.buienradar
|
||||||
buienradar==0.91
|
buienradar==0.91
|
||||||
|
|
Loading…
Add table
Reference in a new issue