Kill bluetooth LE scanning gracefully when asked to shut down. (#22586)

* Kill bluetooth LE scanning gracefully when asked to shut down.

* Add missing argument.

* Refactor to use data instead of passing nonlocal variables about.

* Fix typo.
This commit is contained in:
Richard Mitchell 2019-04-17 21:48:17 +01:00 committed by Paulus Schoutsen
parent fa73b8e37a
commit f6349a6cf4
4 changed files with 18 additions and 3 deletions

View file

@ -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}

View file

@ -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": []

View file

@ -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": []

View file

@ -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