From 7c99567b65a5124c202d37f85a71b475282a303b Mon Sep 17 00:00:00 2001 From: Frederik Bolding Date: Sat, 31 Mar 2018 23:22:54 +0200 Subject: [PATCH] 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 --- .../device_tracker/bluetooth_tracker.py | 32 +++++++++++++------ requirements_all.txt | 3 ++ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/device_tracker/bluetooth_tracker.py b/homeassistant/components/device_tracker/bluetooth_tracker.py index 9d41611d9a2..807f6c0d0a4 100644 --- a/homeassistant/components/device_tracker/bluetooth_tracker.py +++ b/homeassistant/components/device_tracker/bluetooth_tracker.py @@ -17,12 +17,15 @@ import homeassistant.util.dt as dt_util _LOGGER = logging.getLogger(__name__) -REQUIREMENTS = ['pybluez==0.22'] +REQUIREMENTS = ['pybluez==0.22', 'bt_proximity==0.1.2'] BT_PREFIX = 'BT_' +CONF_REQUEST_RSSI = 'request_rssi' + 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.""" # pylint: disable=import-error import bluetooth + from bt_proximity import BluetoothRSSI - def see_device(device): + def see_device(mac, name, rssi=None): """Mark a device as seen.""" - see(mac=BT_PREFIX + device[0], host_name=device[1], - source_type=SOURCE_TYPE_BLUETOOTH) + attributes = {} + 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(): """Discover Bluetooth devices.""" @@ -64,27 +71,32 @@ def setup_scanner(hass, config, see, discovery_info=None): if track_new: for dev in discover_devices(): 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]) - see_device(dev) + see_device(dev[0], dev[1]) interval = config.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL) + request_rssi = config.get(CONF_REQUEST_RSSI, False) + def update_bluetooth(now): """Lookup Bluetooth device and update status.""" try: if track_new: for dev in discover_devices(): 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]) for mac in devs_to_track: _LOGGER.debug("Scanning %s", mac) 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 continue - see_device((mac, result)) + see_device(mac, result, rssi) except bluetooth.BluetoothError: _LOGGER.exception("Error looking up Bluetooth device") track_point_in_utc_time( diff --git a/requirements_all.txt b/requirements_all.txt index 7eeed2d1ef2..b2098b37f63 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -172,6 +172,9 @@ boto3==1.4.7 # homeassistant.scripts.credstash botocore==1.7.34 +# homeassistant.components.device_tracker.bluetooth_tracker +bt_proximity==0.1.2 + # homeassistant.components.sensor.buienradar # homeassistant.components.weather.buienradar buienradar==0.91