From 4496be296bef854253d4076124e99f71b217165d Mon Sep 17 00:00:00 2001 From: JJ del Rio <641373+partymola@users.noreply.github.com> Date: Sun, 18 Oct 2026 20:24:16 +0100 Subject: [PATCH] Fix Synology DSM not detecting USB drives connected after integration startup (#182786) --- .../components/synology_dsm/common.py | 2 + tests/components/synology_dsm/test_sensor.py | 50 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/homeassistant/components/synology_dsm/common.py b/homeassistant/components/synology_dsm/common.py index eb0d2317ded8..c199283ee1d2 100644 --- a/homeassistant/components/synology_dsm/common.py +++ b/homeassistant/components/synology_dsm/common.py @@ -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) diff --git a/tests/components/synology_dsm/test_sensor.py b/tests/components/synology_dsm/test_sensor.py index f8da3ba250f3..a04d4c767a08 100644 --- a/tests/components/synology_dsm/test_sensor.py +++ b/tests/components/synology_dsm/test_sensor.py @@ -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,