diff --git a/homeassistant/components/bluetooth_le_tracker/device_tracker.py b/homeassistant/components/bluetooth_le_tracker/device_tracker.py index f1aab4e1fd5..f24b943f188 100644 --- a/homeassistant/components/bluetooth_le_tracker/device_tracker.py +++ b/homeassistant/components/bluetooth_le_tracker/device_tracker.py @@ -6,10 +6,13 @@ from homeassistant.components.device_tracker import ( YAML_DEVICES, CONF_TRACK_NEW, CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL, load_config, SOURCE_TYPE_BLUETOOTH_LE ) +from homeassistant.const import EVENT_HOMEASSISTANT_STOP import homeassistant.util.dt as dt_util _LOGGER = logging.getLogger(__name__) +DATA_BLE = 'BLE' +DATA_BLE_ADAPTER = 'ADAPTER' BLE_PREFIX = 'BLE_' MIN_SEEN_NEW = 5 @@ -19,6 +22,17 @@ def setup_scanner(hass, config, see, discovery_info=None): # pylint: disable=import-error import pygatt new_devices = {} + hass.data.setdefault(DATA_BLE, {DATA_BLE_ADAPTER: None}) + + async def async_stop(event): + """Try to shut down the bluetooth child process nicely.""" + # These should never be unset at the point this runs, but just for + # safety's sake, use `get`. + adapter = hass.data.get(DATA_BLE, {}).get(DATA_BLE_ADAPTER) + if adapter is not None: + adapter.kill() + + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_stop) def see_device(address, name, new_device=False): """Mark a device as seen.""" @@ -48,6 +62,7 @@ def setup_scanner(hass, config, see, discovery_info=None): _LOGGER.debug("Discovering Bluetooth LE devices") try: adapter = pygatt.GATTToolBackend() + hass.data[DATA_BLE][DATA_BLE_ADAPTER] = adapter devs = adapter.scan() devices = {x['address']: x['name'] for x in devs} diff --git a/homeassistant/components/bluetooth_le_tracker/manifest.json b/homeassistant/components/bluetooth_le_tracker/manifest.json index cd67ec31536..d2f8f10290e 100644 --- a/homeassistant/components/bluetooth_le_tracker/manifest.json +++ b/homeassistant/components/bluetooth_le_tracker/manifest.json @@ -3,7 +3,7 @@ "name": "Bluetooth le tracker", "documentation": "https://www.home-assistant.io/components/bluetooth_le_tracker", "requirements": [ - "pygatt[GATTTOOL]==3.2.0" + "pygatt[GATTTOOL]==4.0.1" ], "dependencies": [], "codeowners": [] diff --git a/homeassistant/components/skybeacon/manifest.json b/homeassistant/components/skybeacon/manifest.json index 8d2f758aed2..893a1f3469e 100644 --- a/homeassistant/components/skybeacon/manifest.json +++ b/homeassistant/components/skybeacon/manifest.json @@ -3,7 +3,7 @@ "name": "Skybeacon", "documentation": "https://www.home-assistant.io/components/skybeacon", "requirements": [ - "pygatt[GATTTOOL]==3.2.0" + "pygatt[GATTTOOL]==4.0.1" ], "dependencies": [], "codeowners": [] diff --git a/requirements_all.txt b/requirements_all.txt index b8b81d90017..b861e8e2a49 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1061,7 +1061,7 @@ pyfttt==0.3 # homeassistant.components.bluetooth_le_tracker # homeassistant.components.skybeacon -pygatt[GATTTOOL]==3.2.0 +pygatt[GATTTOOL]==4.0.1 # homeassistant.components.gogogate2 pygogogate2==0.1.1