Al-Adab Al-Mufrad, Book 43, Hadith 1056 (English)

Ibn 'Abbas said, "The verse, 'Do not enter houses other than your own until you have asked permission and greeted their inhabitants' (24: 27) has an exception made to it when Allah says, 'There is nothing wrong in your entering houses where no one lives where there is some service for you. Allah knows what you divulge and what you conceal.'"

حَدَّثَنَا إِسْحَاقُ، قَالَ‏:‏ حَدَّثَنَا عَلِيُّ بْنُ الْحُسَيْنِ قَالَ‏:‏ حَدَّثَنِي أَبِي، عَنْ يَزِيدَ النَّحْوِيِّ، عَنْ عِكْرِمَةَ، عَنِ ابْنِ عَبَّاسٍ قَالَ‏:‏ ‏{‏لاَ تَدْخُلُوا بُيُوتًا غَيْرَ بُيُوتِكُمْ حَتَّى تَسْتَأْنِسُوا وَتُسَلِّمُوا عَلَى أَهْلِهَا‏}‏، وَاسْتَثْنَى مِنْ ذَلِكَ، فَقَالَ‏:‏ ‏{‏لَيْسَ عَلَيْكُمْ جُنَاحٌ أَنْ تَدْخُلُوا بُيُوتًا غَيْرَ مَسْكُونَةٍ فِيهَا مَتَاعٌ لَكُمْ وَاللهُ يَعْلَمُ مَا تُبْدُونَ وَمَا تَكْتُمُونَ‏}‏‏.‏

sunnah.com