|
19 | 19 | BluetoothScanningMode, |
20 | 20 | BluetoothServiceInfo, |
21 | 21 | HaBluetoothConnector, |
| 22 | + async_clear_address_from_match_history, |
22 | 23 | async_process_advertisements, |
23 | 24 | async_rediscover_address, |
24 | 25 | async_track_unavailable, |
@@ -1175,6 +1176,61 @@ async def test_rediscovery( |
1175 | 1176 | assert mock_config_flow.mock_calls[1][1][0] == "switchbot" |
1176 | 1177 |
|
1177 | 1178 |
|
| 1179 | +@pytest.mark.usefixtures("enable_bluetooth") |
| 1180 | +async def test_clear_address_from_match_history( |
| 1181 | + hass: HomeAssistant, mock_bleak_scanner_start: MagicMock |
| 1182 | +) -> None: |
| 1183 | + """Test clearing match history without re-triggering discovery.""" |
| 1184 | + mock_bt = [ |
| 1185 | + {"domain": "switchbot", "service_uuid": "cba20d00-224d-11e6-9fb8-0002a5d5c51b"} |
| 1186 | + ] |
| 1187 | + with ( |
| 1188 | + patch( |
| 1189 | + "homeassistant.components.bluetooth.async_get_bluetooth", |
| 1190 | + return_value=mock_bt, |
| 1191 | + ), |
| 1192 | + patch.object(hass.config_entries.flow, "async_init") as mock_config_flow, |
| 1193 | + ): |
| 1194 | + await async_setup_with_default_adapter(hass) |
| 1195 | + hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) |
| 1196 | + await hass.async_block_till_done() |
| 1197 | + |
| 1198 | + assert len(mock_bleak_scanner_start.mock_calls) == 1 |
| 1199 | + |
| 1200 | + switchbot_device = generate_ble_device("44:44:33:11:23:45", "wohand") |
| 1201 | + switchbot_adv = generate_advertisement_data( |
| 1202 | + local_name="wohand", service_uuids=["cba20d00-224d-11e6-9fb8-0002a5d5c51b"] |
| 1203 | + ) |
| 1204 | + switchbot_adv_2 = generate_advertisement_data( |
| 1205 | + local_name="wohand", |
| 1206 | + service_uuids=["cba20d00-224d-11e6-9fb8-0002a5d5c51b"], |
| 1207 | + manufacturer_data={1: b"\x01"}, |
| 1208 | + ) |
| 1209 | + inject_advertisement(hass, switchbot_device, switchbot_adv) |
| 1210 | + await hass.async_block_till_done() |
| 1211 | + |
| 1212 | + inject_advertisement(hass, switchbot_device, switchbot_adv) |
| 1213 | + await hass.async_block_till_done() |
| 1214 | + |
| 1215 | + assert len(mock_config_flow.mock_calls) == 1 |
| 1216 | + assert mock_config_flow.mock_calls[0][1][0] == "switchbot" |
| 1217 | + |
| 1218 | + # Clear match history - should NOT trigger immediate rediscovery |
| 1219 | + async_clear_address_from_match_history(hass, "44:44:33:11:23:45") |
| 1220 | + await hass.async_block_till_done() |
| 1221 | + |
| 1222 | + # No new discovery should have been triggered |
| 1223 | + assert len(mock_config_flow.mock_calls) == 1 |
| 1224 | + |
| 1225 | + # But when we inject new advertisement with different data, it should be discovered |
| 1226 | + inject_advertisement(hass, switchbot_device, switchbot_adv_2) |
| 1227 | + await hass.async_block_till_done() |
| 1228 | + |
| 1229 | + # Now discovery should happen because history was cleared and data changed |
| 1230 | + assert len(mock_config_flow.mock_calls) == 2 |
| 1231 | + assert mock_config_flow.mock_calls[1][1][0] == "switchbot" |
| 1232 | + |
| 1233 | + |
1178 | 1234 | @pytest.mark.usefixtures("macos_adapter") |
1179 | 1235 | async def test_async_discovered_device_api( |
1180 | 1236 | hass: HomeAssistant, mock_bleak_scanner_start: MagicMock |
|
0 commit comments