Repository: home-assistant/core
File: tests/components/enocean/test_config_flow.py (Python)

Code before:
        (
            UsbServiceInfo(
                device="/dev/enocean0",
                pid="6001",
                vid="0403",
                serial_number="1234",
                description="USB 300",
                manufacturer="EnOcean GmbH",
            ),
            "/dev/enocean0",
            "EnOcean USB 300",
        ),
    ],
)
async def test_usb_discovery(
    hass: HomeAssistant,
    # mock_usb_serial_by_id: MagicMock,
    usb_discovery_info: UsbServiceInfo,
    device: str,
    discovery_name: str,
) -> None:
    """Test usb discovery success path."""
    # test discovery step
    result = await hass.config_entries.flow.async_init(
        DOMAIN,
        context={"source": config_entries.SOURCE_USB},
        data=usb_discovery_info,
    )

    assert result["type"] is FlowResultType.FORM
    assert result["step_id"] == "usb_confirm"
    assert result["errors"] == {}

    # test invalid device path
    with patch(DONGLE_VALIDATE_PATH_METHOD, Mock(return_value=False)):
        result = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            {},
        )
    # await hass.async_block_till_done()

    assert result["type"] is FlowResultType.FORM
    assert result["step_id"] == "manual"
    assert result["errors"] == {"device": "invalid_dongle_path"}

    # test valid device path
    with patch(DONGLE_VALIDATE_PATH_METHOD, Mock(return_value=True)):
        result = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            {},
        )

Review comment (suggestion):
```suggestion
        context={"source": SOURCE_USB},
```

Code after:

async def test_usb_discovery(
    hass: HomeAssistant,
) -> None:
    """Test usb discovery success path."""
    usb_discovery_info = UsbServiceInfo(
        device="/dev/enocean0",
        pid="6001",
        vid="0403",
        serial_number="1234",
        description="USB 300",
        manufacturer="EnOcean GmbH",
    )
    device = "/dev/enocean0"
    # test discovery step
    result = await hass.config_entries.flow.async_init(
        DOMAIN,
        context={"source": SOURCE_USB},
        data=usb_discovery_info,
    )

    assert result["type"] is FlowResultType.FORM
    assert result["step_id"] == "usb_confirm"
    assert result["errors"] is None

    # test device path
    with (
        patch(DONGLE_VALIDATE_PATH_METHOD, Mock(return_value=True)),
        patch(SETUP_ENTRY_METHOD, AsyncMock(return_value=True)),
        patch(
            "homeassistant.components.usb.get_serial_by_id",
            side_effect=lambda x: x,
        ),
    ):
        result = await hass.config_entries.flow.async_configure(result["flow_id"], {})

    assert result["type"] is FlowResultType.CREATE_ENTRY
    assert result["title"] == MANUFACTURER
    assert result["data"] == {"device": device}
    assert result["context"]["unique_id"] == "0403:6001_1234_EnOcean GmbH_USB 300"
    assert result["context"]["title_placeholders"] == {
        "name": "USB 300 - /dev/enocean0, s/n: 1234 - EnOcean GmbH - 0403:6001"
    }
    assert result["result"].state is ConfigEntryState.LOADED


async def test_usb_discovery_already_configured_updates_path(
    hass: HomeAssistant,
) -> None:
    """Test usb discovery aborts when already configured and updates device path."""
    # Existing entry with the same unique_id but an old device path