Ensure bluetooth can be reloaded when hot plugging a bluetooth adapter (#75699)

This commit is contained in:
J. Nick Koston 2022-07-24 19:23:23 -05:00 committed by GitHub
parent 511af3c455
commit 22ca28b93d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View file

@ -440,5 +440,11 @@ class BluetoothManager:
self._cancel_unavailable_tracking() self._cancel_unavailable_tracking()
self._cancel_unavailable_tracking = None self._cancel_unavailable_tracking = None
if self.scanner: if self.scanner:
await self.scanner.stop() try:
await self.scanner.stop()
except BleakError as ex:
# This is not fatal, and they may want to reload
# the config entry to restart the scanner if they
# change the bluetooth dongle.
_LOGGER.error("Error stopping scanner: %s", ex)
uninstall_multiple_bleak_catcher() uninstall_multiple_bleak_catcher()

View file

@ -1213,3 +1213,20 @@ async def test_getting_the_scanner_returns_the_wrapped_instance(hass, enable_blu
"""Test getting the scanner returns the wrapped instance.""" """Test getting the scanner returns the wrapped instance."""
scanner = bluetooth.async_get_scanner(hass) scanner = bluetooth.async_get_scanner(hass)
assert isinstance(scanner, models.HaBleakScannerWrapper) assert isinstance(scanner, models.HaBleakScannerWrapper)
async def test_config_entry_can_be_reloaded_when_stop_raises(
hass, caplog, enable_bluetooth
):
"""Test we can reload if stopping the scanner raises."""
entry = hass.config_entries.async_entries(bluetooth.DOMAIN)[0]
assert entry.state == ConfigEntryState.LOADED
with patch(
"homeassistant.components.bluetooth.HaBleakScanner.stop", side_effect=BleakError
):
await hass.config_entries.async_reload(entry.entry_id)
await hass.async_block_till_done()
assert entry.state == ConfigEntryState.LOADED
assert "Error stopping scanner" in caplog.text