diff --git a/homeassistant/components/usb/__init__.py b/homeassistant/components/usb/__init__.py index 7fd27b32b9a..115b0fc3de9 100644 --- a/homeassistant/components/usb/__init__.py +++ b/homeassistant/components/usb/__init__.py @@ -113,7 +113,7 @@ class USBDiscovery: try: context = Context() - except ImportError: + except (ImportError, OSError): return False monitor = Monitor.from_netlink(context) diff --git a/tests/components/usb/test_init.py b/tests/components/usb/test_init.py index e511fac061e..f7b642c3390 100644 --- a/tests/components/usb/test_init.py +++ b/tests/components/usb/test_init.py @@ -243,7 +243,8 @@ async def test_discovered_by_scanner_after_started_no_vid_pid(hass): assert len(mock_config_flow.mock_calls) == 0 -async def test_non_matching_discovered_by_scanner_after_started(hass): +@pytest.mark.parametrize("exception_type", [ImportError, OSError]) +async def test_non_matching_discovered_by_scanner_after_started(hass, exception_type): """Test a device is discovered by the scanner after the started event that does not match.""" new_usb = [{"domain": "test1", "vid": "4444", "pid": "4444"}] @@ -258,7 +259,7 @@ async def test_non_matching_discovered_by_scanner_after_started(hass): ) ] - with patch("pyudev.Context", side_effect=ImportError), patch( + with patch("pyudev.Context", side_effect=exception_type), patch( "homeassistant.components.usb.async_get_usb", return_value=new_usb ), patch( "homeassistant.components.usb.comports", return_value=mock_comports