From 6dcfe861fdc1907cdd88909d4a88ab2de4029586 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 16 Apr 2024 18:34:57 -0500 Subject: [PATCH] Bump habluetooth to 2.5.2 (#115721) --- .../components/bluetooth/manifest.json | 2 +- homeassistant/package_constraints.txt | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/bluetooth/test_scanner.py | 233 +++++++++--------- 5 files changed, 121 insertions(+), 120 deletions(-) diff --git a/homeassistant/components/bluetooth/manifest.json b/homeassistant/components/bluetooth/manifest.json index 58009216464..5939a03cefc 100644 --- a/homeassistant/components/bluetooth/manifest.json +++ b/homeassistant/components/bluetooth/manifest.json @@ -20,6 +20,6 @@ "bluetooth-auto-recovery==1.4.0", "bluetooth-data-tools==1.19.0", "dbus-fast==2.21.1", - "habluetooth==2.4.2" + "habluetooth==2.5.2" ] } diff --git a/homeassistant/package_constraints.txt b/homeassistant/package_constraints.txt index b6f814c9f58..92522a69e53 100644 --- a/homeassistant/package_constraints.txt +++ b/homeassistant/package_constraints.txt @@ -28,7 +28,7 @@ dbus-fast==2.21.1 fnv-hash-fast==0.5.0 ha-av==10.1.1 ha-ffmpeg==3.2.0 -habluetooth==2.4.2 +habluetooth==2.5.2 hass-nabucasa==0.78.0 hassil==1.6.1 home-assistant-bluetooth==1.12.0 diff --git a/requirements_all.txt b/requirements_all.txt index 64d67ada712..c4e22294747 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1029,7 +1029,7 @@ ha-philipsjs==3.1.1 habitipy==0.2.0 # homeassistant.components.bluetooth -habluetooth==2.4.2 +habluetooth==2.5.2 # homeassistant.components.cloud hass-nabucasa==0.78.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index fd3bad4398b..be395c42054 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -843,7 +843,7 @@ ha-philipsjs==3.1.1 habitipy==0.2.0 # homeassistant.components.bluetooth -habluetooth==2.4.2 +habluetooth==2.5.2 # homeassistant.components.cloud hass-nabucasa==0.78.0 diff --git a/tests/components/bluetooth/test_scanner.py b/tests/components/bluetooth/test_scanner.py index 523364e0dfd..5658aea523b 100644 --- a/tests/components/bluetooth/test_scanner.py +++ b/tests/components/bluetooth/test_scanner.py @@ -22,7 +22,6 @@ from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util from . import ( - _get_manager, async_setup_with_one_adapter, generate_advertisement_data, generate_ble_device, @@ -183,7 +182,7 @@ async def test_adapter_needs_reset_at_start( with ( patch( "habluetooth.scanner.OriginalBleakScanner.start", - side_effect=[BleakError(error), None], + side_effect=[BleakError(error), BleakError(error), None], ), patch( "habluetooth.util.recover_adapter", return_value=True @@ -239,46 +238,47 @@ async def test_recovery_from_dbus_restart( assert called_start == 1 - start_time_monotonic = time.monotonic() - mock_discovered = [MagicMock()] + start_time_monotonic = time.monotonic() + mock_discovered = [MagicMock()] - # Ensure we don't restart the scanner if we don't need to - with patch_bluetooth_time( - start_time_monotonic + 10, - ): - async_fire_time_changed(hass, dt_util.utcnow() + SCANNER_WATCHDOG_INTERVAL) - await hass.async_block_till_done() + # Ensure we don't restart the scanner if we don't need to + with patch_bluetooth_time( + start_time_monotonic + 10, + ): + async_fire_time_changed(hass, dt_util.utcnow() + SCANNER_WATCHDOG_INTERVAL) + await hass.async_block_till_done() - assert called_start == 1 + assert called_start == 1 - # Fire a callback to reset the timer - with patch_bluetooth_time( - start_time_monotonic, - ): - _callback( - generate_ble_device("44:44:33:11:23:42", "any_name"), - generate_advertisement_data(local_name="any_name"), - ) + # Fire a callback to reset the timer + with patch_bluetooth_time( + start_time_monotonic, + ): + _callback( + generate_ble_device("44:44:33:11:23:42", "any_name"), + generate_advertisement_data(local_name="any_name"), + ) - # Ensure we don't restart the scanner if we don't need to - with patch_bluetooth_time( - start_time_monotonic + 20, - ): - async_fire_time_changed(hass, dt_util.utcnow() + SCANNER_WATCHDOG_INTERVAL) - await hass.async_block_till_done() + # Ensure we don't restart the scanner if we don't need to + with patch_bluetooth_time( + start_time_monotonic + 20, + ): + async_fire_time_changed(hass, dt_util.utcnow() + SCANNER_WATCHDOG_INTERVAL) + await hass.async_block_till_done() - assert called_start == 1 + assert called_start == 1 - # We hit the timer, so we restart the scanner - with patch_bluetooth_time( - start_time_monotonic + SCANNER_WATCHDOG_TIMEOUT + 20, - ): - async_fire_time_changed( - hass, dt_util.utcnow() + SCANNER_WATCHDOG_INTERVAL + timedelta(seconds=20) - ) - await hass.async_block_till_done() + # We hit the timer, so we restart the scanner + with patch_bluetooth_time( + start_time_monotonic + SCANNER_WATCHDOG_TIMEOUT + 20, + ): + async_fire_time_changed( + hass, + dt_util.utcnow() + SCANNER_WATCHDOG_INTERVAL + timedelta(seconds=20), + ) + await hass.async_block_till_done() - assert called_start == 2 + assert called_start == 2 async def test_adapter_recovery(hass: HomeAssistant, one_adapter: None) -> None: @@ -327,43 +327,42 @@ async def test_adapter_recovery(hass: HomeAssistant, one_adapter: None) -> None: assert called_start == 1 - scanner = _get_manager() - mock_discovered = [MagicMock()] + mock_discovered = [MagicMock()] - # Ensure we don't restart the scanner if we don't need to - with patch_bluetooth_time( - start_time_monotonic + 10, - ): - async_fire_time_changed(hass, dt_util.utcnow() + SCANNER_WATCHDOG_INTERVAL) - await hass.async_block_till_done() + # Ensure we don't restart the scanner if we don't need to + with patch_bluetooth_time( + start_time_monotonic + 10, + ): + async_fire_time_changed(hass, dt_util.utcnow() + SCANNER_WATCHDOG_INTERVAL) + await hass.async_block_till_done() - assert called_start == 1 + assert called_start == 1 - # Ensure we don't restart the scanner if we don't need to - with patch_bluetooth_time( - start_time_monotonic + 20, - ): - async_fire_time_changed(hass, dt_util.utcnow() + SCANNER_WATCHDOG_INTERVAL) - await hass.async_block_till_done() + # Ensure we don't restart the scanner if we don't need to + with patch_bluetooth_time( + start_time_monotonic + 20, + ): + async_fire_time_changed(hass, dt_util.utcnow() + SCANNER_WATCHDOG_INTERVAL) + await hass.async_block_till_done() - assert called_start == 1 + assert called_start == 1 - # We hit the timer with no detections, so we reset the adapter and restart the scanner - with ( - patch_bluetooth_time( - start_time_monotonic - + SCANNER_WATCHDOG_TIMEOUT - + SCANNER_WATCHDOG_INTERVAL.total_seconds(), - ), - patch( - "habluetooth.util.recover_adapter", return_value=True - ) as mock_recover_adapter, - ): - async_fire_time_changed(hass, dt_util.utcnow() + SCANNER_WATCHDOG_INTERVAL) - await hass.async_block_till_done() + # We hit the timer with no detections, so we reset the adapter and restart the scanner + with ( + patch_bluetooth_time( + start_time_monotonic + + SCANNER_WATCHDOG_TIMEOUT + + SCANNER_WATCHDOG_INTERVAL.total_seconds(), + ), + patch( + "habluetooth.util.recover_adapter", return_value=True + ) as mock_recover_adapter, + ): + async_fire_time_changed(hass, dt_util.utcnow() + SCANNER_WATCHDOG_INTERVAL) + await hass.async_block_till_done() - assert len(mock_recover_adapter.mock_calls) == 1 - assert called_start == 2 + assert len(mock_recover_adapter.mock_calls) == 1 + assert called_start == 2 async def test_adapter_scanner_fails_to_start_first_time( @@ -418,61 +417,61 @@ async def test_adapter_scanner_fails_to_start_first_time( assert called_start == 1 - scanner = _get_manager() - mock_discovered = [MagicMock()] + mock_discovered = [MagicMock()] - # Ensure we don't restart the scanner if we don't need to - with patch_bluetooth_time( - start_time_monotonic + 10, - ): - async_fire_time_changed(hass, dt_util.utcnow() + SCANNER_WATCHDOG_INTERVAL) - await hass.async_block_till_done() + # Ensure we don't restart the scanner if we don't need to + with patch_bluetooth_time( + start_time_monotonic + 10, + ): + async_fire_time_changed(hass, dt_util.utcnow() + SCANNER_WATCHDOG_INTERVAL) + await hass.async_block_till_done() - assert called_start == 1 + assert called_start == 1 - # Ensure we don't restart the scanner if we don't need to - with patch_bluetooth_time( - start_time_monotonic + 20, - ): - async_fire_time_changed(hass, dt_util.utcnow() + SCANNER_WATCHDOG_INTERVAL) - await hass.async_block_till_done() + # Ensure we don't restart the scanner if we don't need to + with patch_bluetooth_time( + start_time_monotonic + 20, + ): + async_fire_time_changed(hass, dt_util.utcnow() + SCANNER_WATCHDOG_INTERVAL) + await hass.async_block_till_done() - assert called_start == 1 + assert called_start == 1 - # We hit the timer with no detections, so we reset the adapter and restart the scanner - with ( - patch_bluetooth_time( - start_time_monotonic - + SCANNER_WATCHDOG_TIMEOUT - + SCANNER_WATCHDOG_INTERVAL.total_seconds(), - ), - patch( - "habluetooth.util.recover_adapter", return_value=True - ) as mock_recover_adapter, - ): - async_fire_time_changed(hass, dt_util.utcnow() + SCANNER_WATCHDOG_INTERVAL) - await hass.async_block_till_done() + # We hit the timer with no detections, so we reset the adapter and restart the scanner + with ( + patch_bluetooth_time( + start_time_monotonic + + SCANNER_WATCHDOG_TIMEOUT + + SCANNER_WATCHDOG_INTERVAL.total_seconds(), + ), + patch( + "habluetooth.util.recover_adapter", return_value=True + ) as mock_recover_adapter, + ): + async_fire_time_changed(hass, dt_util.utcnow() + SCANNER_WATCHDOG_INTERVAL) + await hass.async_block_till_done() - assert len(mock_recover_adapter.mock_calls) == 1 - assert called_start == 3 + assert len(mock_recover_adapter.mock_calls) == 1 + assert called_start == 4 - # We hit the timer again the previous start call failed, make sure - # we try again - with ( - patch_bluetooth_time( - start_time_monotonic - + SCANNER_WATCHDOG_TIMEOUT - + SCANNER_WATCHDOG_INTERVAL.total_seconds(), - ), - patch( - "habluetooth.util.recover_adapter", return_value=True - ) as mock_recover_adapter, - ): - async_fire_time_changed(hass, dt_util.utcnow() + SCANNER_WATCHDOG_INTERVAL) - await hass.async_block_till_done() + now_monotonic = time.monotonic() + # We hit the timer again the previous start call failed, make sure + # we try again + with ( + patch_bluetooth_time( + now_monotonic + + SCANNER_WATCHDOG_TIMEOUT * 2 + + SCANNER_WATCHDOG_INTERVAL.total_seconds(), + ), + patch( + "habluetooth.util.recover_adapter", return_value=True + ) as mock_recover_adapter, + ): + async_fire_time_changed(hass, dt_util.utcnow() + SCANNER_WATCHDOG_INTERVAL) + await hass.async_block_till_done() - assert len(mock_recover_adapter.mock_calls) == 1 - assert called_start == 4 + assert len(mock_recover_adapter.mock_calls) == 1 + assert called_start == 5 async def test_adapter_fails_to_start_and_takes_a_bit_to_init( @@ -497,9 +496,11 @@ async def test_adapter_fails_to_start_and_takes_a_bit_to_init( nonlocal called_start called_start += 1 if called_start == 1: - raise BleakError("org.bluez.Error.InProgress") - if called_start == 2: raise BleakError("org.freedesktop.DBus.Error.UnknownObject") + if called_start == 2: + raise BleakError("org.bluez.Error.InProgress") + if called_start == 3: + raise BleakError("org.bluez.Error.InProgress") async def stop(self, *args, **kwargs): """Mock Start.""" @@ -538,7 +539,7 @@ async def test_adapter_fails_to_start_and_takes_a_bit_to_init( ): await async_setup_with_one_adapter(hass) - assert called_start == 3 + assert called_start == 4 assert len(mock_recover_adapter.mock_calls) == 1 assert "Waiting for adapter to initialize" in caplog.text