|
| 1 | +"""Test that advertising interval tracking is properly cleared when scanner pauses.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from habluetooth.advertisement_tracker import AdvertisementTracker |
| 6 | +from habluetooth.base_scanner import BaseHaScanner |
| 7 | +from habluetooth.central_manager import get_manager |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.asyncio |
| 11 | +async def test_scanner_paused_clears_timing_data(): |
| 12 | + """Test timing data is cleared when scanner pauses but intervals are preserved.""" |
| 13 | + tracker = AdvertisementTracker() |
| 14 | + source = "test_scanner" |
| 15 | + address = "AA:BB:CC:DD:EE:FF" |
| 16 | + |
| 17 | + # Simulate collecting timing data |
| 18 | + tracker.sources[address] = source |
| 19 | + tracker._timings[address] = [1.0, 2.0, 3.0] # Some timing data |
| 20 | + tracker.intervals[address] = 10.0 # Already learned interval |
| 21 | + |
| 22 | + # Call async_scanner_paused |
| 23 | + tracker.async_scanner_paused(source) |
| 24 | + |
| 25 | + # Check that timing data is cleared but interval is preserved |
| 26 | + assert address not in tracker._timings |
| 27 | + assert tracker.intervals[address] == 10.0 # Interval should still be there |
| 28 | + assert tracker.sources[address] == source # Source mapping should still be there |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.asyncio |
| 32 | +async def test_scanner_paused_only_affects_matching_source(): |
| 33 | + """Test that pausing only affects devices from the matching source.""" |
| 34 | + tracker = AdvertisementTracker() |
| 35 | + source1 = "scanner1" |
| 36 | + source2 = "scanner2" |
| 37 | + address1 = "AA:BB:CC:DD:EE:01" |
| 38 | + address2 = "AA:BB:CC:DD:EE:02" |
| 39 | + |
| 40 | + # Set up data for two sources |
| 41 | + tracker.sources[address1] = source1 |
| 42 | + tracker.sources[address2] = source2 |
| 43 | + tracker._timings[address1] = [1.0, 2.0] |
| 44 | + tracker._timings[address2] = [1.0, 2.0] |
| 45 | + tracker.intervals[address1] = 5.0 |
| 46 | + tracker.intervals[address2] = 6.0 |
| 47 | + |
| 48 | + # Pause only source1 |
| 49 | + tracker.async_scanner_paused(source1) |
| 50 | + |
| 51 | + # Check that only source1 timing is cleared |
| 52 | + assert address1 not in tracker._timings |
| 53 | + assert address2 in tracker._timings # source2 should still have timing data |
| 54 | + assert tracker.intervals[address1] == 5.0 # Intervals preserved |
| 55 | + assert tracker.intervals[address2] == 6.0 |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.asyncio |
| 59 | +async def test_connection_clears_timing_data(): |
| 60 | + """Test that timing data is cleared when a connection is initiated.""" |
| 61 | + # Get the manager that was set up by the fixture |
| 62 | + test_manager = get_manager() |
| 63 | + |
| 64 | + # Create actual BaseHaScanner to test the method |
| 65 | + real_scanner = BaseHaScanner( |
| 66 | + source="test_scanner", adapter="hci0", connectable=True |
| 67 | + ) |
| 68 | + # BaseHaScanner gets the manager internally via get_manager() |
| 69 | + |
| 70 | + # Set up some timing data |
| 71 | + address = "AA:BB:CC:DD:EE:FF" |
| 72 | + test_manager._advertisement_tracker.sources[address] = real_scanner.source |
| 73 | + test_manager._advertisement_tracker._timings[address] = [1.0, 2.0, 3.0] |
| 74 | + test_manager._advertisement_tracker.intervals[address] = 10.0 |
| 75 | + |
| 76 | + # Call _add_connecting which should clear timing data |
| 77 | + real_scanner._add_connecting(address) |
| 78 | + |
| 79 | + # Verify timing data was cleared but interval preserved |
| 80 | + assert address not in test_manager._advertisement_tracker._timings |
| 81 | + assert test_manager._advertisement_tracker.intervals.get(address) == 10.0 |
| 82 | + assert address in real_scanner._connect_in_progress |
0 commit comments