Fix Synology DSM not detecting USB drives connected after integration startup (#182786)

This commit is contained in:
JJ del Rio
2026-10-18 21:24:16 +02:00
committed by GitHub
parent 067173d3b9
commit 4496be296b
2 changed files with 52 additions and 0 deletions
@@ -273,8 +273,10 @@ class SynoApi:
self._with_information = bool(
self._fetching_entities.get(SynoDSMInformation.API_KEY)
)
# Keep fetching while no device is connected, as no entity subscribes yet
self._with_external_usb = bool(
self._fetching_entities.get(SynoCoreExternalUSB.API_KEY)
or (self.external_usb is not None and not self.external_usb.get_devices)
)
self._with_hardware = bool(
self._fetching_entities.get(SynoCoreHardware.API_KEY)
@@ -73,6 +73,13 @@ def mock_dsm_with_usb():
yield dsm
@pytest.fixture
def mock_dsm_with_usb_no_devices(mock_dsm_with_usb: MagicMock) -> MagicMock:
"""Mock a successful service with USB support but no USB device connected."""
mock_dsm_with_usb.external_usb.get_devices = mock_dsm_external_usb_devices_usb0()
return mock_dsm_with_usb
@pytest.fixture
def mock_dsm_without_usb():
"""Mock a successful service without USB devices."""
@@ -290,6 +297,49 @@ async def test_external_usb_new_device(
assert sensor.attributes[attr_key] == attr_value
async def test_external_usb_first_device(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_dsm_with_usb_no_devices: MagicMock,
setup_dsm_with_usb: MagicMock,
) -> None:
"""Test Synology DSM USB adding a device when none was connected at setup."""
entity_id = "sensor.nas_meontheinternet_com_usb_disk_1_status"
entry = setup_dsm_with_usb.mock_entry
coordinator = entry.runtime_data.coordinator_central
await coordinator.async_refresh()
await hass.async_block_till_done()
assert hass.states.get(entity_id) is None
# Simulate a USB disk being added, which is only seen by the next update
def _update(with_information: bool) -> None:
setup_dsm_with_usb.external_usb.get_devices = (
mock_dsm_external_usb_devices_usb1()
)
setup_dsm_with_usb.update.side_effect = _update
await coordinator.async_refresh()
await hass.async_block_till_done()
assert (state := hass.states.get(entity_id))
assert state.state == "normal"
# The API is no longer updated once all entities of the device are disabled
for entity_entry in er.async_entries_for_config_entry(
entity_registry, entry.entry_id
):
if "USB Disk" in entity_entry.unique_id and not entity_entry.disabled:
entity_registry.async_update_entity(
entity_entry.entity_id, disabled_by=er.RegistryEntryDisabler.USER
)
await hass.async_block_till_done()
await coordinator.async_refresh()
await hass.async_block_till_done()
assert entry.runtime_data.api.external_usb is None
async def test_external_usb_availability(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,