1
0
mirror of https://github.com/elisspace/core.git synced 2026-08-29 15:43:55 +00:00

Make usb.async_is_plugged_in reflect current state

This commit is contained in:
Erik
2023-01-08 13:31:59 +01:00
parent 9c39c51a96
commit 5dfb9f3f8d
2 changed files with 10 additions and 1 deletions

View File

@@ -85,7 +85,7 @@ async def async_is_plugged_in(hass: HomeAssistant, matcher: USBCallbackMatcher)
await usb_discovery.async_request_scan()
return any(
_is_matching(USBDevice(*device_tuple), matcher)
for device_tuple in usb_discovery.seen
for device_tuple in usb_discovery.plugged_in
)
@@ -182,6 +182,7 @@ class USBDiscovery:
"""Init USB Discovery."""
self.hass = hass
self.usb = usb
self.plugged_in: set[tuple[str, ...]] = set()
self.seen: set[tuple[str, ...]] = set()
self.observer_active = False
self._request_debouncer: Debouncer[Coroutine[Any, Any, None]] | None = None
@@ -263,6 +264,7 @@ class USBDiscovery:
"""Process a USB discovery."""
_LOGGER.debug("Discovered USB Device: %s", device)
device_tuple = dataclasses.astuple(device)
self.plugged_in.add(device_tuple)
if device_tuple in self.seen:
return
self.seen.add(device_tuple)
@@ -299,6 +301,7 @@ class USBDiscovery:
@hass_callback
def _async_process_ports(self, ports: list[ListPortInfo]) -> None:
"""Process each discovered port."""
self.plugged_in = set()
for port in ports:
if port.vid is None and port.pid is None:
continue

View File

@@ -876,6 +876,12 @@ async def test_async_is_plugged_in(hass):
await hass.async_block_till_done()
assert await usb.async_is_plugged_in(hass, matcher)
with patch("homeassistant.components.usb.comports", return_value=[]), patch.object(
hass.config_entries.flow, "async_init"
), patch("homeassistant.components.usb.REQUEST_SCAN_COOLDOWN", 0):
await hass.async_block_till_done()
assert not await usb.async_is_plugged_in(hass, matcher)
@pytest.mark.parametrize(
"matcher",